#!/usr/bin/env python3
"""Ambil judul + isi artikel via WP REST dengan RETRY (host ini sering flaky).
Usage: python3 fetch_isi_retry.py <url> [<url> ...]
"""
import json, re, subprocess, sys, time

URL_RE = re.compile(r"<[^>]+>")


def clean(h):
    h = re.sub(r"<(script|style)[^>]*>.*?</\1>", " ", h, flags=re.S | re.I)
    h = URL_RE.sub(" ", h)
    for a, b in [("&#8217;", "'"), ("&#8216;", "'"), ("&#038;", "&"), ("&#8211;", "-"),
                 ("&nbsp;", " "), ("&amp;", "&"), ("&#8220;", '"'), ("&#8221;", '"'), ("&quot;", '"')]:
        h = h.replace(a, b)
    return re.sub(r"\s+", " ", h).strip()


def get(api, tries=6):
    for i in range(tries):
        r = subprocess.run(["curl", "-s", "--http1.1", "--max-time", "45", api],
                           capture_output=True, text=True, timeout=60)
        raw = r.stdout
        try:
            return json.loads(raw)
        except Exception:
            time.sleep(3)
    return None


for url in sys.argv[1:]:
    p = url.rstrip("/").split("/")
    slug = p[-1]
    root = "/".join(url.split("/")[:3])
    api = "%s/wp-json/wp/v2/posts?slug=%s&_fields=title,link,date,content" % (root, slug)
    posts = get(api)
    print("=" * 78)
    if not posts:
        print("GAGAL ambil: %s" % url)
        continue
    a = posts[0]
    body = clean(a["content"]["rendered"])
    print("URL  : %s" % a["link"])
    print("JUDUL: %s" % clean(a["title"]["rendered"]))
    print("TGL  : %s" % a["date"])
    print("ISI  : %s" % body[:2600])
