File size: 1,626 Bytes
1c951bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python3
"""
URL and citation link checker for UnsolvedEarth v1.0.0.
Verifies that all source_url and literature_sources URLs use valid HTTPS schemes
and clean URI formatting.
"""

import json
import sys
import urllib.parse
from pathlib import Path

ROOT = Path(__file__).resolve().parent.parent


def is_valid_https_url(u):
    if not u or not isinstance(u, str):
        return False
    parsed = urllib.parse.urlparse(u)
    return parsed.scheme == "https" and bool(parsed.netloc) and " " not in u


def main():
    problems = json.loads((ROOT / "problems.json").read_text(encoding="utf-8"))
    sources = json.loads((ROOT / "source_registry.json").read_text(encoding="utf-8"))

    errors = []
    checked = 0

    for s in sources:
        u = s.get("url")
        checked += 1
        if not is_valid_https_url(u):
            errors.append(f"{s.get('source_id')}: Invalid URL '{u}'")

    for p in problems:
        u = p.get("source_url")
        checked += 1
        if not is_valid_https_url(u):
            errors.append(f"{p.get('id')}: Invalid source_url '{u}'")
        for ls in p.get("literature_sources") or []:
            lu = ls.get("url")
            checked += 1
            if not is_valid_https_url(lu):
                errors.append(f"{p.get('id')}: Invalid literature_source URL '{lu}'")

    print(f"Checked {checked} URLs across sources and problems.")
    if errors:
        print(f"FAILED with {len(errors)} link errors:")
        for e in errors[:20]:
            print("  ", e)
        sys.exit(1)
    print("LINK CHECK PASSED: 0 errors.")


if __name__ == "__main__":
    main()