#!/usr/bin/env python3
"""Ambil isi 10 artikel klaster klinik/RS (23 Jul) untuk direncanakan konsolidasinya."""
import base64, html, json, re, time, urllib.request

S = "https://studiosoft.id"
AUTH = base64.b64encode(b"bambangbot:q26r RePo BVy6 pwU2 F3z7 ouyl").decode()
IDS = [5970, 5971, 5972, 5973, 5974, 5975, 5976, 5977, 5978, 5979]


def api(path):
    h = {"Authorization": f"Basic {AUTH}", "User-Agent": "Mozilla/5.0"}
    r = urllib.request.Request(S + path, headers=h)
    err = None
    for _ in range(4):
        try:
            with urllib.request.urlopen(r, timeout=240) as x:
                return json.loads(x.read())
        except Exception as ex:
            err = ex; time.sleep(5)
    print("gagal:", str(err)[:60]); return None


hasil = []
for pid in IDS:
    p = api(f"/wp-json/wp/v2/posts/{pid}?context=edit&_fields=id,link,title,content,meta")
    if not isinstance(p, dict):
        print(f"  #{pid} gagal"); continue
    raw = p["content"]["raw"]
    teks = html.unescape(re.sub(r"<[^>]+>", " ", raw))
    kata = len(teks.split())
    h2 = [html.unescape(re.sub("<[^>]+>", "", h)).strip() for h in re.findall(r"<h2[^>]*>(.*?)</h2>", raw, re.S)]
    hasil.append({"id": pid, "judul": html.unescape(p["title"]["rendered"]), "link": p["link"], "kata": kata,
                  "h2": h2, "fk": p["meta"].get("_yoast_wpseo_focuskw", ""),
                  "meta": p["meta"].get("_yoast_wpseo_metadesc", ""), "ringkas": re.sub(r"\s+", " ", teks)[:400]})

json.dump(hasil, open("ss_klaster_klinik.json", "w"), indent=1, ensure_ascii=False)
for x in hasil:
    print("=" * 100)
    print(f"#{x['id']} | {x['kata']} kata | {x['judul']}")
    print("   H2:", " // ".join(h[:36] for h in x['h2'][:8]))
    print("   ISI:", x['ringkas'][:230])
