#!/usr/bin/env python3
"""Ambil daftar artikel (link+title+date) dari WP REST ke-4 situs via openssl (TLS DPI workaround)."""
import json, re, sys
sys.path.insert(0, "/home/mantapman/.hermes/profiles/atun/workspace")
from openssl_fetch import fetch

SITES = {
    "studiosoft": "studiosoft.id",
    "tslaw": "www.tslaw.id",
    "tsc": "www.theskincodeclinic.com",
    "das": "dasindonesia.com",
}
OUT = "/home/mantapman/.hermes/profiles/atun/workspace/_cache_artikel_wp_20okt.json"


def strip_chunked(b: bytes) -> bytes:
    """Decode transfer-encoding chunked: gabungkan semua chunk (perbaikan 17 Okt 2026).

    Versi lama cuma membuang baris ukuran pertama → JSON multi-chunk bikin
    `Extra data: line 2 column 1` dan daftar artikel jadi kosong.
    """
    if not re.match(rb"^[0-9a-fA-F]{1,7}\r\n", b):
        return b  # bukan chunked
    out = bytearray()
    i = 0
    while i < len(b):
        j = b.find(b"\r\n", i)
        if j == -1:
            break
        try:
            size = int(b[i:j].split(b";")[0].strip(), 16)
        except ValueError:
            break
        if size == 0:
            break
        out += b[j + 2:j + 2 + size]
        i = j + 2 + size + 2
    return bytes(out)


hasil = {}
for key, host in SITES.items():
    items = []
    for page in range(1, 6):
        st, head, body = fetch(f"https://{host}/wp-json/wp/v2/posts?per_page=100&page={page}&_fields=link,title,date")
        if st != 200:
            print(f"{key} page {page}: status {st}", flush=True)
            break
        try:
            arr = json.loads(strip_chunked(body).decode("utf-8", "replace"))
        except Exception as e:
            print(f"{key} page {page}: parse gagal {e} :: {body[:120]}", flush=True)
            break
        if not isinstance(arr, list) or not arr:
            break
        for x in arr:
            items.append({
                "link": x.get("link"),
                "title": (x.get("title") or {}).get("rendered") if isinstance(x.get("title"), dict) else x.get("title"),
                "date": x.get("date"),
            })
        if len(arr) < 100:
            break
    hasil[key] = items
    print(f"{key}: {len(items)} artikel", flush=True)

json.dump(hasil, open(OUT, "w"), ensure_ascii=False, indent=1)
print("saved ->", OUT)
