#!/usr/bin/env python3
"""TSC — rencana perapian dua klaster: halaman kanonik, halaman yang digabung, dan bagian uniknya."""
import base64, importlib.util, json, re, time, urllib.error, urllib.request

WS = "/home/mantapman/.hermes/profiles/atun/workspace"
spec = importlib.util.spec_from_file_location("cfg", f"{WS}/cfg_tsc.py")
m = importlib.util.module_from_spec(spec)
spec.loader.exec_module(m)
CFG = m.CFG
DOM = CFG["domain"]
AUTH = base64.b64encode(f"{CFG['user']}:{CFG['pass']}".encode()).decode()
UA = {"User-Agent": "Mozilla/5.0"}


def rest(path):
    h = {"Authorization": f"Basic {AUTH}", **UA}
    r = urllib.request.Request(f"https://{DOM}{path}", headers=h)
    for i in range(4):
        try:
            with urllib.request.urlopen(r, timeout=120) as x:
                return json.loads(x.read())
        except urllib.error.HTTPError as e:
            if e.code in (429, 404):
                time.sleep(4)
                continue
            return {"err": e.code}
        except Exception:
            time.sleep(4)
    return {}


peta = json.load(open(f"{WS}/drafts/tsc_peta_topik.json"))
ambil = {x["topik"]: x for x in peta}
TOPIK = ["klinik kecantikan krian", "jerawat", "facial & perawatan wajah"]

for nama in TOPIK:
    t = ambil.get(nama)
    if not t:
        continue
    kan = t["kanonik"]
    print(f"\n■ {nama}")
    print(f"   KANONIK #{kan['id']} {kan['tayang']} tayang · {kan['kata']} kata · /{kan['slug'][:60]}")
    konten_kan = rest(f"/wp-json/wp/v2/posts/{kan['id']}?context=edit&_fields=content") if kan["jenis"] == "posts" else rest(f"/wp-json/wp/v2/pages/{kan['id']}?context=edit&_fields=content")
    h2_kan = set()
    if isinstance(konten_kan, dict) and "content" in konten_kan:
        h2_kan = {re.sub(r"<[^>]+>", "", x).strip().lower() for x in re.findall(r"<h2[^>]*>(.*?)</h2>", konten_kan["content"]["raw"], re.S)}
    for k in t["kandidat"]:
        if k["tayang"] > 25:
            continue
        jenis = k["jenis"]
        o = rest(f"/wp-json/wp/v2/{jenis}/{k['id']}?context=edit&_fields=content")
        h2 = []
        if isinstance(o, dict) and "content" in o:
            h2 = [re.sub(r"<[^>]+>", "", x).strip() for x in re.findall(r"<h2[^>]*>(.*?)</h2>", o["content"]["raw"], re.S)]
        unik = [h for h in h2 if h.strip().lower() not in h2_kan]
        print(f"      → gabung #{k['id']:<5} {k['tayang']:>3} tayang · {k['kata']:>4} kata · /{k['slug'][:52]}")
        print(f"         bagian yang belum ada di kanonik: {len(unik)} → {[u[:38] for u in unik[:4]]}")
        time.sleep(0.4)
