#!/usr/bin/env python3
"""Ambil daftar artikel studiosoft.id lewat proxy r.jina.ai (host ini HTTPS-nya hang).

Output: /tmp/ss_articles.json  -> [{"id":..,"date":..,"slug":..,"link":..}, ...]
Pakai: python3 fetch_ss_articles.py [per_page]
"""
import json
import re
import subprocess
import sys

PER = sys.argv[1] if len(sys.argv) > 1 else "50"
URL = f"https://r.jina.ai/https://studiosoft.id/wp-json/wp/v2/posts?per_page={PER}&_fields=id,date,slug,link,title"

raw = ""
for attempt in range(4):
    p = subprocess.run(["curl", "-s", "--max-time", "180", URL], capture_output=True, text=True)
    raw = p.stdout
    i = raw.find('[{"id"')
    if i < 0:
        i = raw.find('[{"')
    if i >= 0:
        j = raw.rfind('}]')
        blob = raw[i:j + 2]
        try:
            data = json.loads(blob)
            break
        except Exception as e:
            print(f"attempt {attempt+1}: parse gagal ({e})")
    else:
        print(f"attempt {attempt+1}: JSON belum ada (len={len(raw)})")
    data = None

if not data:
    print("GAGAL ambil artikel. Cuplikan:", raw[:400])
    sys.exit(1)

json.dump(data, open("/tmp/ss_articles.json", "w"), indent=1)
print(f"{len(data)} artikel → /tmp/ss_articles.json")
for a in data[:8]:
    print(" ", a["date"][:16], a["slug"])
