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