import orjson import pickle import os import requests import shutil # Parse all cards with open("../data/cardinfo.json", "rb") as f: card_data = orjson.loads(f.read()) # Downloaded image cache existing_images = [] if os.path.isfile("dl_cache.pkl"): with open("dl_cache.pkl", "rb") as f: existing_images = pickle.load(f) else: with open("dl_cache.pkl", "wb") as f: pickle.dump(existing_images, f) if not os.path.exists("cards"): os.makedirs("cards") # Download all cards that don't have art FULL_DL_ENDPOINT = "https://images.ygoprodeck.com/images/cards/{}.jpg" SMALL_DL_ENDPOINT = "https://images.ygoprodeck.com/images/cards_small/{}.jpg" for card_json in card_data["data"]: print(f"Downloading {card_json['name']}") images_to_download = [imgs["id"] for imgs in card_json["card_images"]] for image_to_download in images_to_download: if image_to_download in existing_images: print("Skipping!") continue if not os.path.exists(f"cards/{image_to_download}"): os.makedirs(f"cards/{image_to_download}") try: full_img = requests.get(FULL_DL_ENDPOINT.format(image_to_download)).content smol_img = requests.get(SMALL_DL_ENDPOINT.format(image_to_download)).content with open(f"cards/{image_to_download}/full.jpg", "wb") as file: file.write(full_img) with open(f"cards/{image_to_download}/small.jpg", "wb") as file: file.write(smol_img) except Exception as e: shutil.rmtree('cards/{image_to_download}', ignore_errors=True) print(f"ERROR: Failed to download {image_to_download}. {str(e)}") else: existing_images.append(image_to_download) with open("dl_cache.pkl", "wb") as f: pickle.dump(existing_images, f) print(f"Success: {image_to_download}") print(f"Done with {card_json['name']}")