-
Notifications
You must be signed in to change notification settings - Fork 0
/
dl_sets.py
71 lines (60 loc) · 2.48 KB
/
dl_sets.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
import json
import requests
import urllib
import multiprocessing
import logging
import threading
import os
from PIL import Image
import ssl
import socket
SetsURL = "https://api.scryfall.com/sets"
SetsFile = "./client/src/assets/data/sets.json"
MTGSets = {}
IconsFolder = "./client/src/assets/img/set_icons/"
opener = urllib.request.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0'), ('Accept', '*/*')]
urllib.request.install_opener(opener)
ssl._create_default_https_context = ssl._create_unverified_context
print("Downloading {}".format(SetsURL))
urllib.request.urlretrieve(SetsURL, SetsFile)
with open(SetsFile, 'r', encoding="utf8") as file:
MTGSets = json.load(file)["data"]
print("Done")
def dl_set_icon(mtgset):
logging.info("Thread %s: starting", mtgset["code"])
for rarity in ["common", "uncommon", "rare", "mythic", "special"]:
icon_url = "https://gatherer.wizards.com/Handlers/Image.ashx?type=symbol&set={}&size=large&rarity={}".format(mtgset["code"],
"T" if rarity == "special" else rarity[0])
icon_path = IconsFolder + mtgset["code"] + "_" + rarity + ".png"
if not os.path.exists(icon_path):
# print("\t{}".format(icon_url))
socket.setdefaulttimeout(5)
try:
urllib.request.urlretrieve(icon_url, icon_path)
if os.path.getsize(icon_path) == 0:
os.remove(icon_path)
else:
try:
#print("\tTriming {}".format(mtgset["code"] + "_" + rarity))
im = Image.open(icon_path)
# im.crop(im.getbbox()).save(icon_path)
except:
print("Error trimming {}".format(icon_path))
except:
print("Error downloading {}".format(icon_url))
logging.info("Thread %s: finishing", mtgset["code"])
threads = list()
for mtgset in MTGSets:
#print(f"Downloading {mtgset['code']} icons...")
x = threading.Thread(target=dl_set_icon, args=(mtgset,))
threads.append(x)
x.start()
for index, thread in enumerate(threads):
thread.join()
SetsWithIcons = set()
for icon in os.listdir(IconsFolder):
set_code = icon[0: icon.index("_")]
SetsWithIcons.add(set_code)
with open("./client/src/assets/data/sets_with_icons.json", 'w', encoding="utf8") as outfile:
json.dump(list(SetsWithIcons), outfile)