#!/usr/bin/env python3 """Cobertura real: cada URL del inventario debe tener su fichero en raw/. Contempla las tres formas en que wget nombra el fichero: /es/x.html -> x.html /es/ -> index.html /es/x?.html (alias con '?' literal) -> "x?.html" o "x" """ import os, json from urllib.parse import urlsplit, unquote BASE = "/home/rafa/joomla-migration/mirror-antiguo" RUN = open(os.path.join(BASE, "CURRENT_RUN")).read().strip() DIR = os.path.join(BASE, "runs", RUN) RAW = os.path.join(DIR, "raw", "antiguo.feadulta.com") def candidates(url): rest = url.split("antiguo.feadulta.com", 1)[1] rest = unquote(rest) yield rest.lstrip("/") # nombre literal, con '?' incluido p = urlsplit(rest).path.lstrip("/") yield p # truncado en el '?' if rest.endswith("/") or p.endswith("/") or p == "": yield (p + "index.html") ok, missing = 0, [] urls = [l.strip() for l in open(os.path.join(BASE, "inventory", "urls-input.txt")) if l.strip()] for u in urls: if any(os.path.isfile(os.path.join(RAW, c)) for c in candidates(u) if c): ok += 1 else: missing.append(u) print("inventario:", len(urls)) print("con fichero en raw/:", ok) print("sin fichero:", len(missing)) print("cobertura: %.2f%%" % (ok * 100.0 / len(urls))) with open(os.path.join(DIR, "coverage-missing.txt"), "w") as f: for u in missing: f.write(u + "\n") for u in missing[:25]: print(" ", u) total = sum(len(fs) for _r, _d, fs in os.walk(os.path.join(DIR, "raw"))) json.dump({"inventario": len(urls), "capturadas": ok, "sin_fichero": len(missing), "cobertura_pct": round(ok * 100.0 / len(urls), 2), "ficheros_totales_raw": total}, open(os.path.join(DIR, "coverage-report.json"), "w"), indent=2)