#!/usr/bin/env python3
"""Cari & buka sumber resmi (.go.id / wikisource) untuk teks pasal KUHPerdata yang akan dikutip."""
import json, 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"}
WS = "/home/mantapman/.hermes/profiles/atun/workspace"
tok = json.load(open(f"{WS}/.serper_token.json"))
KEY = tok.get("api_key") or tok.get("key") or list(tok.values())[0]


def cari(q, n=6):
    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:
        d = json.load(r)
    return [(o.get("link"), (o.get("snippet") or "")[:300]) for o in d.get("organic", [])[:n]]


def get(u, n=400000):
    try:
        r = urllib.request.Request(u, headers=UA)
        with urllib.request.urlopen(r, timeout=70) as x:
            return x.status, x.read(n).decode("utf-8", "replace")
    except urllib.error.HTTPError as e:
        return e.code, ""
    except Exception as e:
        return type(e).__name__, ""


def teks(html):
    t = re.sub(r"<(script|style)[^>]*>.*?</\1>", " ", html, flags=re.S | re.I)
    t = re.sub(r"<[^>]+>", " ", t)
    t = re.sub(r"&nbsp;?", " ", t)
    return re.sub(r"\s+", " ", t).strip()


Q = ["\"Pasal 1243 KUHPerdata\" bunyi pasal penggantian biaya kerugian dan bunga site:go.id",
     "\"Pasal 1249 KUHPerdata\" \"Pasal 1250\" bunga moratoir site:go.id",
     "\"Pasal 1754 KUHPerdata\" pinjam meminjam bunyi site:go.id",
     "\"Pasal 1234 KUHPerdata\" wanprestasi site:go.id",
     "Kitab Undang-Undang Hukum Perdata wikisource Buku III perikatan"]

for q in Q:
    print(f"\n### {q}")
    for link, sn in cari(q):
        print(f"  - {link}\n      {sn}")

print("\n=== BUKA KANDIDAT ===")
for u in ["https://id.wikisource.org/wiki/Kitab_Undang-Undang_Hukum_Perdata",
          "https://id.wikisource.org/wiki/KUHPerdata/Buku_III",
          "https://id.wikisource.org/wiki/Kitab_Undang-undang_Hukum_Perdata"]:
    st, h = get(u)
    print(f"  {st} {u} ({len(h)} byte)")
    if st == 200:
        print("     judul:", re.search(r"<title>(.*?)</title>", h, re.S).group(1)[:120] if re.search(r"<title>(.*?)</title>", h, re.S) else "-")
