mirror of
https://github.com/rommapp/romm.git
synced 2026-11-29 10:01:00 +01:00
* fix(hltb): accept token-only sessions from /init HowLongToBeat's /api/search/site/init now returns only a token and the site's own search sends just x-auth-token. parse_session still required hpKey and hpVal, so every mint read as "no session" and all HLTB lookups (and the endpoint discovery workflow) silently returned nothing. The honeypot pair is now optional and only sent when HLTB issues it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DHm9rRfJsxunF1RckjQqzx * review: share the honeypot check and narrow optional mock calls Move the both-fields honeypot check into HLTBSession.honeypot() so the headers and body cannot drift apart, drop the historical note from its comment, and assert mock call args are not None before reading kwargs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DHm9rRfJsxunF1RckjQqzx * feat(hltb): discover the search endpoint from HLTB when GitHub fails If the endpoint fixture can't be fetched from GitHub, run the same build-manifest discovery the weekly workflow uses against HLTB itself, and fall back to the fixture bundled in the image rather than the stale hard-coded /api/find. The discovery helpers now log instead of printing so their output joins RomM's logs. Search requests also send an Origin header, as the browser does for HLTB's same-origin POST. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DHm9rRfJsxunF1RckjQqzx * fix(hltb): keep a malformed discovery response from aborting scans parse_session and the discovery validator called .get() on whatever JSON HLTB returned, so a non-object body raised AttributeError out of initialize() and aborted the scan. Both now reject non-object JSON, and the handler logs any discovery failure and keeps the bundled endpoint. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DHm9rRfJsxunF1RckjQqzx --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
182 lines
5.8 KiB
Python
182 lines
5.8 KiB
Python
# uv run python -m utils.update_hltb_api_url
|
|
"""
|
|
Utility script to update HowLongToBeat API URL by discovering the dynamic endpoint from the website
|
|
"""
|
|
|
|
import re
|
|
import sys
|
|
import time
|
|
|
|
import httpx
|
|
|
|
from logger.logger import log
|
|
from utils.context import create_httpx_client
|
|
from utils.hltb_search import (
|
|
HLTB_API_URL_FIXTURE,
|
|
HLTB_BASE_URL,
|
|
SESSION_MINT_SUFFIX,
|
|
HLTBSession,
|
|
base_headers,
|
|
build_search_payload,
|
|
parse_session,
|
|
search_body,
|
|
search_headers,
|
|
)
|
|
|
|
# Next.js lists every route it serves as a plain literal in _buildManifest.js.
|
|
BUILD_MANIFEST_REGEX = re.compile(
|
|
r'src=["\'](?P<path>[^"\']*/_next/static/[^"\']+/_buildManifest\.js)["\']'
|
|
)
|
|
API_ROUTE_REGEX = re.compile(r'["\'](?P<route>/api/[^"\']*)["\']')
|
|
|
|
# A term with many well known hits, so an empty result means the route, not the term.
|
|
VALIDATION_SEARCH_TERM = "mario"
|
|
|
|
|
|
def fetch_build_manifest(client: httpx.Client, base_url: str) -> str | None:
|
|
"""Fetch the Next.js build manifest linked from the homepage."""
|
|
headers = base_headers(base_url)
|
|
homepage_url = f"{base_url}/"
|
|
response = client.get(homepage_url, headers=headers, timeout=15)
|
|
response.raise_for_status()
|
|
log.info("Fetched homepage: %s", homepage_url)
|
|
|
|
match = BUILD_MANIFEST_REGEX.search(response.text)
|
|
if not match:
|
|
log.warning("Could not locate the Next.js build manifest")
|
|
return None
|
|
|
|
manifest_url = str(httpx.URL(homepage_url).join(match.group("path")))
|
|
log.info("Located build manifest: %s", manifest_url)
|
|
|
|
response = client.get(manifest_url, headers=headers, timeout=15)
|
|
response.raise_for_status()
|
|
log.info("Downloaded build manifest (%d chars)", len(response.text))
|
|
|
|
return response.text
|
|
|
|
|
|
def candidate_search_routes(manifest: str) -> list[str]:
|
|
"""List the API routes that ship a session mint, most search-like first."""
|
|
routes = list(dict.fromkeys(API_ROUTE_REGEX.findall(manifest)))
|
|
|
|
# serves_game_search() is the real oracle, so this only narrows and orders.
|
|
candidates = [
|
|
route for route in routes if f"{route}{SESSION_MINT_SUFFIX}" in routes
|
|
]
|
|
return sorted(candidates, key=lambda route: "search" not in route)
|
|
|
|
|
|
def _mint_session(
|
|
client: httpx.Client, base_url: str, search_url: str
|
|
) -> HLTBSession | None:
|
|
"""Mint a session at the candidate's /init, or None if it does not issue one."""
|
|
response = client.get(
|
|
f"{search_url}{SESSION_MINT_SUFFIX}",
|
|
params={"t": int(time.time())},
|
|
headers=base_headers(base_url),
|
|
timeout=15,
|
|
)
|
|
response.raise_for_status()
|
|
|
|
return parse_session(response.json())
|
|
|
|
|
|
def _rejection_reason(
|
|
client: httpx.Client, base_url: str, search_url: str
|
|
) -> str | None:
|
|
try:
|
|
session = _mint_session(client, base_url, search_url)
|
|
if not session:
|
|
return "/init issued no session"
|
|
|
|
payload = build_search_payload(VALIDATION_SEARCH_TERM, "")
|
|
response = client.post(
|
|
search_url,
|
|
json=search_body(payload, session),
|
|
headers=search_headers(base_url, session),
|
|
timeout=30,
|
|
)
|
|
response.raise_for_status()
|
|
body = response.json()
|
|
if not isinstance(body, dict):
|
|
return "search did not return a JSON object"
|
|
results = body.get("data")
|
|
except (httpx.RequestError, httpx.HTTPStatusError, ValueError) as e:
|
|
return str(e)
|
|
|
|
# The same shape search_games() reads.
|
|
if not isinstance(results, list) or not results:
|
|
return "search returned no results"
|
|
|
|
if not all(
|
|
isinstance(game, dict) and "game_id" in game and "game_name" in game
|
|
for game in results
|
|
):
|
|
return "results are not HLTB games"
|
|
|
|
return None
|
|
|
|
|
|
def serves_game_search(client: httpx.Client, base_url: str, search_url: str) -> bool:
|
|
"""Check the candidate answers the search HLTBHandler sends, not just /init."""
|
|
reason = _rejection_reason(client, base_url, search_url)
|
|
if reason:
|
|
log.info("Rejected %s: %s", search_url, reason)
|
|
return False
|
|
|
|
log.info("Confirmed %s serves game search", search_url)
|
|
return True
|
|
|
|
|
|
def discover_hltb_endpoint(base_url: str = HLTB_BASE_URL) -> str | None:
|
|
"""Discover the current HLTB search endpoint from the site's route table."""
|
|
base_url = base_url.rstrip("/")
|
|
try:
|
|
with create_httpx_client() as client:
|
|
manifest = fetch_build_manifest(client, base_url)
|
|
if not manifest:
|
|
return None
|
|
|
|
candidates = candidate_search_routes(manifest)
|
|
if not candidates:
|
|
log.warning("No HLTB API route offers a session mint")
|
|
return None
|
|
log.info("Candidate search routes: %s", ", ".join(candidates))
|
|
|
|
for route in candidates:
|
|
search_url = f"{base_url}{route}"
|
|
if serves_game_search(client, base_url, search_url):
|
|
log.info("Resolved HLTB search endpoint: %s", search_url)
|
|
return search_url
|
|
|
|
log.warning("No candidate HLTB route served game search")
|
|
return None
|
|
except (httpx.RequestError, httpx.HTTPStatusError) as e:
|
|
log.warning("Error discovering HLTB endpoint: %s", e)
|
|
return None
|
|
|
|
|
|
def main():
|
|
"""Main function to discover and update the HLTB API URL."""
|
|
print("Starting HLTB API URL discovery...")
|
|
|
|
search_url = discover_hltb_endpoint()
|
|
|
|
if not search_url:
|
|
print("Failed to discover HLTB API URL")
|
|
sys.exit(1)
|
|
|
|
try:
|
|
with open(HLTB_API_URL_FIXTURE, "w") as f:
|
|
f.write(f"{search_url}\n")
|
|
print(f"Successfully updated HLTB API URL to: {search_url}")
|
|
print(f"Written to: {HLTB_API_URL_FIXTURE}")
|
|
except OSError as e:
|
|
print(f"Error writing to fixture file: {e}")
|
|
sys.exit(1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|