""" Download classification, price and genres for every game in data/. - Game ids come from the file names in data/ (e.g. data/730.csv -> app id 730). - A row is complete when it has price AND genres. Incomplete or missing rows are (re)fetched, so the script can be stopped and re-run, and old files without a genres column are upgraded. - Writes game_tags.csv: sheet, group, name, categories, price, currency, genres group = sp / mp / both / unknown (from Steam store "categories") price = base price before discount, 0 if free, "NA" if no price listed genres = Steam store genres joined by ";" ("none" if the store lists none) Install: pip install requests """ import csv import glob import os import time import requests DATA_GLOB = "data/*.csv" OUT = "game_tags.csv" API = "https://store.steampowered.com/api/appdetails" CC = "us" DELAY = 1.6 SAVE_EVERY = 10 FIELDS = ["sheet", "group", "name", "categories", "price", "currency", "genres"] MP_CATS = {"multi-player", "co-op", "online co-op", "pvp", "online pvp", "mmo", "cross-platform multiplayer"} SP_CATS = {"single-player"} def ids_from_files(): ids = [] for path in sorted(glob.glob(DATA_GLOB)): stem = os.path.splitext(os.path.basename(path))[0] if stem.isdigit(): ids.append(stem) else: print(f"skipping {path}: file name is not a numeric app id") return ids def load_rows(): """Existing rows keyed by sheet; columns missing in an old file load as blanks.""" if not os.path.exists(OUT): return {} with open(OUT, newline="", encoding="utf-8") as f: return {row["sheet"]: {k: row.get(k) or "" for k in FIELDS} for row in csv.DictReader(f)} def save_rows(rows): tmp = OUT + ".tmp" with open(tmp, "w", newline="", encoding="utf-8") as f: writer = csv.DictWriter(f, fieldnames=FIELDS) writer.writeheader() writer.writerows(rows.values()) os.replace(tmp, OUT) def fetch(appid): """Store data dict, {} if Steam has no store entry, None on a temporary failure.""" for attempt in range(5): try: r = requests.get(API, params={"appids": appid, "l": "english", "cc": CC}, timeout=20) if r.status_code == 429: time.sleep(60 * (attempt + 1)) continue r.raise_for_status() entry = r.json()[appid] return entry["data"] if entry.get("success") else {} except (requests.RequestException, ValueError, KeyError, TypeError): time.sleep(5 * (attempt + 1)) return None def classify(categories): cats = {c.lower() for c in categories} mp, sp = bool(cats & MP_CATS), bool(cats & SP_CATS) if mp and sp: return "both" if mp: return "mp" if sp: return "sp" return "unknown" def get_price(data): if data.get("is_free"): return 0, "" po = data.get("price_overview") if po: return round(po["initial"] / 100, 2), po.get("currency", "") return "NA", "" def needs_fetch(rows, appid): r = rows.get(appid) return r is None or r["price"] == "" or r["genres"] == "" def main(): ids = ids_from_files() rows = load_rows() todo = [i for i in ids if needs_fetch(rows, i)] print(f"{len(ids)} games in data/, {len(ids) - len(todo)} complete, {len(todo)} to fetch " f"(about {len(todo) * DELAY / 60:.0f} min)") try: for n, appid in enumerate(todo, 1): data = fetch(appid) if data is None: print(f"[{n}/{len(todo)}] {appid}: request failed, will retry on next run") continue cats = [c["description"] for c in data.get("categories", [])] genres = [g["description"] for g in data.get("genres", [])] price, currency = get_price(data) rows[appid] = {"sheet": appid, "group": classify(cats), "name": data.get("name", ""), "categories": ";".join(cats), "price": price, "currency": currency, "genres": ";".join(genres) or "none"} print(f"[{n}/{len(todo)}] {appid} {data.get('name', '?')}: {rows[appid]['group']}, " f"{price} {currency}, {rows[appid]['genres']}") if n % SAVE_EVERY == 0: save_rows(rows) time.sleep(DELAY) finally: save_rows(rows) if __name__ == "__main__": main()