#!/usr/bin/env python3
"""hc43: ambil isi artikel via WP REST by slug -> /tmp/hc43_<slug>.json"""
import json, subprocess, sys, re, html

SLUGS = ["rs-klinik-digital-marketing-2026", "strategi-digital-marketing-rumah-sakit",
         "data-analytics-rumah-sakit-kebutuhan-pasien"]

for s in SLUGS:
    r = subprocess.run(["curl", "-s", "--http1.1", "--max-time", "150",
                        "https://studiosoft.id/wp-json/wp/v2/posts?slug=%s&_fields=date,slug,title,content,excerpt,link" % s],
                       capture_output=True, text=True)
    try:
        d = json.loads(r.stdout)
    except Exception as e:
        print("FETCH GAGAL", s, e, r.stdout[:200])
        continue
    if not d:
        print("KOSONG", s)
        continue
    p = d[0]
    json.dump(p, open("/tmp/hc43_%s.json" % s, "w"))
    raw = p["content"]["rendered"]
    txt = re.sub(r"<[^>]+>", " ", raw)
    txt = html.unescape(txt)
    txt = re.sub(r"\s+", " ", txt).strip()
    print("\n========== %s ==========" % s)
    print("TITLE:", p["title"]["rendered"])
    print("LINK:", p["link"])
    print("LEN:", len(txt))
    print(txt[:4000])
