#!/usr/bin/env python3
"""hc44: ambil isi artikel via WP REST by slug -> /tmp/hc44_<slug>.json + teks bersih."""
import json, subprocess, re, html

SLUGS = ["google-business-profile-klinik", "social-media-rumah-sakit-efektif",
         "konten-edukasi-kesehatan-kepercayaan-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[:300])
        continue
    if not d:
        print("KOSONG", s)
        continue
    p = d[0]
    json.dump(p, open("/tmp/hc44_%s.json" % s, "w"))
    raw = p["content"]["rendered"]
    txt = re.sub(r"<[^>]+>", " ", raw)
    txt = html.unescape(txt)
    txt = re.sub(r"\s+", " ", txt).strip()
    open("/tmp/hc44_%s.txt" % s, "w").write(txt)
    print("\n========== %s ==========" % s)
    print("TITLE:", p["title"]["rendered"])
    print("LINK:", p["link"])
    print("LEN:", len(txt))
    print(txt[:3500])
