8 set_ids
= { # NOTE: if there is ever an 'f' in here, update the ponyhead link generation
10 'Canterlot Nights': 'cn',
11 'Rock \'n Rave': 'rr',
12 'Celestial Solstice': 'cs',
16 set_names
= {} # generated
18 def __init__(self
, url
='https://dl.dropboxusercontent.com/u/32733446/cards.json'):
19 logging
.debug('init card database')
21 json_path
= 'cards.json'
23 for name
, id in CardDb
.set_ids
.iteritems():
24 CardDb
.set_names
[id] = name
26 if not os
.path
.isfile(json_path
) or time
.time() - os
.path
.getmtime(json_path
) >= 24 * 60 * 60:
27 self
.download_cards_json(json_path
, url
)
29 self
._db
= self
.parse_cards_json(json_path
)
34 self
._by
_name
[(card
['title'] + (', ' + card
['subtitle'] if card
.get('subtitle') is not None else '')).lower()] = card
35 self
._by
_id
[card
['id'].lower()] = card
36 if card
.has_key('allIds'):
37 for alt_id
in card
['allIds']:
38 self
._by
_id
['%s%s' % (CardDb
.set_ids
[card
['set']], alt_id
.lower())] = card
44 return self
._by
_id
[id.lower()]
46 def by_name(self
, name
):
47 return self
._by
_name
[name
.lower()]
49 def parse_cards_json(self
, json_path
):
50 logging
.debug('parsing %s', json_path
)
52 with
open(json_path
) as f
:
55 logging
.exception('failed')
58 def download_cards_json(self
, json_path
, url
):
59 logging
.debug('downloading %s from %s', json_path
, url
)
61 urllib
.urlretrieve(url
, json_path
)
63 logging
.exception('failed')