Add card info and image downloading

Only tested on Windows
This commit is contained in:
2023-12-10 03:50:19 -05:00
parent 4a3f8f5004
commit 2fd4280c74
3 changed files with 60 additions and 1 deletions

3
.gitignore vendored
View File

@@ -6,4 +6,5 @@ static
pkg pkg
./Cargo.lock ./Cargo.lock
package-lock.json package-lock.json
/data/ /card_downloader/cards
/card_downloader/dl_cache.pkl

View File

@@ -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']}")

4
data/get_card_info.bat Normal file
View File

@@ -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