-
Notifications
You must be signed in to change notification settings - Fork 1
/
scry.py
476 lines (365 loc) · 13.2 KB
/
scry.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
import math
import os
import re
import requests
import sys
import time
from functools import total_ordering
class Scry:
API = 'https://api.scryfall.com'
def __init__(self, uri):
self.uri = Scry.API + uri if uri.startswith('/') else uri
def __str__(self):
return self.uri
def __repr__(self):
return f'Scry({self.uri!r})'
def get(self, **kwargs):
if self.uri.startswith(Scry.API):
time.sleep(0.1)
response = requests.get(self.uri, params=kwargs)
print(f"GET {response.url}", file=sys.stderr)
response.raise_for_status() # raise if not 200 OK
return response
def post(self, **kwargs):
if self.uri.startswith(Scry.API):
time.sleep(0.1)
response = requests.post(self.uri, json=kwargs)
print(f"POST {response.url}", file=sys.stderr)
response.raise_for_status() # raise if not 200 OK
return response
class Object:
_subclasses = {}
def __init_subclass__(cls, /, object, **kwargs):
super().__init_subclass__(**kwargs)
Object._subclasses[object] = cls
cls.object = object
def __new__(cls, json):
return super().__new__(Object._subclasses[json['object']] if cls is Object else cls)
def __init__(self, json):
if json['object'] != self.object:
raise Exception(f"Creating {type(self)} from json object {json.get('object')!r}")
self._json = json
self.__dict__.update(Object._map(json))
def __str__(self):
return str(self._json)
def __repr__(self):
return f'Object({self._json!r})'
@staticmethod
def get(uri, **kwargs):
return Object(Scry(uri).get(**kwargs).json())
@staticmethod
def post(uri, **kwargs):
return Object(Scry(uri).post(**kwargs).json())
@staticmethod
def _map(json):
if isinstance(json, dict):
return {k: Object._submap(v) for k, v in json.items()}
elif isinstance(json, list):
return [Object._submap(v) for v in json]
return json
@staticmethod
def _submap(json):
return Object(json) if isinstance(json, dict) and 'object' in json else Object._map(json)
def _store(self, name):
uri = getattr(self, name)
basename = os.path.basename(str(uri))
path = f'img/{basename}'
query_pos = path.find('?')
if query_pos >= 0:
path = path[:query_pos]
if not os.path.exists('img'):
os.mkdir('img')
if not os.path.exists(path):
image_data = uri.get().content
with open(path, 'wb') as image_file:
image_file.write(image_data)
return path
class Error(Object, object='error'):
pass
class List(Object, object='list'):
def __iter__(self):
return List.Iter(self)
def __len__(self):
return self.total_cards
class Iter:
def __init__(self, page):
self._page = page
self._iter = iter(page.data)
def __iter__(self):
return self
def __next__(self):
try:
return next(self._iter)
except StopIteration as e:
if not self._page.has_more:
raise e
self._page = self._page.next_page()
self._iter = iter(self._page.data)
return next(self)
class Set(Object, object='set'):
def __init__(self, json):
super().__init__(json)
if 'scryfall_uri' in json:
self.scryfall_uri = Scry(json['scryfall_uri'])
if 'icon_svg_uri' in json:
self.icon_svg_uri = Scry(json['icon_svg_uri'])
if 'search_uri' in json:
self.search_uri = Scry(json['search_uri'])
def icon_svg(self):
return self._store('icon_svg_uri')
@staticmethod
def list(**kwargs):
return Object.get('/sets', **kwargs)
@staticmethod
def from_code(code, **kwargs):
return Object.get(f'/sets/{code}', **kwargs)
@staticmethod
def from_tcgplayer_id(tcgplayer_id, **kwargs):
return Object.get(f'/sets/tcgplayer/{tcgplayer_id}', **kwargs)
@staticmethod
def from_id(id, **kwargs):
return Object.get(f'/sets/{id}', **kwargs)
class Card(Object, object='card'):
def __init__(self, json):
super().__init__(json)
if 'colors' in json:
self.colors = list(map(Color, json['colors']))
if 'mana_cost' in json:
self.mana = Mana(json['mana_cost'])
if 'prints_search_uri' in json:
self.prints_search_uri = Scry(json['prints_search_uri'])
if 'rulings_uri' in json:
self.rulings_uri = Scry(json['rulings_uri'])
if 'scryfall_uri' in json:
self.scryfall_uri = Scry(json['scryfall_uri'])
@property
def cmc_or_inf(self):
return self.cmc if self.front.mana.symbols else math.inf
@property
def frame_rank(self):
# Frame rank represents the general card categorisation within sets:
# 0. Colorless
# 1. White
# 2. Blue
# 3. Black
# 4. Red
# 5. Green
# 6. Multicolored
# 7. Colorless artifact
# 8. Land
if 'Land' in self.front.type_line:
return 8
# Adventures are multi-faced but their faces have no recorded color.
# However, we can't check for layout == 'adventure' because then any
# future heterochromatic Adventure would break. It's not pretty, but
# let's use the front's color if it exists and the card's otherwise.
colors = self.front.colors if hasattr(self.front, 'colors') else self.colors
if len(colors) == 1:
return colors[0].rank + 1
elif len(colors) > 1:
return 6
elif 'Artifact' in self.front.type_line:
return 7
else:
return 0
@property
def front(self):
return self.card_faces[0] if 'card_faces' in self._json and self.layout != 'split' else self
@staticmethod
def search(q, **kwargs):
return Object.get('/cards/search', q=q, **kwargs)
@staticmethod
def named(**kwargs):
return Object.get('/cards/named', **kwargs)
@staticmethod
def autocomplete(q, **kwargs):
return Object.get('/cards/autocomplete', q=q, **kwargs)
@staticmethod
def random(q=None, **kwargs):
return Object.get('/cards/random', q=q, **kwargs)
@staticmethod
def collection(identifiers, **kwargs):
card_limit = 75
this_page = identifiers[:card_limit]
next_page = identifiers[card_limit:]
collection = Object.post('/cards/collection', identifiers=this_page, **kwargs)
# Collections aren't quite like normal lists, as they lack 'has_next'
# and hence 'next_page', so we stitch our own onto the returned object.
if collection.not_found:
raise Exception(f"Not found: {collection.not_found}")
has_more = bool(next_page)
collection.has_more = has_more
if has_more:
collection.next_page = lambda: Card.collection(next_page, **kwargs)
return collection
@staticmethod
def from_collector_number(code, number, lang=None, **kwargs):
uri = f'/cards/{code}/{number}'
if lang:
uri += f'/{lang}'
return Scry(uri).get(kwargs)
@staticmethod
def from_multiverse_id(multiverse_id, **kwargs):
return Object.get(f'/cards/multiverse/{multiverse_id}', **kwargs)
@staticmethod
def from_mtgo_id(mtgo_id, **kwargs):
return Object.get(f'/cards/mtgo/{mtgo_id}', **kwargs)
@staticmethod
def from_arena_id(arena_id, **kwargs):
return Object.get(f'/cards/arena/{arena_id}', **kwargs)
@staticmethod
def from_tcgplayer_id(tcgplayer_id, **kwargs):
return Object.get(f'/cards/tcgplayer/{tcgplayer_id}', **kwargs)
@staticmethod
def from_cardmarket_id(cardmarket_id, **kwargs):
return Object.get(f'/cards/cardmarket/{cardmarket_id}', **kwargs)
@staticmethod
def from_id(id, **kwargs):
return Object.get(f'/cards/{id}', **kwargs)
class CardFace(Object, object='card_face'):
def __init__(self, json):
super().__init__(json)
if 'colors' in json:
self.colors = list(map(Color, json['colors']))
if 'mana_cost' in json:
self.mana = Mana(json['mana_cost'])
class RelatedCard(Object, object='related_card'):
pass
class Ruling(Object, object='ruling'):
@staticmethod
def by_multiverse_id(multiverse_id, **kwargs):
return Object.get(f'/cards/multiverse/{multiverse_id}/rulings', **kwargs)
@staticmethod
def by_mtgo_id(mtgo_id, **kwargs):
return Object.get(f'/cards/mtgo/{mtgo_id}', **kwargs)
@staticmethod
def by_arena_id(arena_id, **kwargs):
return Object.get(f'/cards/arena/{arena_id}', **kwargs)
@staticmethod
def by_collector_number(code, number, **kwargs):
return Object.get(f'/cards/{code}/{number}', **kwargs)
@staticmethod
def by_id(id, **kwargs):
return Object.get(f'/cards/{id}', **kwargs)
class CardSymbol(Object, object='card_symbol'):
def __init__(self, json):
super().__init__(json)
if 'svg_uri' in json:
self.svg_uri = Scry(json['svg_uri'])
def svg(self):
return self._store('svg_uri')
@staticmethod
def list(**kwargs):
return Object.get('/symbology', **kwargs)
@total_ordering
class Color:
def __init__(self, code):
self._code = code
def __str__(self):
return self._code
def __eq__(self, other):
return self._code == other._code
def __lt__(self, other):
return self.rank < other.rank
@property
def rank(self):
return 'WUBRG'.index(self._code)
def __hash__(self):
return hash(self._code)
class Mana:
_pattern = re.compile(r'\{[^}]+\}')
_symbols = None
def __init__(self, cost):
self.cost = cost
if Mana._symbols is None:
Mana._symbols = {symbol.symbol: symbol for symbol in CardSymbol.list() if symbol.represents_mana}
self.symbols = []
self.cmc = 0
colors = set()
for code in Mana._pattern.findall(cost):
symbol = Mana._symbols[code]
self.symbols.append(symbol)
self.cmc += symbol.cmc
colors.update(map(Color, symbol.colors))
self.colors = list(colors)
def __iter__(self):
return iter(self.symbols)
@property
def colorless(self):
return len(self.colors) == 0
@property
def monocolored(self):
return len(self.colors) == 1
@property
def multicolored(self):
return len(self.colors) >= 2
class ManaCost(Object, object='mana_cost'):
@staticmethod
def parse(cost, **kwargs):
return Object.get('/symbology/parse-mana', cost=cost, **kwargs)
class Catalog(Object, object='catalog'):
@staticmethod
def card_names(**kwargs):
return Object.get('/catalog/card-names', **kwargs)
@staticmethod
def artist_names(**kwargs):
return Object.get('/catalog/artist-names', **kwargs)
@staticmethod
def word_bank(**kwargs):
return Object.get('/catalog/word-bank', **kwargs)
@staticmethod
def creature_types(**kwargs):
return Object.get('/catalog/creature-types', **kwargs)
@staticmethod
def planeswalker_types(**kwargs):
return Object.get('/catalog/planeswalker-types', **kwargs)
@staticmethod
def land_types(**kwargs):
return Object.get('/catalog/land-types', **kwargs)
@staticmethod
def artifact_types(**kwargs):
return Object.get('/catalog/artifact-types', **kwargs)
@staticmethod
def enchantment_types(**kwargs):
return Object.get('/catalog/enchantment-types', **kwargs)
@staticmethod
def spell_types(**kwargs):
return Object.get('/catalog/spell-types', **kwargs)
@staticmethod
def powers(**kwargs):
return Object.get('/catalog/powers', **kwargs)
@staticmethod
def toughnesses(**kwargs):
return Object.get('/catalog/toughnesses', **kwargs)
@staticmethod
def loyalties(**kwargs):
return Object.get('/catalog/loyalties', **kwargs)
@staticmethod
def watermarks(**kwargs):
return Object.get('/catalog/watermarks', **kwargs)
@staticmethod
def keyword_abilities(**kwargs):
return Object.get('/catalog/keyword-abilities', **kwargs)
@staticmethod
def keyword_actions(**kwargs):
return Object.get('/catalog/keyword-actions', **kwargs)
@staticmethod
def ability_words(**kwargs):
return Object.get('/catalog/ability-words', **kwargs)
class BulkData(Object, object='bulk_data'):
@staticmethod
def list(**kwargs):
return Object.get('/bulk-data', **kwargs)
@staticmethod
def from_id(id):
return Object.get('f/bulk-data/{id}', **kwargs)
@staticmethod
def from_type(type):
return Object.get('f/bulk-data/{type}', **kwargs)
class Migration(Object, object='migration'):
@staticmethod
def list(**kwargs):
return Object.get('/migrations', **kwargs)
@staticmethod
def from_id(id, **kwargs):
return Object.get(f'/migrations/{id}', **kwargs)