From c6b6f8a788a3f298d8d45365732e0a429bb9af5e Mon Sep 17 00:00:00 2001 From: steffen123 Date: Thu, 8 Jul 2010 19:46:25 +0200 Subject: [PATCH] many not really separable changes for IMAP import and associated cleaning - deactivated use of TourneySummary (short: TS) from HHC and subclasses - menu entry for IMAP import - converted ImapSummaries (short: IS) to use the config file - IS now optionally supports non-SSL and can run on folders other than INBOX - removed gametypes and getGameTypeAsString from TS method as identical one is in Hand - some other stuff --- pyfpdb/Configuration.py | 17 ++++++++++++++ pyfpdb/Database.py | 18 ++++++++------ pyfpdb/FulltiltToFpdb.py | 2 +- pyfpdb/Hand.py | 1 - pyfpdb/HandHistoryConverter.py | 1 - pyfpdb/ImapSummaries.py | 43 +++++++++++++++++----------------- pyfpdb/PokerStarsSummary.py | 21 ++++++++++------- pyfpdb/SQL.py | 1 - pyfpdb/fpdb.pyw | 10 +++++++- 9 files changed, 72 insertions(+), 42 deletions(-) diff --git a/pyfpdb/Configuration.py b/pyfpdb/Configuration.py index 6a71a750..ac0229f6 100755 --- a/pyfpdb/Configuration.py +++ b/pyfpdb/Configuration.py @@ -434,6 +434,19 @@ class Import: return " interval = %s\n callFpdbHud = %s\n hhArchiveBase = %s\n saveActions = %s\n fastStoreHudCache = %s\n" \ % (self.interval, self.callFpdbHud, self.hhArchiveBase, self.saveActions, self.fastStoreHudCache) +class Email: + def __init__(self, node): + self.node = node + self.host= node.getAttribute("host") + self.username = node.getAttribute("username") + self.password = node.getAttribute("password") + self.useSsl = node.getAttribute("useSsl") + self.folder = node.getAttribute("folder") + + def __str__(self): + return " host = %s\n username = %s\n password = %s\n useSsl = %s\n folder = %s\n" \ + % (self.host, self.username, self.password, self.useSsl, self.folder) + class HudUI: def __init__(self, node): self.node = node @@ -593,6 +606,10 @@ class Config: imp = Import(node = imp_node) self.imp = imp + for email_node in doc.getElementsByTagName("email"): + email = Email(node = email_node) + self.email = email + for hui_node in doc.getElementsByTagName('hud_ui'): hui = HudUI(node = hui_node) self.ui = hui diff --git a/pyfpdb/Database.py b/pyfpdb/Database.py index 7b9262f1..4c4cf27a 100644 --- a/pyfpdb/Database.py +++ b/pyfpdb/Database.py @@ -802,17 +802,17 @@ class Database: #print "session stat_dict =", stat_dict #return stat_dict - def get_player_id(self, config, site, player_name): + def get_player_id(self, config, siteName, playerName): c = self.connection.cursor() - #print "get_player_id: player_name =", player_name, type(player_name) - p_name = Charset.to_utf8(player_name) - c.execute(self.sql.query['get_player_id'], (p_name, site)) + playerNameUtf = Charset.to_utf8(playerName) + print "db.get_player_id siteName",siteName,"playerName",playerName + c.execute(self.sql.query['get_player_id'], (playerNameUtf, siteName)) row = c.fetchone() if row: return row[0] else: return None - + def get_player_names(self, config, site_id=None, like_player_name="%"): """Fetch player names from players. Use site_id and like_player_name if provided""" @@ -1995,10 +1995,14 @@ class Database: return tourneyId #end def createOrUpdateTourney - def createOrUpdateTourneysPlayers(self, hand): + def createOrUpdateTourneysPlayers(self, hand, source=None): tourneysPlayersIds=[] for player in hand.players: - playerId = hand.dbid_pids[player[1]] + print "beginning of for in createOrUpdateTourneysPlayers, player",player,"dbid_pids",hand.dbid_pids + if source=="TourneySummary": #TODO remove this horrible hack + playerId = hand.dbid_pids[player] + else: + playerId = hand.dbid_pids[player[1]] cursor = self.get_cursor() cursor.execute (self.sql.query['getTourneysPlayersId'].replace('%s', self.sql.query['placeholder']), diff --git a/pyfpdb/FulltiltToFpdb.py b/pyfpdb/FulltiltToFpdb.py index 9b287edc..afa7b622 100755 --- a/pyfpdb/FulltiltToFpdb.py +++ b/pyfpdb/FulltiltToFpdb.py @@ -20,7 +20,7 @@ import logging from HandHistoryConverter import * -import TourneySummary +#import TourneySummary # Fulltilt HH Format converter diff --git a/pyfpdb/Hand.py b/pyfpdb/Hand.py index a2cd8b0e..f26258ff 100644 --- a/pyfpdb/Hand.py +++ b/pyfpdb/Hand.py @@ -37,7 +37,6 @@ import Configuration from Exceptions import * import DerivedStats import Card -import Tourney class Hand(object): diff --git a/pyfpdb/HandHistoryConverter.py b/pyfpdb/HandHistoryConverter.py index 7d04e824..3f3d2bd7 100644 --- a/pyfpdb/HandHistoryConverter.py +++ b/pyfpdb/HandHistoryConverter.py @@ -35,7 +35,6 @@ log = logging.getLogger("parser") import Hand -import Tourney from Exceptions import FpdbParseError import Configuration diff --git a/pyfpdb/ImapSummaries.py b/pyfpdb/ImapSummaries.py index 8482478f..844d4b3b 100755 --- a/pyfpdb/ImapSummaries.py +++ b/pyfpdb/ImapSummaries.py @@ -19,8 +19,7 @@ #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 -import sys -from imaplib import IMAP4_SSL +from imaplib import IMAP4, IMAP4_SSL import PokerStarsSummary def splitPokerStarsSummaries(emailText): @@ -30,20 +29,20 @@ def splitPokerStarsSummaries(emailText): return splitSummaries #end def emailText -if __name__ == '__main__': - #TODO: move all these into the config file. until then usage is: ./ImapSummaries.py YourImapHost YourImapUser YourImapPw - configHost=sys.argv[1] - configUser=sys.argv[2] - configPw=sys.argv[3] - #TODO: specify folder, whether to use SSL - - try: - server = IMAP4_SSL(configHost) #TODO: optionally non-SSL - response = server.login(configUser, configPw) #TODO catch authentication error - #print "response to logging in:",response +def run(config, db): + print "start of IS.run" + server=None + #try: + print "in try of IS.run" + if config.email.useSsl: + server = IMAP4_SSL(config.email.host) + else: + server = IMAP4(config.email.host) + response = server.login(config.email.username, config.email.password) #TODO catch authentication error + print "response to logging in:",response #print "server.list():",server.list() #prints list of folders - response = server.select("INBOX") + response = server.select(config.email.folder) #print "response to selecting INBOX:",response if response[0]!="OK": raise error #TODO: show error message @@ -68,15 +67,15 @@ if __name__ == '__main__': if messageData[0]=="PS": summaryTexts=(splitPokerStarsSummaries(bodyData)) for summaryText in summaryTexts: - result=PokerStarsSummary.PokerStarsSummary(db=db, sitename="PokerStars", summaryText=summaryText, builtFrom = "IMAP") - #print "result:",result + result=PokerStarsSummary.PokerStarsSummary(db=db, config=config, siteName=u"PokerStars", summaryText=summaryText, builtFrom = "IMAP") + #print "finished importing a PS summary with result:",result #TODO: count results and output to shell like hand importer does print "completed running Imap import, closing server connection" - finally: - try: - server.close() - finally: - pass - server.logout() + #finally: + # try: + # server.close() + # finally: + # pass + #server.logout() \ No newline at end of file diff --git a/pyfpdb/PokerStarsSummary.py b/pyfpdb/PokerStarsSummary.py index 1f8042af..2000d7f2 100644 --- a/pyfpdb/PokerStarsSummary.py +++ b/pyfpdb/PokerStarsSummary.py @@ -23,16 +23,11 @@ from PokerStarsToFpdb import PokerStars from TourneySummary import * class PokerStarsSummary(TourneySummary): - sitename = "PokerStars" - siteId = 2 - #limits = PokerStars.limits - #games = PokerStars.games - # = PokerStars. - re_TourNo = re.compile("\#[0-9]+,") re_Entries = re.compile("[0-9]+") re_Prizepool = re.compile("\$[0-9]+\.[0-9]+") - re_Player = re.compile("""(?P[0-9]+):\s(?P.*)\s\(.*\),(\s\$(?P[0-9]+\.[0-9]+)\s\()?""") + re_Player = re.compile(u"""(?P[0-9]+):\s(?P.*)\s\(.*\),(\s\$(?P[0-9]+\.[0-9]+)\s\()?""") + re_BuyInFee = re.compile("(?P[0-9]+\.[0-9]+).*(?P[0-9]+\.[0-9]+)") # = re.compile("") def parseSummary(self): @@ -40,7 +35,17 @@ class PokerStarsSummary(TourneySummary): self.tourNo = self.re_TourNo.findall(lines[0])[0][1:-1] #ignore game and limit type as thats not recorded - #ignore lines[1] as buyin/fee are already recorded by HHC + if lines[1].find("$")!=-1: + self.currency="USD" + elif lines[1].find(u"€")!=-1: + self.currency="EUR" + else: + raise fpdbParseError("didn't recognise buyin currency") + + result=self.re_BuyInFee.search(lines[1]) + result=result.groupdict() + self.buyin=int(100*Decimal(result['BUYIN'])) + self.fee=int(100*Decimal(result['FEE'])) self.entries = self.re_Entries.findall(lines[2])[0] diff --git a/pyfpdb/SQL.py b/pyfpdb/SQL.py index 97cc89cc..1c526717 100644 --- a/pyfpdb/SQL.py +++ b/pyfpdb/SQL.py @@ -1944,7 +1944,6 @@ class Sql: self.query['getPlayerIdBySite'] = """SELECT id from Players where name = %s AND siteId = %s""" - # used in Filters: self.query['getSiteId'] = """SELECT id from Sites where name = %s""" self.query['getGames'] = """SELECT DISTINCT category from Gametypes""" diff --git a/pyfpdb/fpdb.pyw b/pyfpdb/fpdb.pyw index fd7cda0f..e5f09c26 100755 --- a/pyfpdb/fpdb.pyw +++ b/pyfpdb/fpdb.pyw @@ -102,6 +102,7 @@ import GuiPrefs import GuiLogView import GuiDatabase import GuiBulkImport +import ImapSummaries import GuiPlayerStats import GuiPositionalStats import GuiTableViewer @@ -655,6 +656,7 @@ class fpdb: + @@ -701,6 +703,7 @@ class fpdb: ('import', None, '_Import'), ('sethharchive', None, '_Set HandHistory Archive Directory', None, 'Set HandHistory Archive Directory', self.select_hhArchiveBase), ('bulkimp', None, '_Bulk Import', 'B', 'Bulk Import', self.tab_bulk_import), + ('imapsummaries', None, '_Import Tourney Summaries through eMail/IMAP', 'I', 'Auto Import and HUD', self.import_imap_summaries), ('autorate', None, 'Auto _Rating (todo)', 'R', 'Auto Rating (todo)', self.not_implemented), ('viewers', None, '_Viewers'), ('autoimp', None, '_Auto Import and HUD', 'A', 'Auto Import and HUD', self.tab_auto_import), @@ -734,6 +737,12 @@ class fpdb: menubar = uimanager.get_widget('/MenuBar') window.add_accel_group(accel_group) return menubar + #end def get_menu + + def import_imap_summaries(self, widget, data=None): + result=ImapSummaries.run(self.config, self.db) + print "import imap summaries result:", result + #end def import_imap_summaries def load_profile(self, create_db = False): """Loads profile from the provided path name.""" @@ -879,7 +888,6 @@ class fpdb: def tab_bulk_import(self, widget, data=None): """opens a tab for bulk importing""" - #print "start of tab_bulk_import" new_import_thread = GuiBulkImport.GuiBulkImport(self.settings, self.config, self.sql) self.threads.append(new_import_thread) bulk_tab=new_import_thread.get_vbox()