From 2046fc11229ead1611369d90111cb30f83825a4a Mon Sep 17 00:00:00 2001 From: steffen123 Date: Wed, 7 Jul 2010 07:06:11 +0200 Subject: [PATCH] the two new files for PS summary mail parsing --- pyfpdb/PokerStarsSummary.py | 70 +++++++++++++++++++++++++++++++++++++ pyfpdb/Summaries.py | 66 ++++++++++++++++++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 pyfpdb/PokerStarsSummary.py create mode 100644 pyfpdb/Summaries.py diff --git a/pyfpdb/PokerStarsSummary.py b/pyfpdb/PokerStarsSummary.py new file mode 100644 index 00000000..bb8bcf05 --- /dev/null +++ b/pyfpdb/PokerStarsSummary.py @@ -0,0 +1,70 @@ +#!/usr/bin/python2 +# -*- coding: utf-8 -*- + +#Copyright 2008-2010 Steffen Schaumburg +#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 . +#In the "official" distribution you can find the license in agpl-3.0.txt. + +"""pokerstars-specific summary parsing code""" + +from TourneySummary import * +from PokerStarsToFpdb import PokerStars + +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_Rank = re.compile("[0-9]+:") + re_Name = re.compile(":.*\(") + re_Winnings = re.compile("\$[0-9]+\.[0-9]+ \(") + # = re.compile("") + + def parseSummary(self): + lines=self.summaryText.splitlines() + + 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 + + self.entries = self.re_Entries.findall(lines[2])[0] + + self.prizepool = self.re_Prizepool.findall(lines[3])[0] + self.prizepool = self.prizepool[1:-3]+self.prizepool[-2:] + + #TODO: lines 4 and 5 are dates, read them + + for i in range(6,len(lines)-2): #lines with rank and winnings info + if lines[i].find(":")==-1: + break + rank=self.re_Rank.findall(lines[i])[0][:-1] + start = lines[i].find(":")+2 + end = lines[i].find("(")-1 + name=lines[i][start:end] + winnings=self.re_Winnings.findall(lines[i]) + if winnings: + winnings=winnings[0][1:-5]+winnings[0][-4:-2] + else: + winnings=0 + + self.addPlayer(rank, name, winnings, "USD", -1, -1, -1) + + #end def parseSummary + +#end class PokerStarsSummary \ No newline at end of file diff --git a/pyfpdb/Summaries.py b/pyfpdb/Summaries.py new file mode 100644 index 00000000..37b3367f --- /dev/null +++ b/pyfpdb/Summaries.py @@ -0,0 +1,66 @@ +#!/usr/bin/python2 +# -*- coding: utf-8 -*- + +#Copyright 2008-2010 Steffen Schaumburg +#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 . +#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 + +import sys +from imaplib import IMAP4_SSL +from Summaries import Summaries +from PokerStarsSummaries import PokerStarsSummaries + +#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] + +server = IMAP4_SSL(configHost) #TODO: optionally non-SSL +response = server.login(configUser, configPw) #TODO catch authentication error +#print "response to logging in:",response +#print "server.list():",server.list() #prints list of folders + +response = server.select("INBOX") +#print "response to selecting INBOX:",response +if response[0]!="OK": + raise error #TODO: show error message + +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)])") + #print "response to fetch subject:",response + if response!="OK": + raise error #TODO: show error message + if headerData[1].find("Subject: PokerStars Tournament History Request - Last x")!=1: + neededMessages.append((messageNumber, "PS") + +tourneys=[] +if len(neededMessages)==0: + raise error #TODO: show error message +for messageData in neededMessages: + response, bodyData = server.fetch(messageData[0], "(UID BODY[TEXT])") + if response!="OK": + raise error #TODO: show error message + if messageData[0]=="PS": + tourneys.append(PokerStarsSummaries.PokerStarsSummaries(bodyData) + +for tourney in tourneys: + print "tourney:",tourney + +server.close() +server.logout()