Commit f28cc611 authored by Senator John's avatar Senator John 💬

Impelement New Feature

Download last 3 or All artworks.
parent 1a6f9b27
...@@ -27,6 +27,7 @@ MANIFEST_JSON = os.path.join(SCRIPT_DIR, 'manifest.json') ...@@ -27,6 +27,7 @@ MANIFEST_JSON = os.path.join(SCRIPT_DIR, 'manifest.json')
OUTPUT_DIR = os.path.join(SCRIPT_DIR, 'pics') OUTPUT_DIR = os.path.join(SCRIPT_DIR, 'pics')
DEBUG = False # set True for verbose logging DEBUG = False # set True for verbose logging
def export_db(): def export_db():
"""Export cards from .cdb into JSON with fields id, name, desc.""" """Export cards from .cdb into JSON with fields id, name, desc."""
conn = sqlite3.connect(DB_PATH) conn = sqlite3.connect(DB_PATH)
...@@ -39,6 +40,7 @@ def export_db(): ...@@ -39,6 +40,7 @@ def export_db():
json.dump(cards, f, ensure_ascii=False, indent=2) json.dump(cards, f, ensure_ascii=False, indent=2)
print(f'Exported {len(cards)} cards to {TRANSFORMED_JSON}') print(f'Exported {len(cards)} cards to {TRANSFORMED_JSON}')
def dump_ids(): def dump_ids():
"""Dump card IDs from transformed JSON to CSV, preserving existing Official mappings.""" """Dump card IDs from transformed JSON to CSV, preserving existing Official mappings."""
existing = {} existing = {}
...@@ -53,7 +55,6 @@ def dump_ids(): ...@@ -53,7 +55,6 @@ def dump_ids():
existing[pre] = off existing[pre] = off
with open(TRANSFORMED_JSON, 'r', encoding='utf-8') as f: with open(TRANSFORMED_JSON, 'r', encoding='utf-8') as f:
cards = json.load(f) cards = json.load(f)
os.makedirs(os.path.dirname(IDS_CSV) or '.', exist_ok=True) os.makedirs(os.path.dirname(IDS_CSV) or '.', exist_ok=True)
with open(IDS_CSV, 'w', newline='', encoding='utf-8') as csvfile: with open(IDS_CSV, 'w', newline='', encoding='utf-8') as csvfile:
writer = csv.writer(csvfile) writer = csv.writer(csvfile)
...@@ -65,6 +66,7 @@ def dump_ids(): ...@@ -65,6 +66,7 @@ def dump_ids():
writer.writerow([name, pre, off]) writer.writerow([name, pre, off])
print(f'Dumped {len(cards)} IDs to {IDS_CSV} (preserved {len(existing)} mappings)') print(f'Dumped {len(cards)} IDs to {IDS_CSV} (preserved {len(existing)} mappings)')
def load_ids_list(): def load_ids_list():
ids = [] ids = []
with open(IDS_CSV, newline='', encoding='utf-8-sig') as f: with open(IDS_CSV, newline='', encoding='utf-8-sig') as f:
...@@ -72,28 +74,29 @@ def load_ids_list(): ...@@ -72,28 +74,29 @@ def load_ids_list():
for row in reader: for row in reader:
pre = row.get('Pre-Release','').strip() pre = row.get('Pre-Release','').strip()
off = row.get('Official','').strip() off = row.get('Official','').strip()
# prefer Official if present
ids.append(off or pre) ids.append(off or pre)
return ids return ids
def load_manifest(): def load_manifest():
with open(MANIFEST_JSON, encoding='utf-8') as f: with open(MANIFEST_JSON, encoding='utf-8') as f:
data = json.load(f) data = json.load(f)
# unwrap nested if needed
if not any(k.isdigit() for k in data.keys()): if not any(k.isdigit() for k in data.keys()):
for v in data.values(): for v in data.values():
if isinstance(v, dict) and any(k.isdigit() for k in v.keys()): if isinstance(v, dict) and any(k.isdigit() for k in v.keys()):
return v return v
return data return data
def get_artwork_url(entry): def get_artwork_url(entry):
# pick in order bestOCG, bestArt, bestTCG # choose in order bestOCG, bestArt, bestTCG
for key in ('bestOCG','bestArt','bestTCG'): for key in ('bestOCG','bestArt','bestTCG'):
url = entry.get(key) url = entry.get(key)
if url: if url:
return url return url
return None return None
def normalize_url(url): def normalize_url(url):
if url.startswith('//'): if url.startswith('//'):
return 'https:' + url return 'https:' + url
...@@ -101,6 +104,7 @@ def normalize_url(url): ...@@ -101,6 +104,7 @@ def normalize_url(url):
return 'https://' + url.lstrip('/') return 'https://' + url.lstrip('/')
return url if url.startswith(('http://','https://')) else 'https://' + url return url if url.startswith(('http://','https://')) else 'https://' + url
def fetch_artworks(): def fetch_artworks():
""" """
Download artworks, prompting when multiple versions exist. Download artworks, prompting when multiple versions exist.
...@@ -109,7 +113,6 @@ def fetch_artworks(): ...@@ -109,7 +113,6 @@ def fetch_artworks():
all_ids = load_ids_list() all_ids = load_ids_list()
manifest = load_manifest() manifest = load_manifest()
# Build list of IDs present in manifest
valid_ids = [] valid_ids = []
for id_str in all_ids: for id_str in all_ids:
if id_str in manifest: if id_str in manifest:
...@@ -127,37 +130,38 @@ def fetch_artworks(): ...@@ -127,37 +130,38 @@ def fetch_artworks():
if DEBUG: if DEBUG:
print(f"Skipping missing ID {id_str}") print(f"Skipping missing ID {id_str}")
# Process each ID
for id_str in tqdm(valid_ids, desc='Fetching artworks', total=len(valid_ids)): for id_str in tqdm(valid_ids, desc='Fetching artworks', total=len(valid_ids)):
entry = manifest.get(id_str) entry = manifest.get(id_str)
# find numeric versions
versions = sorted([v for v in entry.keys() if v.isdigit()], key=int) versions = sorted([v for v in entry.keys() if v.isdigit()], key=int)
count = len(versions) count = len(versions)
# decide which versions to download
if count <= 1: if count <= 1:
to_dl = ['1'] to_download = ['1']
else: else:
default_n = count if count <= 3 else 3 default_n = count if count <= 3 else 3
resp = input(f"ID {id_str} has {count} artworks. Download last {default_n}? [Y/n]: ").strip().lower() prompt = (
if resp not in ('', 'y', 'yes'): f"ID {id_str} has {count} artworks. "
f"[L]ast {default_n}, [A]ll {count}, or [N]one? ")
resp = input(prompt).strip().lower()
if resp == 'a' or resp == 'all':
to_download = versions
elif resp in ('', 'l', 'last'):
to_download = versions[-default_n:]
else:
if DEBUG: if DEBUG:
print(f"Skipped extra arts for {id_str}") print(f"User skipped ID {id_str}")
continue continue
# pick last default_n versions
to_dl = [str(v) for v in versions[-default_n:]] for ver in to_download:
art = entry.get(ver, {})
# download selected raw = get_artwork_url(art)
for ver in to_dl: if not raw:
art_entry = entry.get(ver, {})
url_raw = get_artwork_url(art_entry)
if not url_raw:
if DEBUG: if DEBUG:
print(f"No URL for {id_str} v{ver}") print(f"No URL for {id_str} v{ver}")
continue continue
url = normalize_url(url_raw) url = normalize_url(raw)
fname = f"{id_str}_{ver}.png" if count > 1 else f"{id_str}.png" fname = f"{id_str}_{ver}.png" if count > 1 else f"{id_str}.png"
outp = os.path.join(OUTPUT_DIR, fname) outp = os.path.join(OUTPUT_DIR, fname)
if os.path.exists(outp): if os.path.exists(outp):
continue continue
try: try:
...@@ -172,6 +176,7 @@ def fetch_artworks(): ...@@ -172,6 +176,7 @@ def fetch_artworks():
print(f"Fetched artwork for {len(valid_ids)} cards to {OUTPUT_DIR}") print(f"Fetched artwork for {len(valid_ids)} cards to {OUTPUT_DIR}")
def main(): def main():
print("Select an option:") print("Select an option:")
print("1: Export from test-release.cdb") print("1: Export from test-release.cdb")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment