import os
import requests
from time import sleep

# --- Configuration ---
TCGDEX_API = "https://api.tcgdex.net/v2/de/cards"
OUTPUT_FOLDER = "names"
REQUEST_DELAY = 0.1  # seconds between requests to avoid rate limiting

# Create output folder if not exists
os.makedirs(OUTPUT_FOLDER, exist_ok=True)

# Fetch card list from TCGdex
print("Fetching card list...")
resp = requests.get(TCGDEX_API)
if resp.status_code != 200:
    raise Exception(f"Failed to fetch card list: {resp.status_code}")
cards = resp.json()
print(f"Total cards fetched: {len(cards)}")

names = set()  # using a set avoids duplicates automatically

for card in cards:
    card_name = card.get("name")
    if not card_name:
        print("Skipping card with missing name:", card)
        continue
    if "◇" in card_name:
        continue
    names.add(card_name)  # set ignores duplicates

output_path = os.path.join(OUTPUT_FOLDER, "name.txt")
with open(output_path, "w", encoding="utf-8") as f:
    for name in names:
        f.write("'" + name + "',\n")

print(f"Wrote {len(names)} unique names to {output_path}")
