#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""hc68_fetch.py — tarik isi artikel by slug dari studiosoft.id (WP REST)."""
import json, re, subprocess, sys, html

SLUGS = sys.argv[1:] or ["website-rumah-sakit-gagal-pasien",
                         "biaya-digital-agency-surabaya",
                         "cara-klinik-kecantikan-di-surabaya-bisa-ditemukan-lebih-cepat-di-google"]

for slug in SLUGS:
    r = subprocess.run(["curl", "-4", "-s", "--http1.1", "--max-time", "150",
                        "-A", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)",
                        "https://studiosoft.id/wp-json/wp/v2/posts?slug=%s&_fields=id,date,slug,title,content,excerpt" % slug],
                       capture_output=True, text=True)
    try:
        d = json.loads(r.stdout)
    except Exception as e:
        print("GAGAL", slug, e, r.stdout[:200])
        continue
    if not d:
        print("KOSONG", slug)
        continue
    p = d[0]
    print("=" * 100)
    print("ID", p["id"], "|", p["date"], "|", slug)
    print("TITLE:", html.unescape(re.sub("<[^>]+>", "", p["title"]["rendered"])))
    txt = re.sub(r"<(script|style)[^>]*>.*?</\1>", " ", p["content"]["rendered"], flags=re.S | re.I)
    txt = re.sub(r"<h([1-6])[^>]*>", lambda m: "\n\n## ", txt)
    txt = re.sub(r"</h[1-6]>", "\n", txt)
    txt = re.sub(r"<li[^>]*>", "\n- ", txt)
    txt = re.sub(r"<p[^>]*>", "\n\n", txt)
    txt = re.sub(r"<br\s*/?>", "\n", txt)
    txt = re.sub(r"<[^>]+>", "", txt)
    txt = html.unescape(txt)
    txt = re.sub(r"\n{3,}", "\n\n", txt)
    print(txt.strip())
    print()
