#!/usr/bin/env python3
"""Cek HTTP semua calon tautan internal tslaw.id yang dipakai di artikel 8 (warisan)."""
import re, sys, urllib.request, urllib.error

sys.path.insert(0, "/home/mantapman/.hermes/profiles/atun/workspace")
from tslaw_listikel_8 import A8

UA = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/124 Safari/537.36"}
urls = sorted(set(re.findall(r'href="(https?://[^"]+)"', A8["isi"])))
gagal = []
for u in urls:
    try:
        with urllib.request.urlopen(urllib.request.Request(u, headers=UA), timeout=60) as x:
            print(f"{x.status}  {u}")
    except urllib.error.HTTPError as e:
        print(f"{e.code}  {u}")
        gagal.append((u, e.code))
    except Exception as e:
        print(f"ERR  {u}  {str(e)[:60]}")
        gagal.append((u, str(e)[:60]))
print(f"\ntotal {len(urls)} tautan | gagal: {len(gagal)}")
for u, k in gagal:
    print(f"  GAGAL {k} -> {u}")
