refactor(metadata): move scene_id_or_none into the metadata package

The helper lived in the roms endpoint and reached back into the three scene
handlers through deferred imports. handler.metadata already imports all three
eagerly, so it can own the helper outright and drop the workaround; the branch
chain over `kind` becomes a lookup table. Its test moves beside the other
metadata tests.

Also drop the issue references from the provider docstrings, the config
comments, and the outbound User-Agent headers, which now match the
`RomM/{version}` form the other handlers already send.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Georges-Antoine Assi
2026-08-31 20:59:11 -04:00
parent fe12b42eea
commit afd8b1007d
9 changed files with 47 additions and 49 deletions

View File

@@ -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)

View File

@@ -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:

View File

@@ -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

View File

@@ -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:

View File

@@ -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():

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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"]