mirror of
https://github.com/Abdess/retroarch_system.git
synced 2026-11-24 10:37:54 -06:00
refactor: route network access through upstream
Branch heads (scrapers, FBNeo import, data directories) resolve through core.upstream.resolve_head, with cache_dir=None to keep nothing under .cache/upstream. Streaming downloads (large files, external pack files, data directory archives, scraper downloads, standalone probe) go through one core.upstream.download: scratch then rename, size cap, retries of a cut stream, a 5xx or a rate limit. The shared rate limit wait reads GoatCounter's X-Rate-Limit-Reset, so site insights, download.py, indexnow and the contributor list use the same retrying client. list_gitlinks raises TruncatedListingError on a partial tree, read from one tree walk shared with list_tree (Forgejo paging included). The Recalbox configs/ listing is paged with the shared page cap.
This commit is contained in:
1 parent
526d7f09c7
commit
8a8fa71fbc
25 files changed
+708
-266
No files matched your search
+28
-26
@@ -19,6 +19,7 @@ from collections import defaultdict
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
|
||||
from core import upstream
|
||||
from core.artifacts import write_text_atomic
|
||||
from core.github import github_token
|
||||
|
||||
@@ -28,14 +29,14 @@ SITE_PROPERTY = "https://abdess.github.io/retrobios/"
|
||||
GOATCOUNTER = "https://retrobios.goatcounter.com/api/v0"
|
||||
REPOSITORY = "Abdess/retrobios"
|
||||
SOURCES = ("gsc", "goatcounter", "github")
|
||||
USER_AGENT = "retrobios-site-insights"
|
||||
# GoatCounter builds an export in the background: it is asked for every
|
||||
# EXPORT_POLL seconds, EXPORT_TRIES times. It allows one export an hour.
|
||||
EXPORT_POLL = 5
|
||||
EXPORT_TRIES = 60
|
||||
# GoatCounter answers four requests a second and 500 per window, and says
|
||||
# when to come back: a short wait is waited, a long one ends the run.
|
||||
RATE_LIMIT_TRIES = 8
|
||||
RATE_LIMIT_LONGEST_WAIT = 60
|
||||
# when to come back in X-Rate-Limit-Reset. core.upstream.open_retrying waits
|
||||
# a short wait and gives up on a long one, which ends the run.
|
||||
|
||||
# Expected click-through rate: the site's own rate at each rounded
|
||||
# position, positions past 20 pooled in LAST_BUCKET. A bucket under
|
||||
@@ -67,25 +68,26 @@ class Query:
|
||||
# Reading the sources
|
||||
|
||||
|
||||
def _open(request: urllib.request.Request) -> tuple[int, bytes]:
|
||||
"""Status and body of a request, retried by core.upstream.open_retrying:
|
||||
a dropped connection or a 5xx is asked again, a rate limit waited out."""
|
||||
try:
|
||||
return upstream.open_retrying(request, lambda response: (response.status, response.read()), 60)
|
||||
except (urllib.error.HTTPError, upstream.RateLimitError) as error:
|
||||
if isinstance(error, urllib.error.HTTPError) and error.code != 429:
|
||||
raise
|
||||
headers = error.headers if isinstance(error, urllib.error.HTTPError) else None
|
||||
wait = upstream.requested_wait(headers)
|
||||
delay = f" for {wait:.0f} s" if wait is not None else ""
|
||||
raise SystemExit(f"{urllib.parse.urlsplit(request.full_url).hostname} refuses requests{delay} "
|
||||
"(rate limit); run again then, or --skip that source") from error
|
||||
|
||||
|
||||
def _fetch(url: str, headers: dict[str, str] | None = None, body: dict | None = None) -> tuple[int, bytes]:
|
||||
data = json.dumps(body).encode() if body is not None else None
|
||||
request = urllib.request.Request(url, data=data, headers={
|
||||
"User-Agent": "retrobios-site-insights", **({"Content-Type": "application/json"} if data else {}),
|
||||
**(headers or {})})
|
||||
for _ in range(RATE_LIMIT_TRIES - 1):
|
||||
try:
|
||||
with urllib.request.urlopen(request, timeout=60) as response:
|
||||
return response.status, response.read()
|
||||
except urllib.error.HTTPError as error:
|
||||
if error.code != 429:
|
||||
raise
|
||||
wait = int(error.headers.get("X-Rate-Limit-Reset") or 1) + 1
|
||||
if wait > RATE_LIMIT_LONGEST_WAIT:
|
||||
raise SystemExit(f"{urllib.parse.urlsplit(url).hostname} refuses requests for "
|
||||
f"{wait} s (rate limit); run again then, or --skip that source") from error
|
||||
time.sleep(wait)
|
||||
with urllib.request.urlopen(request, timeout=60) as response:
|
||||
return response.status, response.read()
|
||||
return _open(urllib.request.Request(url, data=data, headers={
|
||||
"User-Agent": USER_AGENT, **({"Content-Type": "application/json"} if data else {}),
|
||||
**(headers or {})}))
|
||||
|
||||
|
||||
def _get_json(url: str, headers: dict[str, str] | None = None, body: dict | None = None) -> dict | list:
|
||||
@@ -112,9 +114,8 @@ def _google_token(key_file: Path) -> str:
|
||||
input=unsigned.encode(), capture_output=True, check=True).stdout
|
||||
body = urllib.parse.urlencode({"grant_type": "urn:ietf:params:oauth:grant-type:jwt-bearer",
|
||||
"assertion": f"{unsigned}.{b64(signature)}"}).encode()
|
||||
request = urllib.request.Request(key["token_uri"], data=body)
|
||||
with urllib.request.urlopen(request, timeout=60) as response:
|
||||
return json.load(response)["access_token"]
|
||||
request = urllib.request.Request(key["token_uri"], data=body, headers={"User-Agent": USER_AGENT})
|
||||
return json.loads(_open(request)[1])["access_token"]
|
||||
|
||||
|
||||
def fetch_queries(start: dt.date, end: dt.date) -> list[Query]:
|
||||
@@ -193,11 +194,12 @@ def fetch_goatcounter(start: dt.date, end: dt.date) -> dict:
|
||||
|
||||
|
||||
def fetch_github() -> dict:
|
||||
token = github_token()
|
||||
if not token:
|
||||
# Traffic figures are only served to the owner: without a token there
|
||||
# is nothing to read, not a smaller quota.
|
||||
if not github_token():
|
||||
raise SystemExit("GitHub traffic needs a token: GITHUB_TOKEN, or gh auth login")
|
||||
auth = {"Authorization": f"Bearer {token}", "Accept": "application/vnd.github+json"}
|
||||
api = f"https://api.github.com/repos/{REPOSITORY}"
|
||||
auth = upstream.forge_headers(api, "application/vnd.github+json", USER_AGENT)
|
||||
releases = _get_json(f"{api}/releases?per_page=10", auth)
|
||||
return {
|
||||
"views": _get_json(f"{api}/traffic/views", auth),
|
||||
|
||||
+21
-20
@@ -18,7 +18,7 @@ from core.artifacts import write_if_changed
|
||||
from core.cli import InputMissing
|
||||
from core.paths import DB_FILE, REPO_ROOT
|
||||
from core.sizes import size_text
|
||||
from core.upstream import forge_headers
|
||||
from core import upstream
|
||||
from installer_build.module import installer_module
|
||||
from model.composition import compute_composition, count_catalog_matched
|
||||
from model.declarations import declared_root
|
||||
@@ -503,26 +503,27 @@ def _existing_contributor_block(readme_path: str = "README.md") -> list[str]:
|
||||
|
||||
|
||||
def fetch_contributors() -> list[dict]:
|
||||
"""Fetch contributors from GitHub API, exclude bots."""
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
|
||||
url = f"https://api.github.com/repos/{REPO}/contributors"
|
||||
headers = forge_headers(url, user_agent="retrobios-readme/1.0")
|
||||
try:
|
||||
req = urllib.request.Request(url, headers=headers)
|
||||
with urllib.request.urlopen(req, timeout=10) as resp:
|
||||
data = json.loads(resp.read().decode())
|
||||
owner = REPO.split("/")[0]
|
||||
return [
|
||||
c
|
||||
for c in data
|
||||
if not c.get("login", "").endswith("[bot]")
|
||||
and c.get("type") == "User"
|
||||
and c.get("login") != owner
|
||||
]
|
||||
except (urllib.error.URLError, urllib.error.HTTPError):
|
||||
"""Contributors from the GitHub API, bots and the owner left out. Empty
|
||||
when the API cannot be read, or under RETROBIOS_OFFLINE: the caller then
|
||||
keeps the published list."""
|
||||
if os.environ.get("RETROBIOS_OFFLINE"):
|
||||
return []
|
||||
url = f"https://api.github.com/repos/{REPO}/contributors"
|
||||
try:
|
||||
body = upstream.fetch_body(url, accept_json=True, timeout=10, user_agent="retrobios-readme/1.0")
|
||||
data = json.loads(body) if body is not None else []
|
||||
except (upstream.UpstreamError, ValueError) as exc:
|
||||
print(f"contributors: {exc}", file=sys.stderr)
|
||||
return []
|
||||
owner = REPO.split("/")[0]
|
||||
return [
|
||||
c
|
||||
for c in data
|
||||
if isinstance(c, dict)
|
||||
and not c.get("login", "").endswith("[bot]")
|
||||
and c.get("type") == "User"
|
||||
and c.get("login") != owner
|
||||
]
|
||||
|
||||
|
||||
_CATALOG_LABELS = {"redump": "Redump", "no-intro": "No-Intro", "tosec": "TOSEC"}
|
||||
|
||||
Reference in new issue
Block a user