From 2514b424c8a020fc31ab90a9de1475a58607e7ea Mon Sep 17 00:00:00 2001 From: Worros Date: Fri, 10 Sep 2010 17:18:16 +0800 Subject: [PATCH] Imap/PSSummary: Update and refactor Primarily to make FTP summaries read from file. --- pyfpdb/ImapFetcher.py | 77 ++++++++++++++++++++++++++++--------- pyfpdb/PokerStarsSummary.py | 4 +- 2 files changed, 61 insertions(+), 20 deletions(-) diff --git a/pyfpdb/ImapFetcher.py b/pyfpdb/ImapFetcher.py index 0585ddea..e5317f8b 100755 --- a/pyfpdb/ImapFetcher.py +++ b/pyfpdb/ImapFetcher.py @@ -30,6 +30,7 @@ from Exceptions import FpdbParseError import SQL import Options import PokerStarsSummary +import FullTiltPokerSummary import locale @@ -44,12 +45,23 @@ else: except IOError: def _(string): return string -def splitPokerStarsSummaries(emailText): - splitSummaries=emailText.split("\nPokerStars Tournament #")[1:] - for i in range(len(splitSummaries)): - splitSummaries[i]="PokerStars Tournament #"+splitSummaries[i] +def splitPokerStarsSummaries(summaryText): + re_SplitTourneys = PokerStarsSummary.PokerStarsSummary.re_SplitTourneys + splitSummaries = re.split(re_SplitTourneys, summaryText) + + if len(splitSummaries) <= 1: + print _("DEBUG: re_SplitTourneyss isn't matching") + + return splitSummaries + +def splitFullTiltSummaries(summaryText): + re_SplitTourneys = FullTiltPokerSummary.FullTiltPokerSummary.re_SplitTourneys + splitSummaries = re.split(re_SplitTourneys, summaryText) + + if len(splitSummaries) <= 1: + print _("DEBUG: re_SplitTourneyss isn't matching") + return splitSummaries -#end def emailText def run(config, db): #print "start of IS.run" @@ -107,27 +119,50 @@ def run(config, db): print _("Errors: %s" % errors) -def readFile(filename): - kodec = "utf8" - in_fh = codecs.open(filename, 'r', kodec) - whole_file = in_fh.read() - in_fh.close() +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 + return whole_file +def runFake(db, config, options): + summaryText = readFile(options.infile, options) + importSummaries(db, config,[summaryText], options=options) - -def runFake(db, config, infile): - summaryText = readFile(infile) - importSummaries(db, config,[summaryText]) - -def importSummaries(db, config, summaries): +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 errors = 0 for summaryText in summaries: - summaryTexts=(splitPokerStarsSummaries(summaryText)) + # And we should def be using a 'Split' from the site object + if options.hhc == "PokerStars": + summaryTexts=(splitPokerStarsSummaries(summaryText)) + elif options.hhc == "Full Tilt Poker": + summaryTexts=(splitFullTiltSummaries(summaryText)) + print "Found %s summaries in email" %(len(summaryTexts)) for j, summaryText in enumerate(summaryTexts, start=1): try: - result=PokerStarsSummary.PokerStarsSummary(db=db, config=config, siteName=u"PokerStars", summaryText=summaryText, builtFrom = "IMAP") + if options.hhc == "PokerStars": + 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") except FpdbParseError, e: errors += 1 print _("Finished importing %s/%s PS summaries") %(j, len(summaryTexts)) @@ -146,6 +181,10 @@ def main(argv=None): print _("USAGE:") sys.exit(0) + if options.hhc == "PokerStarsToFpdb": + print _("Need to define a converter") + exit(0) + # These options should really come from the OptionsParser config = Configuration.Config() db = Database.Database(config) @@ -156,7 +195,7 @@ def main(argv=None): settings.update(config.get_default_paths()) db.recreate_tables() - runFake(db, config, options.infile) + runFake(db, config, options) if __name__ == '__main__': sys.exit(main()) diff --git a/pyfpdb/PokerStarsSummary.py b/pyfpdb/PokerStarsSummary.py index 3f5d2fc7..c5f0926c 100644 --- a/pyfpdb/PokerStarsSummary.py +++ b/pyfpdb/PokerStarsSummary.py @@ -57,7 +57,7 @@ class PokerStarsSummary(TourneySummary): 'LS' : "\$|\xe2\x82\xac|" # legal currency symbols - Euro(cp1252, utf-8) } - re_SplitGames = re.compile("^PokerStars") + re_SplitTourneys = re.compile("PokerStars Tournament ") re_TourNo = re.compile("\#(?P[0-9]+),") @@ -81,6 +81,8 @@ class PokerStarsSummary(TourneySummary): re_DateTime = re.compile("\[(?P[0-9]{4})\/(?P[0-9]{2})\/(?P[0-9]{2})[\- ]+(?P[0-9]+):(?P[0-9]+):(?P[0-9]+)") + codepage = ["utf-8"] + def parseSummary(self): m = self.re_TourneyInfo.search(self.summaryText) if m == None: