forked from RZachLamberty/mtg_data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscgdecks.py
executable file
·314 lines (261 loc) · 8.92 KB
/
scgdecks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Module: scgdecks.py
Author: zlamberty
Created: 2016-02-28
Description:
SCG specific deck implementation
Usage:
<usage>
"""
import datetime
import functools
import json
import logging
import re
import lxml.html
import requests
import tqdm
import common
import decks
# ----------------------------- #
# Module Constants #
# ----------------------------- #
RESULT_REGEX = r'^(?P<finish>\d+)(?:th|rd|st) place at [\w ]+ on (?P<date>\d{1,2}/\d{1,2}/\d{2,4})$'
DECK_URL = 'http://sales.starcitygames.com//deckdatabase/deckshow.php'
SCG_SESSION = None
LOGGER = logging.getLogger(__name__)
# ----------------------------- #
# utility functions #
# ----------------------------- #
def require_scg_session(f):
"""decorator to require an SCG requests session and execute the function
from within a context manager if that is not already the case
"""
global SCG_SESSION
@functools.wraps(f)
def wrapped_f(*args, **kwargs):
global SCG_SESSION
if SCG_SESSION is None:
LOGGER.info("establishing a persistent connection for SCG requests")
with requests.Session() as SCG_SESSION:
return f(*args, **kwargs)
else:
# session must already be established
return f(*args, **kwargs)
return wrapped_f
class ScgDeckParseError(Exception):
pass
# ----------------------------- #
# deck definition #
# ----------------------------- #
class ScgDeck(decks.Deck):
"""This will not be as simple as cards, since decks have a lot of meaningful
metadata (e.g. tournament result, format, date, etc)
This deck type is specific for decks as catalogued on the Star City Games
deck database
"""
@require_scg_session
def __init__(self, url):
"""parse the SCG deck url into a deck object
args:
url: (string) url of the deck
"""
self.url = url
self.root = common.url2html(url, session=SCG_SESSION)
self._author = None
self._authorurl = None
self._name = None
self._event = None
self._eventurl = None
self._resultelem = None
self._resulttext = None
self._resultregmatch = None
self._date = None
self._finish = None
self._mainboard = None
self._sideboard = None
self._format = None
@property
def author(self):
if self._author is None:
try:
self._author = self.root.cssselect('.player_name a')[0].text.lower()
except:
raise ScgDeckParseError("can't find author name")
return self._author
@property
def authorurl(self):
if self._authorurl is None:
try:
self._authorurl = self.root.cssselect('.player_name a')[0].get('href')
except:
raise ScgDeckParseError("can't find author url")
return self._authorurl
@property
def name(self):
if self._name is None:
try:
self._name = self.root.cssselect('.deck_title a')[0].text.lower()
except:
raise ScgDeckParseError("can't find deck name")
return self._name
@property
def resultelem(self):
if self._resultelem is None:
try:
self._resultelem = self.root.cssselect('.deck_played_placed')[0]
except:
raise ScgDeckParseError("can't find result element")
return self._resultelem
@property
def resulttext(self):
if self._resulttext is None:
try:
self._resulttext = self.resultelem.text_content().strip().lower()
except:
raise ScgDeckParseError("can't find result text")
return self._resulttext
@property
def resultregmatch(self):
if self._resultregmatch is None:
try:
self._resultregmatch = re.search(
RESULT_REGEX, self.resulttext
).groupdict()
except:
raise ScgDeckParseError("can't regex parse result text")
return self._resultregmatch
@property
def event(self):
if self._event is None:
try:
self._event = self.resultelem.find('a').text.lower()
except:
raise ScgDeckParseError("can't find event name")
return self._event
@property
def eventurl(self):
if self._eventurl is None:
try:
self._eventurl = self.resultelem.find('a').get('href')
except:
raise ScgDeckParseError("can't find event url")
return self._eventurl
@property
def date(self):
if self._date is None:
try:
self._date = datetime.datetime.strptime(
self.resultregmatch['date'], '%m/%d/%Y'
)
except:
raise ScgDeckParseError("can't find event date")
return self._date
@property
def finish(self):
if self._finish is None:
try:
self._finish = int(self.resultregmatch['finish'])
except:
raise ScgDeckParseError("can't find event finishing place")
return self._finish
@property
def mainboard(self):
if self._mainboard is None:
try:
self._mainboard = {}
for cardrow in self.root.cssselect('.decklist_heading+ ul li'):
qty = int(cardrow.text.lower().strip())
card = cardrow.find('a').text.lower()
self._mainboard[card] = qty
except:
raise ScgDeckParseError("can't find the deck mainboard")
return self._mainboard
@property
def sideboard(self):
if self._sideboard is None:
try:
self._sideboard = {}
for cardrow in self.root.cssselect('.deck_sideboard li'):
qty = int(cardrow.text.lower().strip())
card = cardrow.find('a').text.lower()
self._sideboard[card] = qty
except:
raise ScgDeckParseError("can't find the deck sideboard")
return self._sideboard
@property
def format(self):
if self._format is None:
try:
fmt = self.root.find('.//div[@class="deck_format"]').text.lower()
self._format = fmt
except:
raise ScgDeckParseError("can't find the deck format")
return self._format
def to_dict(self):
return {
'author': self.author,
'authorurl': self.authorurl,
'date': '{:%F}'.format(self.date),
'event': self.event,
'eventurl': self.eventurl,
'finish': self.finish,
'mainboard': [
{'cardname': k, 'qty': v} for (k, v) in self.mainboard.items()
],
'format': self.format,
'name': self.name,
'sideboard': [
{'cardname': k, 'qty': v} for (k, v) in self.sideboard.items()
],
'url': self.url,
}
def to_json(self):
return json.dumps(self.to_dict())
class ScgParseError(Exception):
def __init__(self, msg):
LOGGER.error(msg)
super().__init__()
@require_scg_session
def scg_decks(includeTest=False):
return decks._get_decks(
deckurls=scg_decklist_urls(includeTest=includeTest),
decktype=ScgDeck
)
@require_scg_session
def scg_decklist_urls(include_test=False):
"""generator of urls for decklists on SCG's deck database
args:
include_test: (bool) whether or not to include test decks (there are
many). this is not currently implemented, fwiw (default: False)
yields:
urls of decks played in competitions
raises:
None
"""
LOGGER.info("collecting urls for scg decklists")
for urlblock in tqdm.tqdm(scg_url_blocks()):
resp = SCG_SESSION.get(urlblock)
root = lxml.html.fromstring(resp.content)
decklinks = root.cssselect('#content strong')
eventtypes = root.cssselect(
'.deckdbbody2:nth-child(4) , .deckdbbody:nth-child(4)'
)
if len(decklinks) != len(eventtypes):
err = "Unequal numbers of decks and decktypes on block url {}"
err = err.format(urlblock)
raise ScgParseError(err)
for (decklink, eventtype) in zip(decklinks, eventtypes):
if eventtype.text != 'Test deck':
yield decklink.getparent().get('href')
@require_scg_session
def scg_url_blocks():
resp = SCG_SESSION.get(url=DECK_URL, params={'limit': 100})
root = lxml.html.fromstring(resp.content)
return [
a.attrib['href'].replace('&limit=limit', '')
for a in root.cssselect('tr:nth-child(106) a')
if 'Next' not in a.text
]