From 2fd4280c74ae6c3730a2e95ef6dbdb8be9df522d Mon Sep 17 00:00:00 2001 From: Matthew Kaminski Date: Sun, 10 Dec 2023 03:50:19 -0500 Subject: [PATCH] Add card info and image downloading Only tested on Windows --- .gitignore | 3 +- card_downloader/download_images.py | 54 ++++++++++++++++++++++++++++++ data/get_card_info.bat | 4 +++ 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 card_downloader/download_images.py create mode 100644 data/get_card_info.bat diff --git a/.gitignore b/.gitignore index 4108571..95b78e4 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ static pkg ./Cargo.lock package-lock.json -/data/ +/card_downloader/cards +/card_downloader/dl_cache.pkl diff --git a/card_downloader/download_images.py b/card_downloader/download_images.py new file mode 100644 index 0000000..463ff80 --- /dev/null +++ b/card_downloader/download_images.py @@ -0,0 +1,54 @@ +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']}") diff --git a/data/get_card_info.bat b/data/get_card_info.bat new file mode 100644 index 0000000..79926ab --- /dev/null +++ b/data/get_card_info.bat @@ -0,0 +1,4 @@ +curl.exe --output cardinfo.json --url https://db.ygoprodeck.com/api/v7/cardinfo.php +curl.exe --output cardsets.json --url https://db.ygoprodeck.com/api/v7/cardsets.php +curl.exe --output archetypes.json --url https://db.ygoprodeck.com/api/v7/archetypes.php +wmic os get localdatetime > last_update_time.txt