This repository has been archived by the owner on Jun 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
mtg_json_get.py
90 lines (79 loc) · 2.69 KB
/
mtg_json_get.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
#extracts data from MTG-JSON file
#Dakota Madden-Fong May 2018
import json
#unzip if zipped only
from pathlib import Path
unzipped = Path('AllSets.json')
if not(unzipped.is_file()):
print ('Unzipping zip file containing the json file')
import zipfile
zip_ref = zipfile.ZipFile('AllSets.json.zip', 'r')
zip_ref.extractall()
zip_ref.close()
#read in json file
try:
jsonsets = json.loads(open('AllSets.json',encoding="utf8").read())
except MemoryError:
raise Exception('Please ensure you are running 64 bit Python')
print('json file loaded')
def getSets():
#return alphabetized list of all sets, removing sets for which no card images exist
retsets = []
for set in (list(jsonsets.keys())):
cards = jsonsets[set]['cards']
empties = False
exists = False
multiverse_ids = []
for card in cards:
try:
multiverse_ids.append(card['multiverseId'])
exists = True
except:
multiverse_ids.append(None)
empties = True
if (empties and (not exists)):
pass
elif (empties and exists):
pass
else:
retsets.append(set)
retsets = sorted(retsets)
return retsets
class card_set_json:
# gather info on a specified set
def __init__(self, setcode):
try:
self.cards = jsonsets[setcode]['cards']
except:
raise NameError('No set found with that setcode')
self.names = []
for card in self.cards:
self.names.append(card['name'])
# self.names = list of all card names
self.uids = []
for card in self.cards:
self.uids.append(card['uuid'])
# self.uids = list of all card unique ids
self.multiverse_ids = []
empties = False
exists = False
for card in self.cards:
try:
self.multiverse_ids.append(card['multiverseId'])
exists = True
except:
self.multiverse_ids.append(None)
empties = True
if (empties and (not exists)):
print('No images accessible')
elif (empties and exists):
print('Some images not accessible')
else:
print('All images accessible')
# self.imgurls = list of all URLs to gatherer card images
self.imgurls = []
for id in self.multiverse_ids:
if (id == None):
self.imgurls.append(None)
else:
self.imgurls.append('http://gatherer.wizards.com/Handlers/Image.ashx?multiverseid='+str(id)+'&type=card')