|
| 1 | +#!/usr/bin/env -S uv run |
| 2 | +# /// script |
| 3 | +# requires-python = ">=3.13" |
| 4 | +# dependencies = [ |
| 5 | +# "requests", |
| 6 | +# ] |
| 7 | +# /// |
| 8 | + |
| 9 | +import requests |
| 10 | +import xml.etree.ElementTree as ET |
| 11 | +import json |
| 12 | +import time |
| 13 | +import logging |
| 14 | +from typing import List, Dict, Any |
| 15 | + |
| 16 | +logging.basicConfig( |
| 17 | + level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s" |
| 18 | +) |
| 19 | +logger = logging.getLogger(__name__) |
| 20 | + |
| 21 | + |
| 22 | +def fetch_boardgame_collection(username: str) -> str: |
| 23 | + url = f"https://boardgamegeek.com/xmlapi2/collection?username={username}&own=1&stats=1&excludesubtype=boardgameexpansion" |
| 24 | + |
| 25 | + max_retries = 5 |
| 26 | + retry_delay = 2 |
| 27 | + |
| 28 | + for attempt in range(max_retries): |
| 29 | + response = requests.get(url) |
| 30 | + |
| 31 | + if response.status_code == 202: |
| 32 | + logger.info( |
| 33 | + f"Collection still processing, retrying in {retry_delay} seconds... (attempt {attempt + 1}/{max_retries})" |
| 34 | + ) |
| 35 | + time.sleep(retry_delay) |
| 36 | + retry_delay *= 2 |
| 37 | + continue |
| 38 | + |
| 39 | + response.raise_for_status() |
| 40 | + return response.text |
| 41 | + |
| 42 | + raise Exception(f"Failed to fetch collection after {max_retries} attempts") |
| 43 | + |
| 44 | + |
| 45 | +def parse_collection_xml(xml_content: str) -> List[Dict[str, Any]]: |
| 46 | + root = ET.fromstring(xml_content) |
| 47 | + games = [] |
| 48 | + |
| 49 | + for item in root.findall("item"): |
| 50 | + game = { |
| 51 | + "objectid": item.get("objectid"), |
| 52 | + "name": None, |
| 53 | + "yearpublished": None, |
| 54 | + "my_rating": None, |
| 55 | + "stats": {}, |
| 56 | + "comment": None, |
| 57 | + } |
| 58 | + |
| 59 | + name_elem = item.find("name") |
| 60 | + if name_elem is not None: |
| 61 | + game["name"] = name_elem.text |
| 62 | + |
| 63 | + year_elem = item.find("yearpublished") |
| 64 | + if year_elem is not None: |
| 65 | + game["yearpublished"] = year_elem.text |
| 66 | + |
| 67 | + thumbnail_elem = item.find("image") |
| 68 | + if thumbnail_elem is not None: |
| 69 | + game["image"] = thumbnail_elem.text |
| 70 | + comment_elem = item.find("comment") |
| 71 | + if comment_elem is not None: |
| 72 | + game["comment"] = comment_elem.text |
| 73 | + |
| 74 | + stats_elem = item.find("stats") |
| 75 | + if stats_elem is not None: |
| 76 | + rating_elem = stats_elem.find("rating") |
| 77 | + if rating_elem is not None: |
| 78 | + game["stats"] = { |
| 79 | + "minplayers": stats_elem.get("minplayers"), |
| 80 | + "maxplayers": stats_elem.get("maxplayers"), |
| 81 | + "playingtime": stats_elem.get("playingtime"), |
| 82 | + } |
| 83 | + if rating_elem.get("value") != "N/A": |
| 84 | + game["my_rating"] = float(rating_elem.get("value")) |
| 85 | + games.append(game) |
| 86 | + games = sorted(games, key=lambda x: x["my_rating"] or 0, reverse=True) |
| 87 | + return games |
| 88 | + |
| 89 | + |
| 90 | +def save_to_json(data: List[Dict[str, Any]], filename: str) -> None: |
| 91 | + with open(filename, "w", encoding="utf-8") as f: |
| 92 | + json.dump(data, f, indent=2, ensure_ascii=False) |
| 93 | + |
| 94 | + |
| 95 | +def main(): |
| 96 | + username = "sinon88" |
| 97 | + output_file = "../static/boardgames_collection.json" |
| 98 | + |
| 99 | + try: |
| 100 | + logger.info(f"Fetching board game collection for user: {username}") |
| 101 | + xml_content = fetch_boardgame_collection(username) |
| 102 | + |
| 103 | + logger.info("Parsing XML content...") |
| 104 | + games_list = parse_collection_xml(xml_content) |
| 105 | + |
| 106 | + logger.info(f"Found {len(games_list)} games in collection") |
| 107 | + |
| 108 | + logger.info(f"Saving to {output_file}...") |
| 109 | + save_to_json(games_list, output_file) |
| 110 | + |
| 111 | + logger.info(f"Successfully saved board game collection to {output_file}") |
| 112 | + |
| 113 | + except requests.exceptions.RequestException as e: |
| 114 | + logger.error(f"Error fetching data: {e}") |
| 115 | + except ET.ParseError as e: |
| 116 | + logger.error(f"Error parsing XML: {e}") |
| 117 | + except Exception as e: |
| 118 | + logger.error(f"Unexpected error: {e}") |
| 119 | + |
| 120 | + |
| 121 | +if __name__ == "__main__": |
| 122 | + main() |
0 commit comments