-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
73 lines (62 loc) · 2.3 KB
/
main.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
import json
import os
import sys
import urllib.request
from urllib.error import HTTPError
from urllib.parse import quote
from consts import ASSETS_DIRPATH, IMAGE_FMT_URL, INFO_FILEPATH, INFO_URL
from PIL import Image
# Toggle this if we want to cleanup whatever we have and start afresh
DO_CLEANUP = False
if __name__ == "__main__":
if DO_CLEANUP:
# Remove everything in the assets directory
for file_name in os.listdir(ASSETS_DIRPATH):
os.remove(os.path.join(ASSETS_DIRPATH, file_name))
stamps_data = None
did_anything_change = False
try:
with urllib.request.urlopen(
urllib.request.Request(
INFO_URL,
headers={"User-Agent": "Python3"},
)
) as fr:
stamps_data = json.load(fr)
except HTTPError as e:
sys.exit("error while fetching JSON data: {}".format(e))
# For each of the stamps, convert, resize and then save them
for stamp_info in stamps_data["items"]:
stamp_slug: str = stamp_info["slug"]
if stamp_slug == "":
continue
stamp_file_path = os.path.join(
ASSETS_DIRPATH,
"{}.webp".format(stamp_slug),
)
if os.path.exists(stamp_file_path):
# If a file already exists, we don't need to download it again
continue
# Atleast one new file has been detected. This signifies we have some
# new information at hand
did_anything_change = True
try:
with urllib.request.urlopen(IMAGE_FMT_URL.format(quote(stamp_slug))) as fr:
img = Image.open(fr)
img.resize(
size=(180, 180),
resample=Image.LANCZOS,
reducing_gap=3.0,
).save(stamp_file_path, format="webp", quality=100)
except HTTPError as e:
sys.exit("error fetching image for '{}': {}".format(stamp_slug, e))
except ConnectionRefusedError as e:
sys.exit("error fetching image for '{}': {}".format(stamp_slug, e))
if did_anything_change:
with open(INFO_FILEPATH, "w") as fw:
json.dump(stamps_data, fw)
print(
"Script completed successfuly. Changes detected status : {}".format(
did_anything_change
)
)