#!/usr/bin/env python3
"""Verifikasi PP 28/2025 (BPK abstract + BKPM riwayat) dan klaim NIB berlaku sebagai TDP/API (situs pemerintah)."""
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 teks(u, batas=4000000):
    try:
        with urllib.request.urlopen(urllib.request.Request(u, headers=UA), timeout=120) as x:
            b = x.read(batas)
    except urllib.error.HTTPError as e:
        return f"HTTP {e.code}"
    except Exception as e:
        return f"ERR {str(e)[:80]}"
    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 = t.replace("&nbsp;", " ").replace("&amp;", "&").replace("&#8217;", "'")
    return re.sub(r"\s+", " ", t).strip()


for u, kws in [
    ("https://peraturan.bpk.go.id/Details/319773/pp-no-28-tahun-2025", ["Mencabut", "Perizinan Berusaha Berbasis Risiko", "Tanggal"]),
    ("https://dpmptsp.bulelengkab.go.id/informasi/detail/artikel/oss-dan-nib-model-baru-sistem-perizinan-19", ["Nomor Induk Berusaha", "Tanda Daftar Perusahaan", "Angka Pengenal"]),
    ("https://peraturan.bpk.go.id/Details/319773/pp-no-28-tahun-2025#", ["NIB"]),
]:
    t = teks(u)
    print(f"\n===== {u}\n  len={len(t)}")
    if isinstance(t, str) and len(t) > 400:
        print("  awal:", t[:260])
        for kw in kws:
            for m in list(re.finditer(kw, t, re.I))[:2]:
                print(f"  [{kw}] ...", t[max(0, m.start() - 200):m.start() + 340])
