Created
May 8, 2024 11:40
-
-
Save C-Ezra-M/aeec05f22b3068071cd341eddffbfbd2 to your computer and use it in GitHub Desktop.
PTCGO card images from Malie.io
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://malie.io/static | |
import requests as r | |
from pathlib import Path | |
import logging as log | |
import time | |
log.basicConfig(level=log.INFO) | |
#log.basicConfig(level=log.DEBUG) | |
sets = r.get("https://malie.io/static/metamon/SetDataMap.json").json().keys() | |
dest = Path("card_images") | |
dest.mkdir(exist_ok=True) | |
for s in sets: | |
exp_url = f"https://malie.io/static/cheatsheets/en_US/json/{s}.json" | |
cards_resp = r.get(exp_url) | |
try: | |
cards_resp.raise_for_status() | |
log.info("fetched card data %s", exp_url) | |
except r.HTTPError: | |
log.error("unsuccessful attempt to retrieve %s %s", url, f"(HTTP {cards_resp.status_code})") | |
continue | |
pre_parse_time = time.time() | |
cards = cards_resp.json() | |
log.debug("%s", cards) | |
after_parse_time = time.time() | |
log.info("parsed card data JSON successfully. Time elapsed: %s s", after_parse_time - pre_parse_time) | |
for c in cards: | |
log.debug(c) | |
url = c['_lossy_url'] | |
filename = c['_lossy_img'] | |
filepath = Path(dest, filename) | |
if filepath.exists(): | |
log.info("File already exists: %s (SKIP)", filename) | |
continue | |
img_resp = r.get(url) | |
try: | |
img_resp.raise_for_status() | |
except r.HTTPError: | |
log.error("unsuccessful attempt to retrieve %s %s", url, f"(HTTP {img_resp.status_code})") | |
continue | |
img_data = img_resp.content | |
with open(filepath, "wb") as img: | |
img.write(img_data) | |
log.info("saved card %s", url) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment