diff --git a/scripts/check_freshness.py b/scripts/check_freshness.py index 30f068f1e..e92e27aea 100644 --- a/scripts/check_freshness.py +++ b/scripts/check_freshness.py @@ -32,6 +32,7 @@ from __future__ import annotations import argparse import contextlib +import http.client import io import json import re @@ -1130,8 +1131,9 @@ def redump_datfile_version(code: str) -> str: gives the download: a HEAD request, nothing downloaded.""" url = game_dat_importer.REDUMP_DATFILE_URL.format(code=code) request = urllib.request.Request(url, method="HEAD", headers={"User-Agent": "retrobios/1.0"}) - with urllib.request.urlopen(request, timeout=60) as response: - disposition = response.headers.get("Content-Disposition", "") + disposition = upstream.open_retrying( + request, lambda response: response.headers.get("Content-Disposition", ""), timeout=60 + ) named = game_dat_importer.DISPOSITION.search(disposition) match = game_dat_importer.REDUMP_FILE.match(named["file"]) if named else None if not match: @@ -1152,7 +1154,7 @@ def _check_redump_games(offline: bool) -> Finding: for code, (_name, system, _floor) in sorted(game_dat_importer.REDUMP_DATS.items()): try: remote = redump_datfile_version(code) - except (OSError, ValueError) as exc: + except (OSError, http.client.HTTPException, upstream.UpstreamError, ValueError) as exc: return Finding("catalogs", subject, UNKNOWN, local=newest, detail=str(exc)) remote_newest = max(remote_newest, remote) if local.get(system) != remote: diff --git a/scripts/fetch_site_images.py b/scripts/fetch_site_images.py index e4ebd8d6d..a9aa7315c 100644 --- a/scripts/fetch_site_images.py +++ b/scripts/fetch_site_images.py @@ -29,13 +29,12 @@ import json import re import subprocess import sys -import urllib.error import urllib.parse -import urllib.request from pathlib import Path from model.registry import load_platform_registry from core.paths import PLATFORMS_DIR, REPO_ROOT +from scraper.base_scraper import FetchError, fetch_bytes sys.path.insert(0, str(REPO_ROOT / "scripts")) @@ -47,9 +46,9 @@ AVATAR_HOST = "avatars.githubusercontent.com" USER_AGENT = "retrobios-site-images" def fetch(url: str) -> bytes: - request = urllib.request.Request(url, headers={"User-Agent": USER_AGENT}) - with urllib.request.urlopen(request, timeout=60) as response: - return response.read() + """A GET retried on a rate limit or a dropped connection: one 429 + midway left icons rewritten under a sources.json of the previous run.""" + return fetch_bytes(url, {"User-Agent": USER_AGENT}, timeout=60) def square_image(data: bytes, size: int, kind: str = "PNG", flip: bool = False, @@ -212,8 +211,8 @@ def fetch_icons() -> None: url = f"{SYSTEM_ICON_BASE}/{urllib.parse.quote(name)}.png" try: data = fetch(url) - except urllib.error.HTTPError as exc: - if exc.code == 404: + except FetchError as exc: + if exc.status == 404: continue raise file_name = f"{name}.png"