rzhugging's picture
Global Eco-Certification Knowledge Base v1.0.0 (research cut-off 2026-11-19) (part 2)
4dd8bd9 verified
Raw History Blame Contribute Delete
2.05 kB
"""Check that every cited source URL resolves (build-time step; needs network and curl).
Usage: python scripts/check_urls.py <repo_root> <url_checks.json>
Result per URL: status = ok | redirected_ok | blocked_automated (401/403/429/503, often bot protection —
the page may still open in a browser) | not_found (404/410) | error (timeout/DNS/TLS) plus the HTTP code.
"""
import concurrent.futures as cf
import json
import subprocess
import sys
import time
from pathlib import Path
UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36"
root, out = Path(sys.argv[1]), Path(sys.argv[2])
urls = sorted({s["url"] for s in json.loads((root / "json" / "official_sources.json").read_text("utf-8"))})
def probe(url):
def run(extra):
r = subprocess.run(["curl", "-s", "-o", "NUL" if sys.platform == "win32" else "/dev/null", "-L", "--max-time", "30",
"-A", UA, "-w", "%{http_code} %{num_redirects}", *extra, url], capture_output=True, text=True)
code, redirects = (r.stdout.split() + ["000", "0"])[:2]
return int(code) if code.isdigit() else 0, int(redirects) if redirects.isdigit() else 0
code, red = run(["-I"])
if code in (0, 400, 403, 404, 405, 429, 500, 501, 503):
code, red = run([]) # some servers reject HEAD; retry with GET
status = ("ok" if 200 <= code < 300 and red == 0 else "redirected_ok" if 200 <= code < 300
else "blocked_automated" if code in (401, 403, 429, 503) else "not_found" if code in (404, 410)
else "error" if code == 0 else f"http_{code}")
return url, {"status": status, "http_code": code, "checked_at": time.strftime("%Y-%m-%d")}
with cf.ThreadPoolExecutor(max_workers=16) as ex:
results = dict(ex.map(probe, urls))
out.write_text(json.dumps(results, indent=1), "utf-8")
summary = {}
for v in results.values():
summary[v["status"]] = summary.get(v["status"], 0) + 1
print(json.dumps({"checked": len(results), "summary": summary}, indent=1))