# 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)