#!/usr/bin/env python3
"""TSC: baca artikel tipis berikutnya (batch 2) untuk bahan memperpanjang."""
import base64, html, json, os, re, time, urllib.request, urllib.error, 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)
UA = {"User-Agent": "Mozilla/5.0"}
AUTH = base64.b64encode(f'{m.CFG["user"]}:{m.CFG["pass"]}'.encode()).decode()

# ambil dari data audit: artikel tipis berimpresi yang belum digarap
data = json.load(open(f"{WS}/drafts/tsc_tipis_urutan.json"))
kandidat = [a for a in data["berimpresi"] if a["slug"] not in
            ("kenapa-dr-avril-the-skin-code-clinic", "dokter-kulit-kelamin-sidoarjo")][:4]

spec2 = None
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"<[^>]+>", " ", raw)))
    print(f"\n{'='*90}")
    print(f"### #{pid} /{d['slug']}  ({a['kata']} kata | imp {a['imp']} | pos {a['pos']})")
    print(f"   judul: {html.unescape(d['title']['rendered'])}")
    print(f"   heading: {[html.unescape(re.sub(r'<[^>]+>', '', x)).strip()[:52] for x in re.findall(r'<h[23][^>]*>(.*?)</h[23]>', raw, re.S)]}")
    print(f"   awal: {teks[:260]}")
    print(f"   akhir: {teks[-260:]}")
    tautan = sorted(set(re.findall(r'href="https://www\.theskincodeclinic\.com(/[^"]*)"', raw)))
    print(f"   tautan: {tautan[:6]}")
