2010-07-08 20:01:03 +02:00
|
|
|
#!/usr/bin/python
|
2010-07-07 02:53:27 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2010-07-15 01:53:22 +02:00
|
|
|
#Copyright 2010 Steffen Schaumburg
|
2010-07-07 02:53:27 +02:00
|
|
|
#This program is free software: you can redistribute it and/or modify
|
|
|
|
#it under the terms of the GNU Affero General Public License as published by
|
|
|
|
#the Free Software Foundation, version 3 of the License.
|
|
|
|
#
|
|
|
|
#This program is distributed in the hope that it will be useful,
|
|
|
|
#but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
#GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
#You should have received a copy of the GNU Affero General Public License
|
|
|
|
#along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#In the "official" distribution you can find the license in agpl-3.0.txt.
|
|
|
|
|
|
|
|
"""This file is to fetch summaries through IMAP and pass them on to the appropriate parser"""
|
|
|
|
#see http://docs.python.org/library/imaplib.html for the python interface
|
|
|
|
#see http://tools.ietf.org/html/rfc2060#section-6.4.4 for IMAP4 search criteria
|
|
|
|
|
2010-09-23 08:10:30 +02:00
|
|
|
import L10n
|
|
|
|
_ = L10n.get_translation()
|
|
|
|
|
2010-07-08 19:46:25 +02:00
|
|
|
from imaplib import IMAP4, IMAP4_SSL
|
2010-09-04 10:33:16 +02:00
|
|
|
import sys
|
|
|
|
import codecs
|
|
|
|
import re
|
|
|
|
|
|
|
|
import Configuration
|
|
|
|
import Database
|
2010-09-04 19:14:08 +02:00
|
|
|
from Exceptions import FpdbParseError
|
2010-09-04 10:33:16 +02:00
|
|
|
import SQL
|
|
|
|
import Options
|
2010-07-07 06:58:42 +02:00
|
|
|
import PokerStarsSummary
|
2010-09-10 11:18:16 +02:00
|
|
|
import FullTiltPokerSummary
|
2010-07-07 06:58:42 +02:00
|
|
|
|
2010-09-04 10:33:16 +02:00
|
|
|
|
2010-09-17 19:18:16 +02:00
|
|
|
def splitPokerStarsSummaries(summaryText): #TODO: this needs to go to PSS.py
|
2010-09-10 11:18:16 +02:00
|
|
|
re_SplitTourneys = PokerStarsSummary.PokerStarsSummary.re_SplitTourneys
|
|
|
|
splitSummaries = re.split(re_SplitTourneys, summaryText)
|
|
|
|
|
|
|
|
if len(splitSummaries) <= 1:
|
2010-09-17 19:18:16 +02:00
|
|
|
print _("DEBUG: re_SplitTourneys isn't matching")
|
2010-09-10 11:18:16 +02:00
|
|
|
|
|
|
|
return splitSummaries
|
|
|
|
|
2010-09-17 19:18:16 +02:00
|
|
|
def splitFullTiltSummaries(summaryText):#TODO: this needs to go to FTPS.py
|
2010-09-10 11:18:16 +02:00
|
|
|
re_SplitTourneys = FullTiltPokerSummary.FullTiltPokerSummary.re_SplitTourneys
|
|
|
|
splitSummaries = re.split(re_SplitTourneys, summaryText)
|
|
|
|
|
|
|
|
if len(splitSummaries) <= 1:
|
2010-09-17 19:18:16 +02:00
|
|
|
print _("DEBUG: re_SplitTourneys isn't matching")
|
2010-09-10 11:18:16 +02:00
|
|
|
|
2010-07-07 06:58:42 +02:00
|
|
|
return splitSummaries
|
|
|
|
|
2010-07-08 19:46:25 +02:00
|
|
|
def run(config, db):
|
2010-07-08 21:46:40 +02:00
|
|
|
#print "start of IS.run"
|
2010-07-08 19:46:25 +02:00
|
|
|
server=None
|
|
|
|
#try:
|
2010-08-10 03:28:57 +02:00
|
|
|
#print "useSSL",config.useSsl,"host",config.host
|
|
|
|
if config.useSsl:
|
|
|
|
server = IMAP4_SSL(config.host)
|
2010-07-08 19:46:25 +02:00
|
|
|
else:
|
2010-08-10 03:28:57 +02:00
|
|
|
server = IMAP4(config.host)
|
|
|
|
response = server.login(config.username, config.password) #TODO catch authentication error
|
2010-08-13 04:48:06 +02:00
|
|
|
print _("response to logging in:"),response
|
2010-07-07 08:17:18 +02:00
|
|
|
#print "server.list():",server.list() #prints list of folders
|
2010-07-07 02:53:27 +02:00
|
|
|
|
2010-08-10 03:28:57 +02:00
|
|
|
response = server.select(config.folder)
|
2010-07-07 08:17:18 +02:00
|
|
|
#print "response to selecting INBOX:",response
|
|
|
|
if response[0]!="OK":
|
|
|
|
raise error #TODO: show error message
|
2010-07-07 02:53:27 +02:00
|
|
|
|
2010-07-07 08:17:18 +02:00
|
|
|
neededMessages=[]
|
|
|
|
response, searchData = server.search(None, "SUBJECT", "PokerStars Tournament History Request")
|
|
|
|
for messageNumber in searchData[0].split(" "):
|
|
|
|
response, headerData = server.fetch(messageNumber, "(BODY[HEADER.FIELDS (SUBJECT)])")
|
|
|
|
if response!="OK":
|
|
|
|
raise error #TODO: show error message
|
2010-08-21 04:28:18 +02:00
|
|
|
neededMessages.append(("PS", messageNumber))
|
2010-09-04 19:14:08 +02:00
|
|
|
|
2010-09-09 10:27:21 +02:00
|
|
|
print _("ImapFetcher: Found %s messages to fetch") %(len(neededMessages))
|
2010-09-04 19:14:08 +02:00
|
|
|
|
2010-07-07 08:17:18 +02:00
|
|
|
if (len(neededMessages)==0):
|
|
|
|
raise error #TODO: show error message
|
2010-09-04 19:14:08 +02:00
|
|
|
|
2010-09-10 08:07:09 +02:00
|
|
|
email_bodies = []
|
2010-09-04 19:14:08 +02:00
|
|
|
for i, messageData in enumerate(neededMessages, start=1):
|
|
|
|
print "Retrieving message %s" % i
|
2010-07-07 08:17:18 +02:00
|
|
|
response, bodyData = server.fetch(messageData[1], "(UID BODY[TEXT])")
|
|
|
|
bodyData=bodyData[0][1]
|
|
|
|
if response!="OK":
|
|
|
|
raise error #TODO: show error message
|
|
|
|
if messageData[0]=="PS":
|
2010-09-10 08:07:09 +02:00
|
|
|
email_bodies.append(bodyData)
|
2010-07-08 19:46:25 +02:00
|
|
|
#finally:
|
|
|
|
# try:
|
2010-07-13 18:04:53 +02:00
|
|
|
server.close()
|
2010-07-08 19:46:25 +02:00
|
|
|
# finally:
|
|
|
|
# pass
|
2010-07-13 18:04:53 +02:00
|
|
|
server.logout()
|
2010-09-10 08:07:09 +02:00
|
|
|
print _("Completed retrieving IMAP messages, closing server connection")
|
|
|
|
|
|
|
|
errors = 0
|
|
|
|
if len(email_bodies) > 0:
|
2010-09-10 12:09:01 +02:00
|
|
|
errors = importSummaries(db, config, email_bodies, options = None)
|
2010-09-10 08:07:09 +02:00
|
|
|
else:
|
|
|
|
print _("No Tournament summaries found.")
|
|
|
|
|
|
|
|
print _("Errors: %s" % errors)
|
2010-09-04 10:33:16 +02:00
|
|
|
|
2010-09-10 11:18:16 +02:00
|
|
|
def readFile(filename, options):
|
|
|
|
codepage = ["utf8"]
|
|
|
|
whole_file = None
|
|
|
|
if options.hhc == "PokerStars":
|
|
|
|
codepage = PokerStarsSummary.PokerStarsSummary.codepage
|
|
|
|
elif options.hhc == "Full Tilt Poker":
|
|
|
|
codepage = FullTiltPokerSummary.FullTiltPokerSummary.codepage
|
|
|
|
|
|
|
|
for kodec in codepage:
|
|
|
|
#print "trying", kodec
|
|
|
|
try:
|
|
|
|
in_fh = codecs.open(filename, 'r', kodec)
|
|
|
|
whole_file = in_fh.read()
|
|
|
|
in_fh.close()
|
|
|
|
break
|
|
|
|
except:
|
|
|
|
pass
|
2010-09-04 10:33:16 +02:00
|
|
|
|
2010-09-10 11:18:16 +02:00
|
|
|
return whole_file
|
2010-09-04 10:33:16 +02:00
|
|
|
|
2010-09-10 11:18:16 +02:00
|
|
|
def runFake(db, config, options):
|
2010-10-13 08:23:41 +02:00
|
|
|
summaryText = readFile(options.filename, options)
|
2010-09-10 11:18:16 +02:00
|
|
|
importSummaries(db, config,[summaryText], options=options)
|
2010-09-10 08:07:09 +02:00
|
|
|
|
2010-09-10 11:18:16 +02:00
|
|
|
def importSummaries(db, config, summaries, options = None):
|
|
|
|
# TODO: At this point we should have:
|
|
|
|
# - list of strings to process
|
|
|
|
# - The sitename OR specialised TourneySummary object
|
|
|
|
# Using options is pretty ugly
|
2010-09-10 08:07:09 +02:00
|
|
|
errors = 0
|
|
|
|
for summaryText in summaries:
|
2010-09-10 11:18:16 +02:00
|
|
|
# And we should def be using a 'Split' from the site object
|
2010-09-10 12:09:01 +02:00
|
|
|
if options == None or options.hhc == "PokerStars":
|
2010-09-10 11:18:16 +02:00
|
|
|
summaryTexts=(splitPokerStarsSummaries(summaryText))
|
|
|
|
elif options.hhc == "Full Tilt Poker":
|
|
|
|
summaryTexts=(splitFullTiltSummaries(summaryText))
|
|
|
|
|
2010-09-10 08:07:09 +02:00
|
|
|
print "Found %s summaries in email" %(len(summaryTexts))
|
|
|
|
for j, summaryText in enumerate(summaryTexts, start=1):
|
|
|
|
try:
|
2010-09-10 12:09:01 +02:00
|
|
|
if options == None or options.hhc == "PokerStars":
|
2010-09-10 11:18:16 +02:00
|
|
|
PokerStarsSummary.PokerStarsSummary(db=db, config=config, siteName=u"PokerStars", summaryText=summaryText, builtFrom = "IMAP")
|
|
|
|
elif options.hhc == "Full Tilt Poker":
|
|
|
|
FullTiltPokerSummary.FullTiltPokerSummary(db=db, config=config, siteName=u"Fulltilt", summaryText=summaryText, builtFrom = "IMAP")
|
2010-09-10 08:07:09 +02:00
|
|
|
except FpdbParseError, e:
|
|
|
|
errors += 1
|
|
|
|
print _("Finished importing %s/%s PS summaries") %(j, len(summaryTexts))
|
|
|
|
|
|
|
|
return errors
|
2010-09-04 10:33:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
def main(argv=None):
|
|
|
|
if argv is None:
|
|
|
|
argv = sys.argv[1:]
|
|
|
|
|
|
|
|
(options, argv) = Options.fpdb_options()
|
|
|
|
|
|
|
|
if options.usage == True:
|
|
|
|
#Print usage examples and exit
|
|
|
|
print _("USAGE:")
|
|
|
|
sys.exit(0)
|
|
|
|
|
2010-09-10 11:18:16 +02:00
|
|
|
if options.hhc == "PokerStarsToFpdb":
|
|
|
|
print _("Need to define a converter")
|
|
|
|
exit(0)
|
|
|
|
|
2010-09-04 10:33:16 +02:00
|
|
|
# These options should really come from the OptionsParser
|
2010-09-04 14:06:24 +02:00
|
|
|
config = Configuration.Config()
|
2010-09-04 10:33:16 +02:00
|
|
|
db = Database.Database(config)
|
|
|
|
sql = SQL.Sql(db_server = 'sqlite')
|
|
|
|
settings = {}
|
|
|
|
settings.update(config.get_db_parameters())
|
|
|
|
settings.update(config.get_import_parameters())
|
|
|
|
settings.update(config.get_default_paths())
|
|
|
|
db.recreate_tables()
|
|
|
|
|
2010-09-10 11:18:16 +02:00
|
|
|
runFake(db, config, options)
|
2010-09-04 10:33:16 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
sys.exit(main())
|
|
|
|
|