#!/usr/bin/env python3
"""TSC batch 3: baca 5 artikel tipis berikutnya (yang masih berimpresi)."""
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}
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})
    try:
        with urllib.request.urlopen(r, timeout=150) as x:
            d = json.loads(x.read())
    except Exception as e:
        print(f"   #{pid} gagal: {str(e)[:50]}")
        continue
    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))))
    print(f"\n{'='*90}")
    print(f"### #{pid} /{d['slug']}")
    print(f"   {a['kata']} kata | imp {a['imp']} | klik {a['klik']} | pos {a['pos']} | sesi {a['sesi']}")
    print(f"   judul: {html.unescape(d['title']['rendered'])}")
    print(f"   heading: {[html.unescape(re.sub(r'<[^>]+>', '', x)).strip()[:48] for x in re.findall(r'<h[23][^>]*>(.*?)</h[23]>', raw, re.S)]}")
    print(f"   awal: {teks[:200]}")
    print(f"   akhir: {teks[-200:]}")
    tautan = sorted(set(re.findall(r'href="https://www\.theskincodeclinic\.com(/[^"]*)"', raw)))
    print(f"   tautan: {tautan[:6]}")
