steam-backlog-enforcer/_installer_import/steam_installer/resolve.py
Krzysztof kuhy Rudnicki 15f647f639 Install any game from an appid, a URL or a name; adopt Steam's partial downloads
`./run.sh <anything>` is now the entry point. It installs its own dependencies
(steamcmd via yay, DepotDownloader from the SteamRE release, a local .venv) and
works out what the argument is: an appid, a Steam URL, an exact title, or part
of a title. Every route is confirmed through steamcmd's own app_info before
anything acts on it.

Name search moves to steamcommunity.com/actions/SearchApps, which works, with
the storefront API kept as a fallback -- it TLS-rejects scripted clients here,
so search had been effectively dead. DNS on this box fails transiently about one
lookup in three, so each backend is retried before giving up. An exact title now
wins over Steam's own ranking, which puts "Portal 2" above "Portal"; an
ambiguous name shows a picker, or exits 2 rather than guessing.

Two problems found while building this, both now handled:

Anonymous steamcmd has no licence for a paid app and does not say so. It
transfers nothing, prints "Success! App '284160' fully installed.", exits 0, and
writes StateFlags=4 with SizeOnDisk=0 and no depots. A running Steam client then
reads that, decides the app is installed but stale, and downloads the whole game
by itself. `ghost.py` detects that state -- validated read-only against all ten
manifests in a real library, where the nine genuine installs are untouched and
only the fabricated one matches -- and `--repair-ghosts` clears it away. Repair
uses rmdir, never rmtree, so a non-empty directory survives a wrong diagnosis.

And Steam's own in-progress downloads are no longer wasted. `takeover.py`
hardlinks `steamapps/downloading/<appid>/` into the install directory before
depotdownloader runs; depotdownloader hash-checks what is already on disk
(ContentDownloader.cs skips a file when neededChunks is empty) and fetches only
the remainder. Nothing is deleted up front -- the staging tree is cleared only
after the install is verified -- so a failed run still leaves Steam able to
resume. The trees share inodes, so an in-place chunk patch corrects both.

cli.py drops from 311 to 244 lines, back under the repo's 250-line cap, by
moving the install flow to flow.py and the manifest finish to finalize.py.

install.sh had a latent bug: `pip install -e .[dev]` only warns about a missing
extra and exits 0, so the `|| fallback` that installed pytest and coverage never
ran. A fresh venv had no test tools at all; the existing one only worked because
they had been installed by hand earlier. Now named outright and asserted.

246 tests, 100% statement and branch coverage. Verified live: all four input
forms resolve to 284160, an exact "Portal" resolves to 400 and not 620, an
ambiguous name exits 2, ghost detection flags nothing on nine real installs, and
a from-scratch venv bootstraps and runs.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Sgkb3wH6MU9fYquX328ovT
2026-09-11 21:38:16 +02:00

147 lines
5.5 KiB
Python

