#!/usr/bin/env python3
"""Cek fakta tahap 2: teks pasal merek/hak cipta dari halaman hukumonline klinik + DGIP."""
import json, re, urllib.request, urllib.error

WS = "/home/mantapman/.hermes/profiles/atun/workspace"
OUT = f"{WS}/tslaw_pemulihan/perbaikan_20261020"
tok = json.load(open(f"{WS}/.serper_token.json"))
KEY = tok.get("api_key") or tok.get("key") or list(tok.values())[0]
UA = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/124 Safari/537.36",
      "Referer": "https://www.dgip.go.id/"}


def cari(q, n=8):
    req = urllib.request.Request("https://google.serper.dev/search",
                                 data=json.dumps({"q": q, "gl": "id", "hl": "id", "num": n}).encode(),
                                 headers={"X-API-KEY": KEY, "Content-Type": "application/json"})
    with urllib.request.urlopen(req, timeout=60) as r:
        return json.load(r)


def teks(u):
    try:
        with urllib.request.urlopen(urllib.request.Request(u, headers=UA), timeout=120) as x:
            b = x.read(4000000)
    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 = re.sub(r"&nbsp;|&#160;", " ", t)
    return re.sub(r"\s+", " ", t).strip()


def cari_pasal(t, pats, lebar=520):
    for p in pats:
        for m in list(re.finditer(p, t, re.I))[:1]:
            print(f"   [{p}] ...{t[max(0,m.start()-120):m.start()+lebar]}...")


Q = ["klinik hukumonline jangka waktu perlindungan merek 10 tahun sejak tanggal penerimaan pasal 35 UU 20/2016",
     "klinik hukumonline ketentuan pidana merek pasal 100 UU 20/2016 penjara denda",
     "klinik hukumonline jangka waktu hak cipta pasal 58 UU 28/2014 70 tahun setelah pencipta meninggal"]
urls = []
for q in Q:
    print(f"\n=== {q}")
    try:
        for o in cari(q).get("organic", [])[:6]:
            print("  -", o.get("link"))
            urls.append(o.get("link"))
    except Exception as e:
        print("  ERR", e)

print("\n\n######## BUKA HALAMAN")
for u in dict.fromkeys(urls):
    if "hukumonline.com" not in u and "dgip.go.id" not in u:
        continue
    t = teks(u)
    print(f"\n--- {u} ({len(t)} char)")
    if len(t) < 800:
        print("   ", t[:200])
        continue
    cari_pasal(t, [r"Pasal 35", r"Pasal 100", r"Pasal 58", r"Pasal 3 ayat", r"Tanggal Penerimaan",
                   r"150 \(seratus lima puluh\)", r"2 \(dua\) bulan"])
