#!/usr/bin/env python3
"""Verifikasi teks pasal: KUHPerdata (Wikisource) + UU 37/2004 Pasal 2 (PDF BPK) — 14 Okt 2026."""
import re, urllib.request, urllib.error

UA = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/124 Safari/537.36"}


def get(u, n=500000):
    try:
        r = urllib.request.Request(u, headers=UA)
        with urllib.request.urlopen(r, timeout=90) as x:
            return x.status, x.read(n)
    except urllib.error.HTTPError as e:
        return e.code, b""
    except Exception as e:
        return type(e).__name__, b""


def teks(b):
    t = b.decode("utf-8", "replace")
    t = re.sub(r"<(script|style)[^>]*>.*?</\1>", " ", t, flags=re.S | re.I)
    t = re.sub(r"<[^>]+>", " ", t)
    t = re.sub(r"&nbsp;?", " ", t)
    return re.sub(r"\s+", " ", t).strip()


# 1) UU 37/2004 PDF resmi BPK
st, b = get("https://peraturan.bpk.go.id/Download/30522/UU%20Nomor%2037%20Tahun%202004.pdf")
print(f"=== UU 37/2004 PDF → HTTP {st}, {len(b)} byte")
if st == 200 and b[:4] == b"%PDF":
    try:
        import io, zlib
        # ekstraksi teks kasar dari PDF (tanpa pustaka eksternal): cari stream teks
        raw = b
        chunks = re.findall(rb"stream\r?\n(.*?)endstream", raw, re.S)
        hasil = []
        for c in chunks:
            try:
                d = zlib.decompress(c)
            except Exception:
                continue
            for m in re.finditer(rb"\((?:[^()\\]|\\.)*\)", d):
                hasil.append(m.group(0)[1:-1].decode("latin-1", "replace"))
        txt = re.sub(r"\s+", " ", " ".join(hasil))
        print("  panjang teks:", len(txt))
        for pat in ["dua atau lebih Kreditor", "jatuh waktu dan dapat ditagih", "dinyatakan pailit"]:
            for m in list(re.finditer(re.escape(pat), txt, re.I))[:2]:
                a, z = max(0, m.start() - 250), min(len(txt), m.end() + 250)
                print(f"  [{pat}] ...{txt[a:z]}...")
    except Exception as e:
        print("  gagal ekstrak:", e)
else:
    print("  badan bukan PDF:", b[:60])

# 2) KUHPerdata Wikisource (Buku III & IV)
for url in ["https://id.wikisource.org/wiki/Kitab_Undang-Undang_Hukum_Perdata/Buku_III",
            "https://id.wikisource.org/wiki/Kitab_Undang-Undang_Hukum_Perdata/Buku_IV"]:
    st, b = get(url)
    print(f"\n=== {url} → HTTP {st}, {len(b)} byte")
    if st != 200 or not b:
        continue
    t = teks(b)
    for pat in ["Pasal 1234", "Pasal 1236", "Pasal 1243", "Pasal 1249", "Pasal 1250",
                "Pasal 1338", "Pasal 1754", "Pasal 1866", "Pasal 1967", "Pasal 1131", "Pasal 613"]:
        m = re.search(re.escape(pat) + r"\s", t)
        if m:
            print(f"  [{pat}] ...{t[m.start():m.start()+430]}...")
