import subprocess, sys, time, xml.etree.ElementTree as ET\n\nwith open('/tmp/.bggkey') as f:\n    bgg_key = f.read().strip()\n\nHEADER = "Authorization: Bearer *** + bgg_key\nBASE_URL = "https://boardgamegeek.com/xmlapi2/search"\n\ndef search_bgg(query_word):\n    url = BASE_URL + "?query=" + query_word + "&type=boardgame"\n    result = subprocess.run(["curl", "-s", "-H", HEADER, url], capture_output=True, text=True, timeout=30)\n    if result.returncode != 0:\n        return []\n    xml_text = result.stdout.strip()\n    if not xml_text:\n        return []\n    try:\n        root = ET.fromstring(xml_text)\n    except ET.ParseError:\n        return []\n    items = []\n    for item in root.findall("item"):\n        if item.get("type", "") != "boardgame":\n            continue\n        name_el = item.find("name")\n        name_val = name_el.get("value", "") if name_el is not None else ""\n        year_el = item.find("yearpublished")\n        year_val = year_el.get("value", "") if year_el is not None else ""\n        items.append((item.get("id", ""), name_val, year_val))\n    return items\n\ndef find_exact(items, full_name):\n    target = full_name.lower().strip()\n    for item_id, name, year in items:\n        if name.lower().strip() == target:\n            return (item_id, name, year)\n    return None\n\ndef find_containing(items, substring):\n    sub = substring.lower()\n    for item_id, name, year in items:\n        if sub in name.lower():\n            return (item_id, name, year)\n    return None\n\ndef find_best(items, full_name, alt_names=None):\n    m = find_exact(items, full_name)\n    if m:\n        return m\n    if alt_names:\n        for alt in alt_names:\n            m = find_exact(items, alt)\n            if m:\n                return m\n    m = find_containing(items, full_name)\n    if m:\n        return m\n    return None\n\nGAMES = [\n    ("Concordia Special Edition", "Concordia", None, ["Concordia: Special Edition"]),\n    ("Altera", "Altera", None, None),\n    ("Aeon's End: System Overload", "Aeon's", "Overload", ["Aeon's End: System Overload"]),\n    ("Entropy", "Entropy", None, None),\n    ("NARCOS", "Narcos", None, None),\n    ("Stonefall", "Stonefall", None, None),\n    ("Capitals of Tactoria", "Capitals", "Tactoria", None),\n    ("A Life", "Life", None, None),\n    ("The Council of Anderian", "Anderian", "Council", None),\n    ("Ancient Blood", "Ancient", "Blood", ["Ancient Blood: The Order of Vampire Hunters"]),\n    ("Growing Season", "Growing", "Season", None),\n    ("Believe in me!", "Believe", None, ["Believe in Me! (Please)"]),\n    ("Wilderfeast", "Wilderfeast", "wilder", None),\n    ("TATANKA", "Tatanka", None, None),\n]\n\nresults = []\nfor full_name, primary_word, fallback_word, alt_names in GAMES:\n    print("\nSearching: " + full_name + " (word: " + primary_word + ")", file=sys.stderr)\n    items = search_bgg(primary_word)\n    print("  Got " + str(len(items)) + " results", file=sys.stderr)\n    for i, (item_id, name, year) in enumerate(items[:10]):\n        print("    [" + str(i) + "] id=" + item_id + " name=" + repr(name) + " year=" + year, file=sys.stderr)\n    \n    match = find_best(items, full_name, alt_names)\n    \n    if match:\n        item_id, name, year = match\n        url = "https://boardgamegeek.com/boardgame/" + item_id\n        results.append(full_name + "|" + item_id + "|" + url)\n        print("  MATCH: " + item_id + " - " + name + " (" + year + ")", file=sys.stderr)\n    elif fallback_word:\n        time.sleep(3)\n        print("  No match, trying fallback: " + fallback_word, file=sys.stderr)\n        items2 = search_bgg(fallback_word)\n        print("  Got " + str(len(items2)) + " results", file=sys.stderr)\n        for i, (item_id, name, year) in enumerate(items2[:10]):\n            print("    [" + str(i) + "] id=" + item_id + " name=" + repr(name) + " year=" + year, file=sys.stderr)\n        \n        match2 = find_best(items2, full_name, alt_names)\n        if match2:\n            item_id, name, year = match2\n            url = "https://boardgamegeek.com/boardgame/" + item_id\n            results.append(full_name + "|" + item_id + "|" + url)\n            print("  MATCH (fallback): " + item_id + " - " + name + " (" + year + ")", file=sys.stderr)\n        else:\n            results.append(full_name + "|NOT_FOUND")\n            print("  NOT FOUND", file=sys.stderr)\n    else:\n        results.append(full_name + "|NOT_FOUND")\n        print("  NOT FOUND", file=sys.stderr)\n    \n    if full_name != GAMES[-1][0]:\n        time.sleep(3)\n\nprint("\n=== FINAL RESULTS ===", file=sys.stderr)\nfor r in results:\n    print(r)