Files
ChronoCCG/Chrono/Build/process_cards.py
T

134 lines
4.0 KiB
Python

"""
One-time script: Download card images from playchrono.com and add imageLink frontmatter.
Usage:
python process_cards.py
Requires a saved HTML copy of https://www.playchrono.com/collections/cards
with the embedded JSON.parse('...') card data.
"""
import re
import json
import os
import urllib.request
import sys
# Adjust these paths for your environment
html_file = r"../playchrono_cards_page.html"
docs_dir = r"../../chrono.docs"
if not os.path.exists(html_file):
# Fallback: search for saved tool output
possible = [f for f in os.listdir(r"C:\Users\jonmc\.local\share\opencode\tool-output")
if f.startswith("tool_") and os.path.isfile(os.path.join(r"C:\Users\jonmc\.local\share\opencode\tool-output", f))]
if possible:
html_file = os.path.join(r"C:\Users\jonmc\.local\share\opencode\tool-output", possible[-1])
with open(html_file, 'r', encoding='utf-8') as f:
html = f.read()
start_marker = "JSON.parse('"
idx = html.find(start_marker)
if idx < 0:
print("ERROR: Could not find JSON.parse in HTML")
sys.exit(1)
start = idx + len(start_marker)
quote_end = html.find("')", start)
if quote_end < 0:
print("ERROR: Could not find closing '")
sys.exit(1)
raw_json_str = html[start:quote_end]
json_str = raw_json_str.encode('utf-8').decode('unicode_escape')
json_str = json_str.replace('\\/', '/')
try:
cards = json.loads(json_str)
except json.JSONDecodeError as e:
print(f"ERROR parsing JSON: {e}")
sys.exit(1)
print(f"Found {len(cards)} cards")
name_to_image = {}
for card in cards:
name = card.get('name', '')
image_url = card.get('image_url', '')
if name and image_url:
name_to_image[name.lower()] = image_url
counterpart = card.get('counterpart')
if counterpart and isinstance(counterpart, dict):
cname = counterpart.get('name', '')
cimage = counterpart.get('image_url', '')
if cname and cimage:
name_to_image[cname.lower()] = cimage
print(f"Built mapping for {len(name_to_image)} card names")
md_files = [f for f in os.listdir(docs_dir) if f.endswith('.md')]
print(f"Found {len(md_files)} markdown files")
processed = 0
downloaded = 0
matched = 0
for md_file in sorted(md_files):
filepath = os.path.join(docs_dir, md_file)
card_name = md_file[:-3]
card_name_lower = card_name.lower()
if card_name_lower not in name_to_image:
continue
matched += 1
image_url = name_to_image[card_name_lower]
png_filename = f"{card_name}.png"
png_filepath = os.path.join(docs_dir, png_filename)
if not os.path.exists(png_filepath):
try:
print(f" DL: {png_filename}")
req = urllib.request.Request(image_url, headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36'
})
with urllib.request.urlopen(req) as response:
with open(png_filepath, 'wb') as out:
out.write(response.read())
downloaded += 1
except Exception as e:
print(f" ERR: {png_filename} - {e}")
continue
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
has_image_link = 'imageLink:' in content
img_ref = f"![[{png_filename}]]"
has_img_ref = img_ref in content
if has_image_link and has_img_ref:
continue
modified = content
if not has_image_link:
modified = modified.rstrip()
if modified.endswith('---'):
modified = modified[:-3] + f'imageLink: "[[{png_filename}]]"\n---'
else:
modified = modified + f'\nimageLink: "[[{png_filename}]]"\n'
if not has_img_ref:
modified = modified.rstrip() + f'\n\n\n{img_ref}\n'
with open(filepath, 'w', encoding='utf-8') as f:
f.write(modified)
processed += 1
print(f"\nDone! Matched: {matched}, Downloaded: {downloaded}, Updated markdown: {processed}")
print(f"Skipped (no card match): {len(md_files) - matched}")