2 # -*- coding: utf-8 -*-
4 #Copyright 2010-2011 Steffen Schaumburg
5 #This program is free software: you can redistribute it and/or modify
6 #it under the terms of the GNU Affero General Public License as published by
7 #the Free Software Foundation, version 3 of the License.
9 #This program is distributed in the hope that it will be useful,
10 #but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 #GNU General Public License for more details.
14 #You should have received a copy of the GNU Affero General Public License
15 #along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #In the "official" distribution you can find the license in agpl-3.0.txt.
18 """This file is to fetch summaries through IMAP and pass them on to the appropriate parser"""
19 #see http://docs.python.org/library/imaplib.html for the python interface
20 #see http://tools.ietf.org/html/rfc2060#section-6.4.4 for IMAP4 search criteria
23 _
= L10n
.get_translation()
25 from imaplib
import IMAP4
, IMAP4_SSL
32 from Exceptions
import FpdbParseError
35 import PokerStarsSummary
36 import FullTiltPokerSummary
39 def splitPokerStarsSummaries(summaryText
): #TODO: this needs to go to PSS.py
40 re_SplitTourneys
= PokerStarsSummary
.PokerStarsSummary
.re_SplitTourneys
41 splitSummaries
= re
.split(re_SplitTourneys
, summaryText
)
43 if len(splitSummaries
) <= 1:
44 print (_("DEBUG:") + " " + _("re_SplitTourneys isn't matching"))
48 def splitFullTiltSummaries(summaryText
):#TODO: this needs to go to FTPS.py
49 re_SplitTourneys
= FullTiltPokerSummary
.FullTiltPokerSummary
.re_SplitTourneys
50 splitSummaries
= re
.split(re_SplitTourneys
, summaryText
)
52 if len(splitSummaries
) <= 1:
53 print(_("DEBUG:") + " " + _("re_SplitTourneys isn't matching"))
58 #print "start of IS.run"
61 #print "useSSL",config.useSsl,"host",config.host
63 server
= IMAP4_SSL(config
.host
)
65 server
= IMAP4(config
.host
)
66 response
= server
.login(config
.username
, config
.password
) #TODO catch authentication error
67 print(_("response to logging in: "), response
)
68 #print "server.list():",server.list() #prints list of folders
70 response
= server
.select(config
.folder
)
71 #print "response to selecting INBOX:",response
73 raise error
#TODO: show error message
76 response
, searchData
= server
.search(None, "SUBJECT", "PokerStars Tournament History Request")
77 for messageNumber
in searchData
[0].split(" "):
78 response
, headerData
= server
.fetch(messageNumber
, "(BODY[HEADER.FIELDS (SUBJECT)])")
80 raise error
#TODO: show error message
81 neededMessages
.append(("PS", messageNumber
))
83 print _("ImapFetcher: Found %s messages to fetch") %(len(neededMessages
))
85 if (len(neededMessages
)==0):
86 raise error
#TODO: show error message
89 for i
, messageData
in enumerate(neededMessages
, start
=1):
90 print "Retrieving message %s" % i
91 response
, bodyData
= server
.fetch(messageData
[1], "(UID BODY[TEXT])")
92 bodyData
=bodyData
[0][1]
94 raise error
#TODO: show error message
95 if messageData
[0]=="PS":
96 email_bodies
.append(bodyData
)
103 print _("Completed retrieving IMAP messages, closing server connection")
106 if len(email_bodies
) > 0:
107 errors
= importSummaries(db
, config
, email_bodies
, options
= None)
109 print _("No Tournament summaries found.")
111 print (_("Errors: %s") % errors
)
113 def readFile(filename
, options
):
116 if options
.hhc
== "PokerStars":
117 codepage
= PokerStarsSummary
.PokerStarsSummary
.codepage
118 elif options
.hhc
== "Full Tilt Poker":
119 codepage
= FullTiltPokerSummary
.FullTiltPokerSummary
.codepage
121 for kodec
in codepage
:
122 #print "trying", kodec
124 in_fh
= codecs
.open(filename
, 'r', kodec
)
125 whole_file
= in_fh
.read()
133 def runFake(db
, config
, options
):
134 summaryText
= readFile(options
.filename
, options
)
135 importSummaries(db
, config
,[summaryText
], options
=options
)
137 def importSummaries(db
, config
, summaries
, options
= None):
138 # TODO: At this point we should have:
139 # - list of strings to process
140 # - The sitename OR specialised TourneySummary object
141 # Using options is pretty ugly
143 for summaryText
in summaries
:
144 # And we should def be using a 'Split' from the site object
145 if options
== None or options
.hhc
== "PokerStars":
146 summaryTexts
=(splitPokerStarsSummaries(summaryText
))
147 elif options
.hhc
== "Full Tilt Poker":
148 summaryTexts
=(splitFullTiltSummaries(summaryText
))
150 print "Found %s summaries in email" %(len(summaryTexts
))
151 for j
, summaryText
in enumerate(summaryTexts
, start
=1):
153 if options
== None or options
.hhc
== "PokerStars":
154 PokerStarsSummary
.PokerStarsSummary(db
=db
, config
=config
, siteName
=u
"PokerStars", summaryText
=summaryText
, builtFrom
= "IMAP")
155 elif options
.hhc
== "Full Tilt Poker":
156 FullTiltPokerSummary
.FullTiltPokerSummary(db
=db
, config
=config
, siteName
=u
"Fulltilt", summaryText
=summaryText
, builtFrom
= "IMAP")
157 except FpdbParseError
, e
:
159 print _("Finished importing %s/%s PS summaries") %(j
, len(summaryTexts
))
168 (options
, argv
) = Options
.fpdb_options()
170 if options
.usage
== True:
171 #Print usage examples and exit
175 if options
.hhc
== "PokerStarsToFpdb":
176 print _("Need to define a converter")
179 # These options should really come from the OptionsParser
180 config
= Configuration
.Config()
181 db
= Database
.Database(config
)
182 sql
= SQL
.Sql(db_server
= 'sqlite')
184 settings
.update(config
.get_db_parameters())
185 settings
.update(config
.get_import_parameters())
186 settings
.update(config
.get_default_paths())
189 runFake(db
, config
, options
)
191 if __name__
== '__main__':