#!/usr/bin/env python3 """ Turn the archived Norbourg.com home page into ready-to-open HTML. Two outputs are produced from the same source capture: norbourg.html one self-contained file (images inlined as data: URIs) norbourg-static/ index.html + images/ with plain relative paths (use this one if data: URIs are blocked by a viewer) Both keep the original table layout, iso-8859-1 charset and the hover JavaScript; they need no server and no network to display. """ import base64 import re import shutil import time import urllib.error import urllib.request from pathlib import Path ROOT = Path(__file__).resolve().parent SRC = ROOT / "source" / "raw-home.html" ASSETS = ROOT / "norbourg-2002" / "images" OUT_INLINE = ROOT / "norbourg.html" OUT_STATIC = ROOT / "norbourg-static" TS = "20020929201450" SHARD = f"http://web.archive.org/web/{TS}id_/http://www.norbourg.com/images/" UA = "Mozilla/5.0 (X11; Linux x86_64) norbourg-template/1.0" MIME = {".gif": "image/gif", ".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".png": "image/png", ".swf": "application/x-shockwave-flash"} IMG_RE = re.compile(r"(?:https?://(?:www\.)?norbourg\.com)?/?images/([A-Za-z0-9_.\-]+\.(?:gif|jpe?g|png))", re.I) LINK_RE = re.compile(r'href="(?:https?://(?:www\.)?norbourg\.com)?/?([A-Za-z0-9_.\-]+\.(?:asp|html))"', re.I) def fetch_asset(name): dest = ASSETS / name if dest.is_file() and dest.stat().st_size > 0: return dest.read_bytes() for attempt in range(4): try: req = urllib.request.Request(SHARD + name, headers={"User-Agent": UA}) with urllib.request.urlopen(req, timeout=60) as r: data = r.read() if data: dest.parent.mkdir(parents=True, exist_ok=True) dest.write_bytes(data) print(f" fetched {name} ({len(data)} bytes)") time.sleep(0.3) return data except urllib.error.HTTPError as e: print(f" {name}: HTTP {e.code}") if e.code == 404: return None except Exception as e: # noqa: BLE001 print(f" {name}: {e}") time.sleep(2 * (attempt + 1)) return None def finalise(html): """Strip the 2002 hit counter and point page links at the archived copies.""" html = re.sub(r"(?is)]*>\s*