mirror of
https://github.com/Abdess/retroarch_system.git
synced 2026-11-26 19:38:52 -06:00
refactor: one network layer for the scrapers
Each network job had several copies. One is kept per job and every caller uses it. - Source revision: model.sourcerevision (source_revision, required_revision, platform_revision) replaces BaseTargetScraper's pattern list, the exporter's _REVISION, the four launcher_revision copies and RetroBat's own YAML reader. It reads the union of the shapes: GitHub raw, blob and tree with or without a path after the revision, GitLab raw, blob, tree and archive, codeload tar.gz and zip. The target scrapers now answer for RetroDECK, NextUI, MinUI and MyMinUI, whose tree URLs end at the commit, and the exporter reads GitLab blob and tree and codeload zip. EmuDeck and RetroBat still require a 40-hex commit, and each launcher reader requires its own repository in the URL. - Latest release: fetch_github_latest_version, type-checked, is the only lookup. mame_hash_scraper, romset_dat_importer and the Lakka target scraper call it. romset_dat_importer's NoReleaseError is gone: a release without a tag raises RuntimeError. - Tree listing: github_tree and tree_entries refuse a truncated or unreadable listing. The libretro and core-info scrapers share core_info_files and fetch_info_file, with libretro's refusal of an empty listing. The core-info listing and the RetroDECK, ROCKNIX launcher and libretro listings were not checked for truncation before. - GET: fetch_bytes is the one request path. fetch_text, fetch_present (None on 404 only), github_json and download are built on it, so every read retries 429 and 5xx, caps the body and raises FetchError, a ConnectionError carrying the HTTP status. The target scrapers' own fetch_text raised RuntimeError: they now raise FetchError, and an optional Batocera board include is skipped on 404 only, not on any failure. The exporter's originals, MiSTer, RetroPie, redump and the buildbot index go through it too. - Headers: core.upstream.forge_headers is the one builder. The token goes to api.github.com and raw.githubusercontent.com, the host compared whole, as Bearer. A URL that only names api.github.com in its query got the scrapers' token before, and profile_sync sent its token to github.com, whose release downloads redirect to asset storage with the header. Scraper reads of raw files now carry the token. - Submodule commit: submodule_commit checks that the path is a submodule. Batocera's target scraper accepted any contents answer with a sha, a directory's tree sha included. - Download: download streams through a scratch file renamed into place, retries like fetch_bytes, can keep an existing file and returns the Content-Disposition. It replaces the _download of romset_dat_importer and game_dat_importer.
This commit is contained in:
1 parent
c25426f14c
commit
f61c2cb7d4
43 files changed
+791
-670
No files matched your search
@@ -8,13 +8,17 @@ from __future__ import annotations
|
||||
import argparse
|
||||
import importlib
|
||||
import pkgutil
|
||||
import re
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
from abc import ABC, abstractmethod
|
||||
from pathlib import Path
|
||||
|
||||
from core.github import github_token
|
||||
from model.sourcerevision import platform_revision
|
||||
|
||||
# Every target scraper reads the network through the platform scrapers'
|
||||
# helpers: one retry policy, one size cap, one token rule.
|
||||
from ..base_scraper import fetch_text, forge_headers
|
||||
|
||||
__all__ = ["BaseTargetScraper", "discover_target_scrapers", "fetch_text", "forge_headers",
|
||||
"target_cli"]
|
||||
|
||||
|
||||
class BaseTargetScraper(ABC):
|
||||
@@ -37,26 +41,10 @@ class BaseTargetScraper(ABC):
|
||||
filtered 10.1.1 systems. Moving to a new release is a rescrape of the
|
||||
platform first.
|
||||
"""
|
||||
import yaml
|
||||
|
||||
directory = Path(platforms_dir) if platforms_dir else (
|
||||
Path(__file__).resolve().parents[3] / "platforms"
|
||||
)
|
||||
with open(directory / f"{platform}.yml", encoding="utf-8") as handle:
|
||||
source = str((yaml.safe_load(handle) or {}).get("source", ""))
|
||||
for pattern in (
|
||||
r"^https://raw\.githubusercontent\.com/[^/]+/[^/]+/([^/]+)/",
|
||||
r"^https://github\.com/[^/]+/[^/]+/(?:blob|tree)/([^/]+)/",
|
||||
r"^https://gitlab\.com/.+?/-/(?:raw|blob|tree)/([^/]+)/",
|
||||
r"^https://codeload\.github\.com/[^/]+/[^/]+/(?:tar\.gz|zip)/([^/]+)$",
|
||||
):
|
||||
match = re.match(pattern, source)
|
||||
if match:
|
||||
return match.group(1)
|
||||
raise RuntimeError(
|
||||
f"platforms/{platform}.yml names no pinned revision in its source "
|
||||
f"({source!r}): rescrape the platform first"
|
||||
)
|
||||
return platform_revision(directory / f"{platform}.yml")
|
||||
|
||||
def write_output(self, data: dict, output_path: str) -> None:
|
||||
"""Write target data to YAML file."""
|
||||
@@ -71,28 +59,6 @@ class BaseTargetScraper(ABC):
|
||||
)
|
||||
|
||||
|
||||
def fetch_text(url: str, headers: dict[str, str] | None = None) -> str:
|
||||
"""GET a URL as UTF-8 text, RuntimeError with the URL when it fails."""
|
||||
try:
|
||||
req = urllib.request.Request(url, headers=headers or {"User-Agent": "retrobios-scraper/1.0"})
|
||||
with urllib.request.urlopen(req, timeout=30) as resp:
|
||||
return resp.read().decode("utf-8")
|
||||
except urllib.error.URLError as e:
|
||||
# A target written from a failed request loses its cores in silence.
|
||||
raise RuntimeError(f"cannot fetch {url}: {e}") from e
|
||||
|
||||
|
||||
def forge_headers(url: str, accept: str | None = None) -> dict[str, str]:
|
||||
"""Request headers, with the GitHub token when the URL is api.github.com."""
|
||||
headers = {"User-Agent": "retrobios-scraper/1.0"}
|
||||
if accept is not None:
|
||||
headers["Accept"] = accept
|
||||
token = github_token()
|
||||
if token and "api.github.com" in url:
|
||||
headers["Authorization"] = f"Bearer {token}"
|
||||
return headers
|
||||
|
||||
|
||||
def target_cli(scraper_class: type[BaseTargetScraper], description: str, unit: str = "cores") -> None:
|
||||
"""The command line every target scraper shares: --dry-run prints each
|
||||
target and its count of `unit`, --output writes the YAML, else it is printed."""
|
||||
|
||||
Reference in new issue
Block a user