Add 'Currencies' filter to the ring player stats viewer.
[fpdb-dooglus.git] / pyfpdb / SummaryEverleaf.py
blob3b80ec0887692ea8c4d10fb78392f962d2a6c627
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 # Copyright (c) 2009-2011 Eric Blade, and the FPDB team.
6 #This program is free software: you can redistribute it and/or modify
7 #it under the terms of the GNU Affero General Public License as published by
8 #the Free Software Foundation, version 3 of the License.
10 #This program is distributed in the hope that it will be useful,
11 #but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 #GNU General Public License for more details.
15 #You should have received a copy of the GNU Affero General Public License
16 #along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #In the "official" distribution you can find the license in agpl-3.0.txt.
19 import urllib, htmllib, formatter
21 class AppURLopener(urllib.FancyURLopener):
22 version = "Free Poker Database/0.12+"
24 urllib._urlopener = AppURLopener()
26 class SummaryParser(htmllib.HTMLParser): # derive new HTML parser
27 def get_attr(self, attrs, key):
28 #print attrs;
29 for a in attrs:
30 if a[0] == key:
31 # print key,"=",a[1]
32 return a[1]
33 return None
35 def __init__(self, formatter) : # class constructor
36 htmllib.HTMLParser.__init__(self, formatter) # base class constructor
37 self.nofill = True
38 self.SiteName = None
39 self.TourneyId = None
40 self.TourneyName = None
41 self.nextStartTime = False
42 self.TourneyStartTime = None
43 self.nextEndTime = False
44 self.TourneyEndTime = None
45 self.TourneyGameType = None
46 self.nextGameType = False
47 self.nextStructure = False
48 self.TourneyStructure = None
49 self.nextBuyIn = False
50 self.TourneyBuyIn = None
51 self.nextPool = False
52 self.TourneyPool = None
53 self.nextPlayers = False
54 self.TourneysPlayers = None
55 self.nextAllowRebuys = False
56 self.TourneyRebuys = None
57 self.parseResultsA = False
58 self.parseResultsB = False
59 self.TempResultStore = [0,0,0,0]
60 self.TempResultPos = 0
61 self.Results = {}
63 def start_meta(self, attrs):
64 x = self.get_attr(attrs, 'name')
65 if x == "author":
66 self.SiteName = self.get_attr(attrs, 'content')
68 def start_input(self, attrs):
69 x = self.get_attr(attrs, 'name')
70 #print "input name=",x
71 if x == "tid":
72 self.TourneyId = self.get_attr(attrs, 'value')
74 def start_h1(self, attrs):
75 if self.TourneyName is None:
76 self.save_bgn()
78 def end_h1(self):
79 if self.TourneyName is None:
80 self.TourneyName = self.save_end()
82 def start_div(self, attrs):
83 x = self.get_attr(attrs, 'id')
84 if x == "result":
85 self.parseResultsA = True
87 def end_div(self): # TODO: Can we get attrs in the END tag too? I don't know? Would be useful to make SURE we're closing the right div ..
88 if self.parseResultsA:
89 self.parseResultsA = False # TODO: Should probably just make sure everything is false at this point, since we're not going to be having anything in the middle of a DIV.. oh well
91 def start_td(self, attrs):
92 self.save_bgn()
94 def end_td(self):
95 x = self.save_end()
97 if not self.parseResultsA:
98 if not self.nextStartTime and x == "Start:":
99 self.nextStartTime = True
100 elif self.nextStartTime:
101 self.TourneyStartTime = x
102 self.nextStartTime = False
104 if not self.nextEndTime and x == "Finished:":
105 self.nextEndTime = True
106 elif self.nextEndTime:
107 self.TourneyEndTime = x
108 self.nextEndTime = False
110 if not self.nextGameType and x == "Game Type:":
111 self.nextGameType = True
112 elif self.nextGameType:
113 self.TourneyGameType = x
114 self.nextGameType = False
116 if not self.nextStructure and x == "Limit:":
117 self.nextStructure = True
118 elif self.nextStructure:
119 self.TourneyStructure = x
120 self.nextStructure = False
122 if not self.nextBuyIn and x == "Buy In / Fee:":
123 self.nextBuyIn = True
124 elif self.nextBuyIn:
125 self.TourneyBuyIn = x # TODO: Further parse the fee from this
126 self.nextBuyIn = False
128 if not self.nextPool and x == "Prize Money:":
129 self.nextPool = True
130 elif self.nextPool:
131 self.TourneyPool = x
132 self.nextPool = False
134 if not self.nextPlayers and x == "Player Count:":
135 self.nextPlayers = True
136 elif self.nextPlayers:
137 self.TourneysPlayers = x
138 self.nextPlayers = False
140 if not self.nextAllowRebuys and x == "Rebuys possible?:":
141 self.nextAllowRebuys = True
142 elif self.nextAllowRebuys:
143 self.TourneyRebuys = x
144 self.nextAllowRebuys = False
146 else: # parse results
147 if x == "Won Prize":
148 self.parseResultsB = True # ok, NOW we can start parsing the results
149 elif self.parseResultsB:
150 if x[0] == "$": # first char of the last of each row is the dollar sign, so now we can put it into a sane order
151 self.TempResultPos = 0
152 name = self.TempResultStore[1]
153 place = self.TempResultStore[0]
154 time = self.TempResultStore[2]
155 # print self.TempResultStore
157 self.Results[name] = {}
158 self.Results[name]['place'] = place
159 self.Results[name]['winamount'] = x
160 self.Results[name]['outtime'] = time
162 # self.Results[self.TempResultStore[1]] = {}
163 # self.Results[self.TempResultStore[1]]['place'] = self.TempResultStore[self.TempResultStore[0]]
164 # self.Results[self.TempResultStore[1]]['winamount'] = x
165 # self.Results[self.TempResultStore[1]]['outtime'] = self.TempResultStore[self.TempResultStore[2]]
166 else:
167 self.TempResultStore[self.TempResultPos] = x
168 self.TempResultPos += 1
170 class EverleafSummary:
171 def __init__(self):
172 if __name__ != "__main__":
173 self.main()
175 def main(self, id="785646"):
176 file = urllib.urlopen("http://www.poker4ever.com/en.tournaments.tournament-statistics?tid="+id)
177 self.parser = SummaryParser(formatter.NullFormatter())
178 self.parser.feed(file.read())
179 print "site=",self.parser.SiteName, "tourneyname=", self.parser.TourneyName, "tourneyid=", self.parser.TourneyId
180 print "start time=",self.parser.TourneyStartTime, "end time=",self.parser.TourneyEndTime
181 print "structure=", self.parser.TourneyStructure, "game type=",self.parser.TourneyGameType
182 print "buy-in=", self.parser.TourneyBuyIn, "rebuys=", self.parser.TourneyRebuys, "total players=", self.parser.TourneysPlayers, "pool=", self.parser.TourneyPool
183 print "results=", self.parser.Results
186 if __name__ == "__main__":
187 me = EverleafSummary()
188 me.main()