#!/usr/bin/env python3
"""Cari teks UU 20/2016 (merek) & UU 28/2014 (hak cipta) dari sumber terbaca (bukan scan)."""
import re, zlib, urllib.request, urllib.error

UA = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/124 Safari/537.36"}
OUT = "/home/mantapman/.hermes/profiles/atun/workspace/tslaw_pemulihan/perbaikan_20261020"

CALON = {
    "merek_dgip": "https://www.dgip.go.id/unduhan/download/uu-nomor-20-tahun-2016-tentang-merek-32",
    "merek_ws": "https://id.wikisource.org/wiki/Undang-Undang_Republik_Indonesia_Nomor_20_Tahun_2016",
    "hc_ws": "https://id.wikisource.org/wiki/Undang-Undang_Republik_Indonesia_Nomor_28_Tahun_2014",
}


def ambil(u):
    try:
        with urllib.request.urlopen(urllib.request.Request(u, headers=UA), timeout=180) as x:
            return x.status, x.read(), x.headers.get("Content-Type", "")
    except urllib.error.HTTPError as e:
        return e.code, b"", ""
    except Exception as e:
        return 0, str(e).encode()[:100], ""


def pdf_teks(b):
    out = []
    for m in re.finditer(rb"stream\r?\n(.*?)endstream", b, re.S):
        try:
            d = zlib.decompress(m.group(1))
        except Exception:
            continue
        if b"Tj" not in d and b"TJ" not in d:
            continue
        pot = []
        for t in re.finditer(rb"\((?:[^()\\]|\\.)*\)|TJ|Tj|T\*|Td|TD", d):
            g = t.group(0)
            if g.startswith(b"("):
                s = g[1:-1].decode("latin-1")
                s = re.sub(r"\\([0-7]{1,3})", lambda mm: chr(int(mm.group(1), 8)), s)
                s = s.replace(r"\(", "(").replace(r"\)", ")").replace(r"\\", "\\")
                pot.append(s)
            elif g in (b"Td", b"TD", b"T*"):
                pot.append("\n")
        out.append("".join(pot))
    return re.sub(r"\n{3,}", "\n\n", "\n".join(out))


def html_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)
    t = re.sub(r"&amp;", "&", t)
    t = re.sub(r"&#160;", " ", t)
    return re.sub(r"[ \t]+", " ", t)


for nama, u in CALON.items():
    st, b, ct = ambil(u)
    print(f"\n== {nama} HTTP {st} {len(b)}b ct={ct[:40]}")
    if st != 200 or not b:
        print("   ", b[:120])
        continue
    t = pdf_teks(b) if (b[:4] == b"%PDF" or "pdf" in ct.lower()) else html_teks(b)
    open(f"{OUT}/{nama}.txt", "w").write(t)
    print("   teks:", len(t))
    print("   cuplikan:", re.sub(r"\s+", " ", t[:200]))
