#!/usr/bin/env python3
"""Daftar teks yang TERLIHAT memuat 'Klinik DNI' (tanpa menghitung slug/url), beserta kalimatnya."""
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"}
POLA = re.compile(r"klinik[\s_-]*dni(?:\s*-?\s*(?:krian|sidoarjo))?", re.I)


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


hasil = []
for jenis in ("posts", "pages"):
    page = 1
    while True:
        d = rest(f"/wp-json/wp/v2/{jenis}?per_page=100&page={page}&status=publish&context=edit"
                 f"&_fields=id,slug,link,title,content")
        if not isinstance(d, list) or not d:
            break
        for x in d:
            # buang isi atribut (href/class/js) supaya hanya teks yang dibaca pengunjung
            bersih = re.sub(r"<[^>]+>", " ", x["content"]["raw"])
            bersih = re.sub(r"\{[^{}]*\}", " ", bersih)
            ketemu = list(POLA.finditer(bersih))
            if ketemu:
                hasil.append({"jenis": jenis, "id": x["id"], "link": x["link"],
                              "judul": re.sub(r"<[^>]+>", "", str(x["title"]["raw"]))[:80],
                              "jumlah": len(ketemu),
                              "contoh": [re.sub(r"\s+", " ", bersih[max(0, k.start() - 130):k.end() + 80]).strip()
                                         for k in ketemu[:5]]})
        if len(d) < 100:
            break
        page += 1

print(f"konten dengan teks terlihat 'Klinik DNI': {len(hasil)}\n")
for h in sorted(hasil, key=lambda y: -y["jumlah"]):
    print(f"■ #{h['id']} ({h['jumlah']}×) {h['judul'][:60]}")
    print(f"   {h['link'][:88]}")
    for c in h["contoh"]:
        print(f"   …{c[:190]}…")
    print()
json.dump(hasil, open(f"{WS}/drafts/tsc_teks_klinik_dni.json", "w"), ensure_ascii=False, indent=1)
