#!/usr/bin/env python3
"""hc61 fetch artikel: Threads=cara-klinik-kecantikan..., LI=tools-ai-untuk-bisnis..., FB=rs-klinik-..."""
import json, re, subprocess, sys, time, html
WS = "/home/mantapman/.hermes/profiles/atun/workspace"

SLUGS = {
    "threads": "cara-klinik-kecantikan-di-surabaya-bisa-ditemukan-lebih-cepat-di-google",
    "linkedin": "tools-ai-untuk-bisnis-pilih-sesuai-kebutuhan",
    "facebook": "rs-klinik-digital-marketing-2026",
}
cb = str(int(time.time()))
for k, s in SLUGS.items():
    u = ("https://studiosoft.id/wp-json/wp/v2/posts?slug=%s&_fields=id,date,link,title,content&cb=%s" % (s, cb))
    r = subprocess.run(["curl", "-4", "-s", "--http1.1", "--max-time", "120",
                        "-A", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)", u], capture_output=True, text=True)
    try:
        p = json.loads(r.stdout)
    except Exception as e:
        print("GAGAL", k, e, r.stdout[:200]); continue
    if not p:
        print("KOSONG", k, s); continue
    p = p[0]
    print("=" * 70)
    print("%s: id=%s date=%s" % (k, p["id"], p["date"]))
    print("  slug:", s)
    print("  link:", p["link"])
    print("  title:", html.unescape(re.sub("<[^>]+>", "", p["title"]["rendered"])))
    body = html.unescape(re.sub("<[^>]+>", " ", p["content"]["rendered"]))
    body = re.sub(r"\s+", " ", body).strip()
    print("  body(1200):", body[:1200])
    print("  body len:", len(body))
    json.dump({"slug": s, "id": p["id"], "link": p["link"],
               "title": html.unescape(re.sub("<[^>]+>", "", p["title"]["rendered"])),
               "body": body}, open("/tmp/hc61_%s.json" % k, "w"))
