diff --git a/backend/config/__init__.py b/backend/config/__init__.py index 72f360e4a..e7f0917ea 100644 --- a/backend/config/__init__.py +++ b/backend/config/__init__.py @@ -136,10 +136,10 @@ FLASHPOINT_API_ENABLED: Final[bool] = safe_str_to_bool( # HOWLONGTOBEAT HLTB_API_ENABLED: Final[bool] = safe_str_to_bool(_get_env("HLTB_API_ENABLED")) -# DEMOZOO / POUET (public JSON, no API key; Phase 0 is tag/ID only) +# DEMOZOO / POUET (public JSON, no API key) DEMOZOO_API_ENABLED: Final[bool] = safe_str_to_bool(_get_env("DEMOZOO_API_ENABLED")) POUET_API_ENABLED: Final[bool] = safe_str_to_bool(_get_env("POUET_API_ENABLED")) -# CSDb XML webservice, C64 stills; ID only, no search +# CSDb XML webservice, C64 stills CSDB_API_ENABLED: Final[bool] = safe_str_to_bool(_get_env("CSDB_API_ENABLED")) # UPC LOOKUP (barcode -> title, used when adding physical games by UPC) diff --git a/backend/endpoints/roms/__init__.py b/backend/endpoints/roms/__init__.py index 9c65d625a..62c5c881d 100644 --- a/backend/endpoints/roms/__init__.py +++ b/backend/endpoints/roms/__init__.py @@ -76,6 +76,7 @@ from handler.metadata import ( meta_ra_handler, meta_ss_handler, meta_upc_handler, + scene_id_or_none, ) from handler.metadata.launchbox_handler.media import populate_rom_specific_paths from handler.metadata.ss_handler import add_ss_auth_to_url, get_preferred_media_types @@ -153,35 +154,6 @@ def safe_int_or_none(value: Any) -> int | None: return safe_int(value) -def scene_id_or_none(value: Any, kind: str) -> int | None: - """Accept a bare id or a Demozoo / Pouët / CSDb production URL. - - ``safe_int`` would turn a pasted URL into 0. Empty / unparseable → None. - """ - if value is None or value == "": - return None - text = str(value).strip() - if text.isdigit(): - # isdigit() also accepts superscripts and other digits int() rejects. - try: - return int(text) - except ValueError: - return None - if kind == "demozoo": - from handler.metadata.demozoo_handler import demozoo_id_from_url - - return demozoo_id_from_url(text) - if kind == "pouet": - from handler.metadata.pouet_handler import pouet_id_from_location - - return pouet_id_from_location(text) - if kind == "csdb": - from handler.metadata.csdb_handler import csdb_id_from_url - - return csdb_id_from_url(text) - return None - - def refresh_affected_smart_collections( rom_ids: Sequence[int], membership_only: bool = False ) -> None: diff --git a/backend/handler/metadata/__init__.py b/backend/handler/metadata/__init__.py index e856e3ece..de3e9df9d 100644 --- a/backend/handler/metadata/__init__.py +++ b/backend/handler/metadata/__init__.py @@ -1,5 +1,7 @@ -from .csdb_handler import CsdbHandler -from .demozoo_handler import DemozooHandler +from typing import Any, Callable, Final + +from .csdb_handler import CsdbHandler, csdb_id_from_url +from .demozoo_handler import DemozooHandler, demozoo_id_from_url from .flashpoint_handler import FlashpointHandler from .gamelist_handler import GamelistHandler from .hasheous_handler import HasheousHandler @@ -9,7 +11,7 @@ from .launchbox_handler import LaunchboxHandler from .libretro_handler import LibretroHandler from .moby_handler import MobyGamesHandler from .playmatch_handler import PlaymatchHandler -from .pouet_handler import PouetHandler +from .pouet_handler import PouetHandler, pouet_id_from_location from .ra_handler import RAHandler from .sgdb_handler import SGDBBaseHandler from .ss_handler import SSHandler @@ -33,3 +35,27 @@ meta_demozoo_handler = DemozooHandler() meta_pouet_handler = PouetHandler() meta_csdb_handler = CsdbHandler() meta_upc_handler = UPCHandler() + +_SCENE_ID_PARSERS: Final[dict[str, Callable[[str], int | None]]] = { + "demozoo": demozoo_id_from_url, + "pouet": pouet_id_from_location, + "csdb": csdb_id_from_url, +} + + +def scene_id_or_none(value: Any, kind: str) -> int | None: + """Accept a bare id or a Demozoo / Pouët / CSDb production URL. + + ``safe_int`` would turn a pasted URL into 0. Empty / unparseable → None. + """ + if value is None or value == "": + return None + text = str(value).strip() + if text.isdigit(): + # isdigit() also accepts superscripts and other digits int() rejects. + try: + return int(text) + except ValueError: + return None + parser = _SCENE_ID_PARSERS.get(kind) + return parser(text) if parser else None diff --git a/backend/handler/metadata/csdb_handler.py b/backend/handler/metadata/csdb_handler.py index 22eeb4514..d4cd8c16c 100644 --- a/backend/handler/metadata/csdb_handler.py +++ b/backend/handler/metadata/csdb_handler.py @@ -171,7 +171,7 @@ class CsdbHandler(MetadataHandler): async def _request(self, url: str) -> str: await _rate_limiter.acquire() headers = { - "User-Agent": f"RomM/{get_version()} (+https://github.com/rommapp/romm/issues/1796)", + "User-Agent": f"RomM/{get_version()}", "Accept": "application/xml, text/xml, */*", } try: diff --git a/backend/handler/metadata/demozoo_handler.py b/backend/handler/metadata/demozoo_handler.py index bddee18e1..970ff4abf 100644 --- a/backend/handler/metadata/demozoo_handler.py +++ b/backend/handler/metadata/demozoo_handler.py @@ -1,7 +1,7 @@ -"""Demozoo metadata — Phase 0–1 of rommapp/romm#1796. +"""Demozoo metadata for demoscene productions. -Phase 0: fetch by production ID (filename tag ``(demozoo-N)`` or Edit-ROM id / URL). -Phase 1: title search via ``?title=`` (never ``?search=``) + Jaro–Winkler rank. +Match by production id (filename tag ``(demozoo-N)`` or Edit-ROM id / URL), or by +title search via ``?title=`` (never ``?search=``) ranked with Jaro-Winkler. Public JSON API, no key. """ @@ -510,7 +510,7 @@ class DemozooHandler(MetadataHandler): async def _request(self, url: str) -> dict: await _rate_limiter.acquire() headers = { - "User-Agent": f"RomM/{get_version()} (+https://github.com/rommapp/romm/issues/1796)", + "User-Agent": f"RomM/{get_version()}", "Accept": "application/json", } try: @@ -583,7 +583,7 @@ class DemozooHandler(MetadataHandler): ] async def get_rom(self, fs_name: str, platform_slug: str) -> DemozooRom: - """Tag first (Phase 0); otherwise title search + rank (Phase 1).""" + """Filename tag first; otherwise title search and rank.""" from handler.filesystem import fs_rom_handler if not self.is_enabled(): diff --git a/backend/handler/metadata/pouet_handler.py b/backend/handler/metadata/pouet_handler.py index 85a88c406..6767daa8f 100644 --- a/backend/handler/metadata/pouet_handler.py +++ b/backend/handler/metadata/pouet_handler.py @@ -1,7 +1,7 @@ -"""Pouët metadata — Phase 0–2 of rommapp/romm#1796. +"""Pouët metadata for demoscene productions. -Phase 0: fetch by production ID (filename tag ``(pouet-N)`` or Edit-ROM paste). -Phase 2: unique-title 302 from ``search.php?type=prod`` (Location ``which=N``). +Match by production id (filename tag ``(pouet-N)`` or Edit-ROM paste), or by the +unique-title 302 from ``search.php?type=prod`` (Location ``which=N``). Never parse ``prod.php`` HTML (ambiguous titles stay unmatched). """ @@ -314,7 +314,7 @@ class PouetHandler(MetadataHandler): async def _request(self, url: str) -> dict: await _rate_limiter.acquire() headers = { - "User-Agent": f"RomM/{get_version()} (+https://github.com/rommapp/romm/issues/1796)", + "User-Agent": f"RomM/{get_version()}", "Accept": "application/json", } try: @@ -368,7 +368,7 @@ class PouetHandler(MetadataHandler): httpx_client = ctx_httpx_client.get() url = f"{POUET_SEARCH_URL}?{urlencode({'what': query, 'type': 'prod'})}" headers = { - "User-Agent": f"RomM/{get_version()} (+https://github.com/rommapp/romm/issues/1796)", + "User-Agent": f"RomM/{get_version()}", "Accept": "text/html,application/xhtml+xml", } # Streamed and never read: an ambiguous title answers 200 with HTML we diff --git a/backend/tests/handler/metadata/test_demozoo_handler.py b/backend/tests/handler/metadata/test_demozoo_handler.py index bfa0d86af..8186fdd63 100644 --- a/backend/tests/handler/metadata/test_demozoo_handler.py +++ b/backend/tests/handler/metadata/test_demozoo_handler.py @@ -1,4 +1,4 @@ -"""Tests for Demozoo Phase 0 handler (filename tags + fetch by ID).""" +"""Tests for the Demozoo handler (filename tags + fetch by ID).""" from unittest.mock import AsyncMock, patch diff --git a/backend/tests/handler/metadata/test_pouet_handler.py b/backend/tests/handler/metadata/test_pouet_handler.py index e3001a016..85713f038 100644 --- a/backend/tests/handler/metadata/test_pouet_handler.py +++ b/backend/tests/handler/metadata/test_pouet_handler.py @@ -1,4 +1,4 @@ -"""Tests for Pouët Phase 0 handler (filename tags + fetch by ID).""" +"""Tests for the Pouët handler (filename tags + fetch by ID).""" from unittest.mock import AsyncMock, patch diff --git a/backend/tests/handler/metadata/test_scene_ids.py b/backend/tests/handler/metadata/test_scene_ids.py index bbfb07806..952f13d1c 100644 --- a/backend/tests/handler/metadata/test_scene_ids.py +++ b/backend/tests/handler/metadata/test_scene_ids.py @@ -1,8 +1,8 @@ -"""Edit ROM accepts a bare scene id or a pasted production URL.""" +"""scene_id_or_none accepts a bare scene id or a pasted production URL.""" import pytest -from endpoints.roms import scene_id_or_none +from handler.metadata import scene_id_or_none KINDS = ["demozoo", "pouet", "csdb"]