#!/usr/bin/env python3
"""TSC batch 4: baca 5 artikel tipis berikutnya + cek keberadaan FAQ/schema dan domain lama."""
import base64, html, json, os, re, time, urllib.request, importlib.util

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

SUDAH = {9266, 1675, 1697, 1713, 1672, 1662, 9268, 1716, 1669, 1744, 1747}
data = json.load(open(f"{WS}/drafts/tsc_tipis_urutan.json"))
kandidat = [a for a in data["berimpresi"] if a["id"] not in SUDAH][:5]

for a in kandidat:
    pid = a["id"]
    r = urllib.request.Request(f'https://{m.CFG["domain"]}/wp-json/wp/v2/posts/{pid}?context=edit&_fields=id,slug,link,title,content',
                               headers={"Authorization": f"Basic {AUTH}", **UA})
    with urllib.request.urlopen(r, timeout=150) as x:
        d = json.loads(x.read())
    raw = d.get("content", {}).get("raw", "")
    teks = re.sub(r"\s+", " ", html.unescape(re.sub(r"<[^>]+>", " ", re.sub(r"<script.*?</script>", " ", raw, flags=re.S))))
    kata = len([w for w in teks.split() if w])
    punya_faq = bool(re.search(r"<h2[^>]*>\s*FAQ\s*</h2>", raw, re.I)) or "FAQPage" in raw
    domain_lama = raw.count("klinikdnisidoarjo.com")
    print(f"\n{'='*90}")
    print(f"### #{pid} /{d['slug']}")
    print(f"   {kata} kata | imp {a['imp']} | klik {a['klik']} | pos {a['pos']} | FAQ: {'ada' if punya_faq else 'BELUM'} | domain lama: {domain_lama}")
    print(f"   judul: {html.unescape(d['title']['rendered'])}")
    print(f"   heading: {[html.unescape(re.sub(r'<[^>]+>', '', x)).strip()[:50] for x in re.findall(r'<h[23][^>]*>(.*?)</h[23]>', raw, re.S)]}")
    print(f"   awal: {teks[:220]}")
    print(f"   akhir: {teks[-200:]}")
