#!/usr/bin/env python3
# SPDX-License-Identifier: MIT
"""Fetch pinned WiiBrew pages into reference/wiki/.
WiiBrew (https://wiibrew.org) states no content licence on its pages, its About,
Policy, General disclaimer or Copyrights pages (read 2026-11-26), so its facts are
used and cited and its prose and tables are never copied into this repository
("facts only"; docs/sources-wii.md).
**robots.txt.** WiiBrew's robots.txt (read 2026-11-26) disallows ``/w/`` for every
user agent: that is where MediaWiki's ``api.php`` and ``index.php?oldid=`` live, so
this script never asks for an old revision or uses the API (unlike
n64-python's fetch_wiki.py). It reads only ``/wiki/
``, the page as it is
now, and pins what it got: the revision id the page reports (``wgRevisionId``) and
the SHA-256 of the article body (the ``mw-content-text`` block). It waits two
seconds between pages.
Consequence: a page edited since the pin cannot be fetched at its pinned revision.
The script then reports the move and keeps nothing; ``--pin`` re-pins deliberately.
python -I scripts/fetch_wiki.py # fetch what is missing, check pins
python -I scripts/fetch_wiki.py --verify # hash what is present, fetch nothing
python -I scripts/fetch_wiki.py --pin # fetch and pin titles not yet pinned (revid 0)
python -I scripts/fetch_wiki.py --repin # fetch every title now and pin it again
Each page lands as reference/wiki/.html (the article body) and a plain-text
rendering beside it (.txt) for reading; only the .html is pinned.
"""
from __future__ import annotations
import argparse
import hashlib
import html
import json
import re
import subprocess
import sys
import time
import urllib.parse
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
PINS = ROOT / "scripts" / "pinned_wiki.json"
BASE = "https://wiibrew.org/wiki/"
# An honest agent name, never a browser's (the 2026-11-26 brief).
USER_AGENT = "wii-python-reference-fetch/0.1 (emulator documentation; honours robots.txt)"
DELAY_S = 2.0
def get(title: str) -> str:
url = BASE + urllib.parse.quote(title.replace(" ", "_"), safe="/:()_-.,")
result = subprocess.run(
["curl", "-sSfL", "--max-time", "120", "-A", USER_AGENT, url],
capture_output=True,
check=False,
)
if result.returncode:
raise OSError(f"{title}: " + result.stderr.decode("utf-8", "replace").strip())
return result.stdout.decode("utf-8", "replace")
def article(page: str) -> tuple[int, str, str]:
"""Return (revision id, last-edited text, article body) from a /wiki/ page."""
rev = re.search(r'"wgRevisionId":(\d+)', page)
start = page.find('