import os, re, json, urllib.request, urllib.error API_URL = "https://runeterra.ar/api/cards/get/en_us?game=chrono" HEADERS = { "Content-Type": "application/json", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36" } def normalize(name): return name.lower().replace('"', "").replace("'", "").replace("\u2019", "'").replace("\u2018", "'").strip() CARD_ROOT = "chrono.docs" FACTION_DIRS = [ "Agent/[[Lifeblood]]", "Agent/[[Phasetide]]", "Agent/[[Splintergleam]]", "Agent/[[Starshard]]", "Agent/[[Synthos]]", "Agent/[[Voidmen]]", "Spell/[[Lifeblood]]", "Spell/[[Phasetide]]", "Spell/[[Splintergleam]]", "Spell/[[Starshard]]", "Spell/[[Synthos]]", "Spell/[[Voidmen]]", "Immortalized/[[Lifeblood]]", "Immortalized/[[Phasetide]]", "Immortalized/[[Splintergleam]]", "Immortalized/[[Starshard]]", "Immortalized/[[Synthos]]", "Immortalized/[[Voidmen]]", "Token/[[Lifeblood]]", "Token/[[Phasetide]]", "Token/[[Splintergleam]]", "Token/[[Starshard]]", "Token/[[Synthos]]", "Token/[[Voidmen]]", ] card_files = [] for faction_dir in FACTION_DIRS: full_path = os.path.join(CARD_ROOT, faction_dir) if os.path.isdir(full_path): for f in sorted(os.listdir(full_path)): if f.endswith(".md"): card_files.append(os.path.join(full_path, f)) card_files.sort() print(f"Found {len(card_files)} card files") def fetch_card_data(card_name): search_terms = [card_name] first_word = card_name.split()[0] if card_name.split() else card_name search_terms.append(first_word) # Try searching for each word in the name for word in card_name.split(): if word not in search_terms: search_terms.append(word) # Try searching for quoted form if name contains quotes quoted = re.findall(r'"([^"]*)"', card_name) if quoted: search_terms.append(quoted[0]) # For "Violent", also try "Violet" if "violent" in card_name.lower(): search_terms.append(card_name.lower().replace("violent", "Violet")) search_terms.append("Violet") seen = set() for term in search_terms: if term in seen: continue seen.add(term) body = json.dumps({"search": term}).encode("utf-8") req = urllib.request.Request(API_URL, data=body, headers=HEADERS) try: with urllib.request.urlopen(req, timeout=10) as resp: data = json.loads(resp.read()) cards = data.get("cards", []) if not cards: continue ncard_name = normalize(card_name) for c in cards: if normalize(c.get("name", "")) == ncard_name: return c.get("rarity"), c.get("artistName") for c in cards: for ac in c.get("associatedCards", []): if normalize(ac.get("name", "")) == ncard_name: return ac.get("rarity"), ac.get("artistName") for c in cards: if ncard_name in normalize(c.get("name", "")) and c.get("type") != "Immortalized Agent": return c.get("rarity"), c.get("artistName") for c in cards: for ac in c.get("associatedCards", []): if ncard_name in normalize(ac.get("name", "")): return ac.get("rarity"), ac.get("artistName") except urllib.error.HTTPError as e: err_body = e.read().decode() print(f"[HTTP {e.code}] {err_body[:200]}", end=" ") return None, None return None, None not_found = [] found_count = 0 skipped_count = 0 for i, filepath in enumerate(card_files, 1): filename = os.path.basename(filepath).replace(".md", "") print(f"[{i}/{len(card_files)}] {filename}...", end=" ") with open(filepath, "r", encoding="utf-8") as f: content = f.read() # Check if rarity and artist already exist in frontmatter if "rarity:" in content.split("---")[1].strip() if content.count("---") >= 2 else False: # Extract existing values to print front = content.split("---")[1] r = "" a = "" for line in front.split("\n"): if line.startswith("rarity:"): r = line.split(":", 1)[1].strip() if line.startswith("artist:"): a = line.split(":", 1)[1].strip() print(f"rarity={r}, artist={a} (skipped)") skipped_count += 1 continue rarity, artist = fetch_card_data(filename) if rarity is None or artist is None: print("NOT FOUND") not_found.append(filename) continue print(f"rarity={rarity}, artist={artist}") # Insert after 'category:' line lines = content.split("\n") new_lines = [] inserted = False for line in lines: new_lines.append(line) if line.startswith("category:") and not inserted: new_lines.append(f"rarity: {rarity}") new_lines.append(f"artist: {artist}") inserted = True with open(filepath, "w", encoding="utf-8") as f: f.write("\n".join(new_lines)) found_count += 1 print(f"\nDone. Found+updated: {found_count}, Skipped (already had): {skipped_count}, Not found: {len(not_found)}") if not_found: print("Not found cards:") for n in not_found: print(f" - {n}")