Files
libretro/scripts/indexnow.py
T
Abdessamad Derraz 8a8fa71fbc 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.
2026-11-21 10:30:13 +01:00

80 lines
3.0 KiB
Python

#!/usr/bin/env python3
"""The pages a deploy changes, as the IndexNow list a search engine takes.
A search engine revisits a page on its own schedule, and an assistant that
searches through it reads the copy it last saw. IndexNow (Bing, Yandex,
Seznam, Naver) takes a list of changed addresses and fetches them soon
after. Google does not take it and reads the sitemap.
The built site carries `digests.json`, each page's SHA-256 before its build
line (scripts/site_markdown.py). This compares it with the one the live site
serves, before the deploy replaces it: a page whose digest differs, a new
page and a removed one are listed. The deploy job posts the list once the
new site is live (.github/workflows/deploy-site.yml).
python scripts/indexnow.py --dist website/dist # print the list
python scripts/indexnow.py --dist website/dist --output "$GITHUB_OUTPUT"
"""
from __future__ import annotations
import argparse
import json
import os
import sys
from pathlib import Path
sys.path.insert(0, os.path.dirname(__file__))
from core import upstream
from sitegen.agents import INDEXNOW_FILE, INDEXNOW_KEY, SITE
HOST = "abdess.github.io"
# What one IndexNow request accepts.
MAX_URLS = 10_000
USER_AGENT = "retrobios-indexnow"
def live_digests(url: str = f"{SITE}digests.json") -> dict[str, str]:
"""The digests the live site serves. None yet (the first deploy that
writes them) is an empty map: every page is new."""
body = upstream.fetch_body(url, timeout=60, user_agent=USER_AGENT)
return {} if body is None else json.loads(body.decode("utf-8"))
def changed_urls(new: dict[str, str], old: dict[str, str]) -> list[str]:
"""Pages added, changed or removed between two digest maps."""
return sorted(url for url in new.keys() | old.keys() if new.get(url) != old.get(url))
def payload(urls: list[str]) -> dict:
if len(urls) > MAX_URLS:
raise SystemExit(f"error: {len(urls)} changed pages, one request takes {MAX_URLS}")
return {"host": HOST, "key": INDEXNOW_KEY, "keyLocation": f"{SITE}{INDEXNOW_FILE}",
"urlList": urls}
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0], allow_abbrev=False)
parser.add_argument("--dist", type=Path, required=True, help="the built site (website/dist)")
parser.add_argument("--output", type=Path,
help="append payload=<json> to this file (a GitHub step output)")
args = parser.parse_args()
new = json.loads((args.dist / "digests.json").read_text(encoding="utf-8"))
urls = changed_urls(new, live_digests())
print(f"{len(urls)} of {len(new)} pages changed", file=sys.stderr)
if not urls:
return 0
body = json.dumps(payload(urls), separators=(",", ":"))
if args.output:
with args.output.open("a", encoding="utf-8") as handle:
handle.write(f"payload={body}\n")
else:
print(body)
return 0
if __name__ == "__main__":
from core.cli import run_cli
run_cli(main)