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
, json_path
='data/cards.json', extra_path
='data/cards_extra.json'):
19 logging
.debug('init card database')
21 for name
, id in CardDb
.set_ids
.iteritems():
22 CardDb
.set_names
[id] = name
24 if not os
.path
.isfile(json_path
) or time
.time() - os
.path
.getmtime(json_path
) >= 24 * 60 * 60:
25 self
.download_cards_json(json_path
)
27 self
._db
= self
.parse_cards_json(json_path
)
32 self
._by
_name
[(card
['title'] + (', ' + card
['subtitle'] if card
.get('subtitle') is not None else '')).lower()] = card
33 self
._by
_id
[card
['id'].lower()] = card
34 if card
.has_key('allIds'):
35 for alt_id
in card
['allIds']:
36 self
._by
_id
['%s%s' % (CardDb
.set_ids
[card
['set']], alt_id
.lower())] = card
38 self
.integrate_cards_extra_json(extra_path
)
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 integrate_cards_extra_json(self
, extra_path
):
59 logging
.debug('parsing %s', extra_path
)
61 with
open(extra_path
) as f
:
63 for card_id
, data
in extra
.items():
65 self
._by
_id
[card_id
.lower()].update(data
)
67 logging
.exception('failed extra data for %s', card_id
)
70 logging
.exception('failed')
72 def download_cards_json(self
, json_path
='data/cards.json', url
='https://dl.dropboxusercontent.com/u/32733446/cards.json'):
73 logging
.debug('downloading %s from %s', json_path
, url
)
75 urllib
.urlretrieve(url
, json_path
)
77 logging
.exception('failed')