#!/usr/bin/env python3
"""Buka sumber langsung: PP 5/2021 (BPK), klinik Hukumonline SIUP/TDP, DPMPTSP, UU 40/2007 Pasal 7."""
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)[:70]}"
    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;", "'").replace("&quot;", '"')
    return re.sub(r"\s+", " ", t).strip()


SUMBER = [
    ("https://peraturan.bpk.go.id/Details/161835/pp-no-5-tahun-2021", ["Nomor Induk Berusaha", "Tanda Daftar Perusahaan", "Siup"]),
    ("https://www.hukumonline.com/klinik/a/siup-dan-tdp-lt6142fb11737c5/", ["Nomor Induk Berusaha", "tidak lagi", "Pasal 7"]),
    ("https://dpmptsp.pamekasankab.go.id/cukup-pakai-nib-pengusaha-kini-tak-perlu-lagi-urus-siup-tdp-hingga-sku/", ["NIB", "SIUP"]),
    ("https://peraturan.bpk.go.id/Details/39188/uu-no-40-tahun-2007", ["akta notaris", "Pasal 7"]),
]

for u, kws in SUMBER:
    t = teks(u)
    print(f"\n===== {u}\n  len={len(t)}")
    print("  awal:", t[:200])
    if len(t) < 600:
        continue
    for kw in kws:
        for m in list(re.finditer(kw, t, re.I))[:3]:
            print(f"  [{kw}] ...", t[max(0, m.start() - 220):m.start() + 380])
            print()