"""Resolve a game name to a Steam AppID.
Two endpoints, tried in order, because the obvious one does not work here:
1. `steamcommunity.com/actions/SearchApps/<term>` -- an undocumented but stable
community-side search. Verified working from this machine; "BeamNG" returns
exactly `284160 BeamNG.drive`.
2. `store.steampowered.com/api/storesearch/` -- the storefront API. It
TLS-rejects scripted clients from at least some networks (confirmed: even
its bare homepage fails the handshake for plain curl/urllib here, while
every other Steam domain works -- storefront-specific bot detection, not an
outage). Kept as a fallback, never relied on.
`ISteamApps/GetAppList` is deliberately not used: it now answers
`404 Method 'GetAppList' not found in interface 'ISteamApps'`.
Either way this is best-effort. search() can fail, and callers fall back to
asking for a numeric AppID rather than treating a search failure as fatal.
"""
from __future__ import annotations
import json
import time
import urllib.error
import urllib.parse
import urllib.request
from dataclasses import dataclass
COMMUNITY_SEARCH_URL = "https://steamcommunity.com/actions/SearchApps/{term}"
STORE_SEARCH_URL = "https://store.steampowered.com/api/storesearch/?term={term}&cc=us"
# urllib's default identifies itself as Python-urllib, which Steam's edge
# sometimes refuses outright. A plain browser UA gets a normal response.
USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64) steam-game-installer"
DEFAULT_TIMEOUT = 15.0
# DNS here fails transiently often enough to lose roughly one lookup in three.
# A search that dies on a blip sends the user off to find an AppID by hand for
# no reason, so retry briefly before giving up on a backend.
ATTEMPTS = 3
RETRY_DELAY = 1.0
@dataclass
class SearchResult:
appid: int
name: str
class ResolveError(Exception):
pass
class SearchUnavailable(Exception):
"""Neither search endpoint could be reached (network/TLS block, not a code bug)."""
def _fetch_json(url: str, timeout: float, *, attempts: int = ATTEMPTS) -> object:
request = urllib.request.Request(url, headers={"User-Agent": USER_AGENT})
last: Exception = RuntimeError("no attempts made")
for attempt in range(attempts):
try:
with urllib.request.urlopen(request, timeout=timeout) as response:
return json.loads(response.read())
except (urllib.error.URLError, TimeoutError, OSError) as exc:
last = exc
if attempt + 1 < attempts:
# Looked up on the module, not bound as a default, so tests can
# replace it without waiting out the real delay.
time.sleep(RETRY_DELAY)
raise last
def _search_community(term: str, timeout: float) -> list[SearchResult]:
"""Community endpoint. Returns a bare JSON list; appids arrive as strings."""
payload = _fetch_json(COMMUNITY_SEARCH_URL.format(term=term), timeout)
if not isinstance(payload, list):
raise TypeError(f"unexpected community search payload: {type(payload)}")
return [
SearchResult(appid=int(item["appid"]), name=item["name"])
for item in payload
if str(item.get("appid", "")).isdigit()
]
def _search_store(term: str, timeout: float) -> list[SearchResult]:
"""Storefront endpoint. Returns {"items": [{"id": int, "name": str}, ...]}."""
payload = _fetch_json(STORE_SEARCH_URL.format(term=term), timeout)
if not isinstance(payload, dict):
raise TypeError(f"unexpected store search payload: {type(payload)}")
return [
SearchResult(appid=int(item["id"]), name=item["name"])
for item in payload.get("items", [])
]
def search(name: str, *, timeout: float = DEFAULT_TIMEOUT) -> list[SearchResult]:
"""Candidate matches for `name`, newest-working backend first. No auth needed."""
term = urllib.parse.quote(name)
failures: list[str] = []
for label, backend in (
("steamcommunity", _search_community),
("store.steampowered", _search_store),
):
try:
return backend(term, timeout)
except (
urllib.error.URLError,
TimeoutError,
OSError,
TypeError,
ValueError,
KeyError,
) as exc:
failures.append(f"{label}: {exc}")
raise SearchUnavailable("; ".join(failures))
def exact_match(results: list[SearchResult], name: str) -> SearchResult | None:
"""The one result whose title is exactly `name`, case-insensitively.
Steam's own ranking puts "Portal 2" above "Portal", so preferring an exact
title is the difference between typing a full name and getting the sequel.
Ambiguous only in theory: if two apps share a title we refuse to guess.
"""
wanted = name.strip().casefold()
hits = [result for result in results if result.name.strip().casefold() == wanted]
return hits[0] if len(hits) == 1 else None
def prompt_confirm(results: list[SearchResult]) -> SearchResult:
"""Show numbered candidates and require an explicit choice. Never guesses."""
if not results:
raise ResolveError("No matches found on Steam.")
print("Matches found:")
for i, result in enumerate(results, start=1):
print(f" {i}. {result.name} (appid {result.appid})")
while True:
choice = input(f"Which one? [1-{len(results)}, or 0 to cancel]: ").strip()
if choice == "0":
raise ResolveError("Cancelled by user.")
if choice.isdigit() and 1 <= int(choice) <= len(results):
return results[int(choice) - 1]
print("Invalid choice, try again.")