#!/usr/bin/env python3
"""Ambil teks PP 28/2025 (PDF BKPM) + halaman DJP NPWP 16 digit + UU 33/2014."""
import re, urllib.request, urllib.error, zlib

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


def raw(u, batas=12000000):
    try:
        with urllib.request.urlopen(urllib.request.Request(u, headers=UA), timeout=180) as x:
            return x.read(batas), x.headers.get("Content-Type", "")
    except urllib.error.HTTPError as e:
        return None, f"HTTP {e.code}"
    except Exception as e:
        return None, f"ERR {str(e)[:80]}"


b, ct = raw("https://jdih-storage.bkpm.go.id/jdih/jdih/Salinan-PP-Nomor-28-Tahun-2025-.pdf")
print("PDF:", len(b) if b else ct, b[:8] if b else "")
if b:
    open("/tmp/pp28_2025.pdf", "wb").write(b)
    out = []
    for m in re.finditer(rb"stream\r?\n?(.*?)endstream", b, re.S):
        try:
            out.append(zlib.decompress(m.group(1)).decode("latin-1"))
        except Exception:
            pass
    tt = " ".join(out)
    tt = " ".join(re.findall(r"\((?:\\.|[^()\\])*\)", tt))
    tt = re.sub(r"[()]", "", tt).replace("\\(", "(").replace("\\)", ")")
    tt = re.sub(r"\s+", " ", tt)
    print("teks PDF:", len(tt), "char")
    open("/tmp/pp28_2025.txt", "w").write(tt)
    for kw in ["Nomor Induk Berusaha", "Tanda Daftar Perusahaan", "Surat Izin Usaha Perdagangan"]:
        ms = list(re.finditer(kw, tt))
        print(f"\n[{kw}] {len(ms)}x")
        for m in ms[:3]:
            print("   ...", tt[max(0, m.start() - 250):m.start() + 400].replace("\n", " "))

for u in ["https://pajak.go.id/id/artikel/coretax-djp-era-npwp-16-digit"]:
    b2, ct2 = raw(u, 3000000)
    print(f"\n{u}: {len(b2) if b2 else ct2}")
    if b2:
        t = b2.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"\s+", " ", t)
        for kw in ["16 digit", "NITKU", "NIK"]:
            for m in list(re.finditer(kw, t))[:2]:
                print(f"  [{kw}] ...", t[max(0, m.start() - 200):m.start() + 300])
