9 'Canterlot Nights': 'cn',
10 'Rock \'n Rave': 'rr',
11 'Celestial Solstice': 'cs',
15 set_names
= {} # generated
17 def __init__(self
, json_path
='cards.json', extra_path
='cards_extra.json'):
18 logging
.debug('init card database')
20 for name
, id in CardDb
.set_ids
.iteritems():
21 CardDb
.set_names
[id] = name
23 if not os
.path
.isfile(json_path
) or time
.time() - os
.path
.getmtime(json_path
) >= 24 * 60 * 60:
24 self
.download_cards_json(json_path
)
26 self
._db
= self
.parse_cards_json(json_path
)
31 self
._by
_name
[(card
['title'] + (', ' + card
['subtitle'] if card
.get('subtitle') is not None else '')).lower()] = card
32 self
._by
_id
[card
['id'].lower()] = card
33 if card
.has_key('allIds'):
34 for alt_id
in card
['allIds']:
35 self
._by
_id
['%s%s' % (CardDb
.set_ids
[card
['set']], alt_id
.lower())] = card
37 self
.integrate_cards_extra_json(extra_path
)
43 return self
._by
_id
[id.lower()]
45 def by_name(self
, name
):
46 return self
._by
_name
[name
.lower()]
48 def parse_cards_json(self
, json_path
):
49 logging
.debug('parsing %s', json_path
)
51 with
open(json_path
) as f
:
54 logging
.exception('failed')
57 def integrate_cards_extra_json(self
, extra_path
):
58 logging
.debug('parsing %s', extra_path
)
60 with
open(extra_path
) as f
:
62 for card_id
, data
in extra
.items():
64 self
._by
_id
[card_id
.lower()].update(data
)
66 logging
.exception('failed extra data for %s', card_id
)
69 logging
.exception('failed')
71 def download_cards_json(self
, json_path
='cards.json', url
='https://dl.dropboxusercontent.com/u/32733446/cards.json'):
72 logging
.debug('downloading %s from %s', json_path
, url
)
74 urllib
.urlretrieve(url
, json_path
)
76 logging
.exception('failed')