Files
romm/backend/utils/hltb_search.py
Sam Dornan 5f05e4ee7b fix(hltb): send a browser user agent so HLTB's firewall lets requests through (#4717)
HowLongToBeat's firewall now answers any tool-style "Name/version" user
agent, including RomM/<version>, with a 403 HTML page. That failed the
heartbeat (shown as "Connection failed" in Settings) and the /init session
mint, so scans and manual matches silently got no HLTB data.

Send a fixed browser user agent instead. Every HLTB call goes through
base_headers(), so the session stays bound to the same agent.


Claude-Session: https://claude.ai/code/session_01Q4AcvMx1hNfc94xqEeqjzw

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-22 16:42:32 -05:00

91 lines
2.9 KiB
Python

"""The HowLongToBeat search wire contract, shared by the handler and the endpoint
discovery script."""
from typing import Final, NamedTuple
HLTB_BASE_URL: Final[str] = "https://howlongtobeat.com"
# HLTB issues a session at the search route's own /init sibling.
SESSION_MINT_SUFFIX: Final[str] = "/init"
# The session token decodes to "<issued-at>::<public IP>|<user agent>|<key>|<hmac>",
# so logging it would put the host's public IP in any shared log or support bundle.
HLTB_SESSION_HEADERS: Final[frozenset[str]] = frozenset(
{"x-auth-token", "x-hp-key", "x-hp-val"}
)
class HLTBSession(NamedTuple):
token: str
hp_key: str
hp_val: str
def parse_session(data: dict) -> HLTBSession | None:
"""Read a session out of an /init response, or None if it did not issue one."""
token, hp_key, hp_val = (data.get(field) for field in ("token", "hpKey", "hpVal"))
if not (token and hp_key and hp_val):
return None
return HLTBSession(token, hp_key, hp_val)
# HLTB's firewall rejects tool-style "Name/version" agents with a 403, so send a
# browser's. The session is bound to it, so every call has to send the same one.
HLTB_USER_AGENT: Final[str] = (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36"
)
def base_headers(base_url: str) -> dict[str, str]:
return {"Referer": base_url, "User-Agent": HLTB_USER_AGENT}
def search_headers(base_url: str, session: HLTBSession) -> dict[str, str]:
return {
"Content-Type": "application/json",
**base_headers(base_url),
"x-auth-token": session.token,
"x-hp-key": session.hp_key,
"x-hp-val": session.hp_val,
}
def search_body(payload: dict, session: HLTBSession) -> dict:
# Some HLTB endpoints require the key:val in the payload. The key rotates with
# the session, so copy the payload instead of accumulating stale keys.
return {**payload, session.hp_key: session.hp_val}
def build_search_payload(search_term: str, platform_name: str) -> dict:
return {
"searchType": "games",
"searchTerms": search_term.split(" "),
"searchPage": 1,
"size": 20,
"searchOptions": {
"games": {
"userId": 0,
"platform": platform_name,
"sortCategory": "popular",
"rangeCategory": "main",
"rangeTime": {"min": None, "max": None},
"gameplay": {
"perspective": "",
"flow": "",
"genre": "",
"difficulty": "",
},
"rangeYear": {"min": "", "max": ""},
"modifier": "",
},
"users": {"sortCategory": "postcount"},
"lists": {"sortCategory": "follows"},
"filter": "",
"sort": 0,
"randomizer": 0,
},
"useCache": True,
}