From 36649150db90a33fefebd84f140b7d2aceb07665 Mon Sep 17 00:00:00 2001 From: Sam Dornan Date: Fri, 21 Aug 2026 17:40:37 -0500 Subject: [PATCH] chore(steam): tighten comments and the facet-column downgrade Trims the comments and docstrings to the repo's one-to-two-line rule, drops an unreachable `or ""` behind a truthiness guard, and guards the roms_facets column drop with if_exists to match the add in upgrade(). Co-Authored-By: Claude Opus 5 --- backend/adapters/services/steam.py | 17 +++------ .../versions/0108_add_steam_metadata.py | 20 ++++------ backend/handler/metadata/steam_handler.py | 38 ++++++++----------- 3 files changed, 28 insertions(+), 47 deletions(-) diff --git a/backend/adapters/services/steam.py b/backend/adapters/services/steam.py index 7955a3f44..b1431d758 100644 --- a/backend/adapters/services/steam.py +++ b/backend/adapters/services/steam.py @@ -19,9 +19,8 @@ from utils import get_version from utils.context import ctx_aiohttp_session from utils.rate_limiter import RateLimiter -# The storefront publishes no rate limit, but it starts returning 429 at roughly -# 200 requests per 5 minutes per IP. Pace well under that, since a library scan -# spends two requests per game. +# Undocumented, but the storefront starts returning 429 at roughly 200 requests +# per 5 minutes per IP, and a scan spends two requests per game. STEAM_MAX_REQUESTS_PER_SECOND: Final[float] = 0.6 STEAM_MAX_REQUEST_ATTEMPTS: Final[int] = 3 STEAM_RATE_LIMIT_BACKOFF_SECONDS: Final[float] = 5 @@ -31,10 +30,8 @@ _rate_limiter = RateLimiter(STEAM_MAX_REQUESTS_PER_SECOND) class SteamService: """Service to interact with the Steam storefront API. - Valve documents no storefront API. These two endpoints back the store's own - search box and app pages, take no key, and are what ProtonDB and comparable - tools read. They carry no compatibility promise, so every failure path here - degrades to "no result" rather than aborting a scan. + Valve documents neither endpoint and promises no compatibility, so every + failure path degrades to "no result" rather than aborting a scan. """ def __init__( @@ -102,8 +99,7 @@ class SteamService: ) -> list[SteamStoreSearchItem]: """Search the storefront by name. - Results include DLC, soundtracks and tools alongside games, so callers - have to filter by type themselves. + Results include DLC, soundtracks and tools, so callers filter by type. """ url = self.url.joinpath("storesearch").with_query( term=term, cc=country, l=language @@ -120,8 +116,7 @@ class SteamService: ) -> SteamAppDetails | None: """Fetch the store page payload for a single app. - Returns None when Steam has no such app, or when the app is region - locked for the configured country. + Returns None when Steam has no such app, or it is locked for `country`. """ url = self.url.joinpath("appdetails").with_query( appids=str(app_id), cc=country, l=language diff --git a/backend/alembic/versions/0108_add_steam_metadata.py b/backend/alembic/versions/0108_add_steam_metadata.py index 3a5e9caf1..942735e75 100644 --- a/backend/alembic/versions/0108_add_steam_metadata.py +++ b/backend/alembic/versions/0108_add_steam_metadata.py @@ -1,13 +1,8 @@ """Add Steam metadata fields -Adds ``roms.steam_id`` / ``roms.steam_metadata`` for the Steam storefront -provider, and mirrors the id into ``roms_facets`` so the Server Stats -metadata-coverage breakdown counts it alongside the other providers (migration -0103). The mirror is trigger-maintained, so the triggers are recreated with the -wider column set. - -Purely additive: the ``generated_*`` columns on ``roms`` are left alone, so no -table rebuild is needed. +Adds ``roms.steam_id`` / ``roms.steam_metadata`` and mirrors the id into +``roms_facets``. That mirror is trigger-maintained, so the triggers are +recreated over the wider column set. Revision ID: 0108_add_steam_metadata Revises: 0107_roms_dedup_cover_index @@ -30,7 +25,7 @@ depends_on = None # (roms_facets column, roms source column) pairs the triggers keep in sync. -# Mirrors migration 0103's final list; `steam_id` is appended by this revision. +# Must match migration 0103's final list, which this revision appends to. _MIRRORED_COLUMNS_BEFORE = [ ("platform_id", "platform_id"), ("genres", "generated_genres"), @@ -142,9 +137,8 @@ def upgrade() -> None: "idx_roms_steam_id", ["steam_id"], unique=False, if_not_exists=True ) - # Guarded so a re-run after a partial failure skips a column already added. - # MySQL/MariaDB auto-commit each DDL, so a mid-migration crash can leave the - # column behind without advancing the alembic version. + # MySQL/MariaDB auto-commit each DDL, so a crash here can leave the column + # behind without advancing the alembic version. existing = { col["name"] for col in inspect(op.get_bind()).get_columns("roms_facets") } @@ -157,7 +151,7 @@ def upgrade() -> None: def downgrade() -> None: _recreate_triggers(_MIRRORED_COLUMNS_BEFORE) - op.drop_column("roms_facets", "steam_id") + op.drop_column("roms_facets", "steam_id", if_exists=True) with op.batch_alter_table("roms", schema=None) as batch_op: batch_op.drop_index("idx_roms_steam_id", if_exists=True) diff --git a/backend/handler/metadata/steam_handler.py b/backend/handler/metadata/steam_handler.py index 2450169ad..6c4e44182 100644 --- a/backend/handler/metadata/steam_handler.py +++ b/backend/handler/metadata/steam_handler.py @@ -15,19 +15,19 @@ from utils.context import ctx_aiohttp_session from .base_handler import BaseRom, MetadataHandler +# Half-Life 2: never region locked, so a fetch failing means Steam is down. +STEAM_HEARTBEAT_APP_ID: Final[int] = 220 + # Regex to detect Steam app ID tags in filenames like (steam-12345) STEAM_TAG_REGEX = re.compile(r"\(steam-(\d+)\)", re.IGNORECASE) -# Portrait capsule, the only Steam artwork shaped like a RomM cover. It is a -# convention rather than a documented asset, so a miss falls back to the -# landscape header the store payload always carries. +# Undocumented convention, so a miss falls back to the landscape header. STEAM_LIBRARY_CAPSULE_URL = ( "https://shared.akamai.steamstatic.com/store_item_assets/steam/apps/" "{app_id}/library_600x900.jpg" ) -# Steam category IDs mapped onto the vocabulary the other providers already use -# for game modes, so the values stay comparable across sources. +# Steam category IDs mapped onto the game-mode vocabulary the other providers use. STEAM_CATEGORY_GAME_MODES: Final[dict[int, str]] = { 1: "Multiplayer", 2: "Single player", @@ -41,8 +41,8 @@ STEAM_CATEGORY_GAME_MODES: Final[dict[int, str]] = { class SteamMetadata(TypedDict): - # Keys shared with the other providers keep their names and shapes, so the - # facet columns can later read Steam without reshaping stored rows. + # Shared keys keep the other providers' names and shapes, so the facet + # columns can read Steam without reshaping stored rows. total_rating: NotRequired[str] first_release_date: NotRequired[int] genres: NotRequired[list[str]] @@ -65,11 +65,10 @@ class SteamRom(BaseRom): def _parse_release_date(raw_date: str) -> int | None: - """Convert Steam's localized release date into an epoch timestamp. + """Convert Steam's release date into an epoch timestamp, or None if coarse. - With `l=en` the store returns "10 Dec, 2020" or "Dec 10, 2020" depending on - the app, and coarser values ("2020", "Q1 2021") for unreleased ones. Only - the full dates are precise enough to keep. + The store mixes "10 Dec, 2020" and "Dec 10, 2020" across apps, and returns + year-only or quarter values for unreleased ones. """ for date_format in ("%d %b, %Y", "%b %d, %Y", "%d %B, %Y", "%B %d, %Y"): try: @@ -135,8 +134,9 @@ def extract_steam_metadata(details: SteamAppDetails) -> SteamMetadata: if details.get("controller_support"): metadata["controller_support"] = details["controller_support"] - if details.get("website"): - metadata["website"] = details["website"] or "" + website = details.get("website") + if website: + metadata["website"] = website if details.get("is_free") is not None: metadata["is_free"] = bool(details["is_free"]) @@ -189,8 +189,7 @@ class SteamHandler(MetadataHandler): if not self.is_enabled(): return SteamRom(steam_id=None) - # Steam only sells for the three PC platforms, so anything else would - # spend requests to find a title that cannot be there. + # Skip non-PC platforms rather than spend requests that cannot match. if platform_slug not in STEAM_PLATFORMS: return SteamRom(steam_id=None) @@ -279,8 +278,7 @@ class SteamHandler(MetadataHandler): async def _resolve_cover_url(self, app_id: int, details: SteamAppDetails) -> str: """Prefer the portrait capsule, falling back to the landscape header. - The capsule lives on the CDN rather than the storefront, so probing it - costs nothing against the storefront's request budget. + The capsule is on the CDN, so probing it costs no storefront budget. """ capsule_url = STEAM_LIBRARY_CAPSULE_URL.format(app_id=app_id) @@ -299,10 +297,4 @@ class SteamHandler(MetadataHandler): return details.get("header_image", "") -# Half-Life 2, a store page that has existed since 2004 and is not region -# locked, so a successful fetch means the storefront is reachable. -STEAM_HEARTBEAT_APP_ID: Final[int] = 220 - - -# Steam publishes for the three PC platforms only. STEAM_PLATFORMS: Final[frozenset[UPS]] = frozenset({UPS.WIN, UPS.LINUX, UPS.MAC})