#!/usr/bin/env python3
"""
Recover ONLY the Spanish recipes that are no longer on the live lapaella.net,
from the Wayback Machine, into wayback-mirror/ .
Targets (decided with the user):
1. Pimientos rojos rellenos de arroz - modern WordPress recipe page, now 404
2. Arroz con pollo, pato y caracoles - /web1/arrozpollo.html (~2003 static site)
3. Paella de habas - /web1/paellahabas.html
4. Paella mixta - /web1/paellamixta.html
Each page is saved with the Wayback toolbar stripped and its images pulled in
alongside it (links rewritten to the local copies).
"""
import json, os, re, subprocess, tempfile, time, urllib.parse
from pathlib import Path
ROOT = Path(__file__).parent
OUT = ROOT / "wayback-mirror"
UA = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36"
TARGETS = [
# (original url, local dir under OUT, local html name)
("https://lapaella.net/arroces-tipicos-de-valencia/pimientos-rojos-rellenos-de-arroz/",
"pimientos-rojos-rellenos-de-arroz", "index.html"),
("http://www.lapaella.net/web1/arrozpollo.html", "web1", "arrozpollo.html"),
("http://www.lapaella.net/web1/paellahabas.html", "web1", "paellahabas.html"),
("http://www.lapaella.net/web1/paellamixta.html", "web1", "paellamixta.html"),
]
def curl(url, binary=True, tries=5):
for a in range(tries):
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".tmp"); tmp.close()
try:
r = subprocess.run(
["/usr/bin/curl", "-s", "-L", "--max-time", "60", "-A", UA,
"-w", "%{http_code}", "-o", tmp.name, url],
capture_output=True, text=True, timeout=90)
code = (r.stdout or "").strip()[-3:]
data = Path(tmp.name).read_bytes()
if code == "200" and data:
return data if binary else data.decode("utf-8", "replace")
if code in ("429", "403", "503", "000", "520", "521"):
wait = 20 * (a + 1)
print(f" {code} on {url[:70]} - backoff {wait}s")
time.sleep(wait); continue
print(f" {code} on {url[:80]}")
return None
finally:
try: os.unlink(tmp.name)
except OSError: pass
return None
def cdx_best(original):
"""Return the newest 200 snapshot timestamp for a URL, or None."""
q = ("http://web.archive.org/cdx/search/cdx?"
+ urllib.parse.urlencode({"url": original, "output": "json",
"filter": "statuscode:200", "limit": "-8"}))
raw = curl(q, binary=False)
if not raw:
return None
try:
rows = json.loads(raw)
except json.JSONDecodeError:
return None
if len(rows) < 2:
return None
# rows[0] is the header; take the last (newest) row's timestamp
return rows[-1][1]
TOOLBAR = [
(re.compile(r'.*?', re.S), ''),
(re.compile(r'', re.S | re.I), ''),
(re.compile(r'