Download scripts/check_urls.py from rzhugging/global-eco-certification-knowledge-base: direct link, hf CLI and curl.
- Browser
- Download file 2.05 kB
-
https://huggingface.co/datasets/rzhugging/global-eco-certification-knowledge-base/resolve/main/scripts/check_urls.py
- Command line
-
hf download hf://datasets/rzhugging/global-eco-certification-knowledge-base/scripts/check_urls.py
-
curl -L -o check_urls.py https://huggingface.co/datasets/rzhugging/global-eco-certification-knowledge-base/resolve/main/scripts/check_urls.py
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)) | |