#!/usr/bin/env python3
"""Simpan daftar artikel terbit studiosoft.id (id, slug, judul, link) ke cache."""
import base64, json, urllib.request

S = "https://studiosoft.id"
AUTH = base64.b64encode(b"bambangbot:q26r RePo BVy6 pwU2 F3z7 ouyl").decode()
H = {"Authorization": f"Basic {AUTH}", "User-Agent": "Mozilla/5.0"}
out = []
page = 1
while True:
    url = (f"{S}/wp-json/wp/v2/posts?status=publish&per_page=100&page={page}"
           f"&orderby=date&order=desc&_fields=id,slug,link,title,date")
    try:
        with urllib.request.urlopen(urllib.request.Request(url, headers=H), timeout=180) as r:
            batch = json.loads(r.read())
    except Exception as e:
        print("berhenti di halaman", page, type(e).__name__)
        break
    if not batch:
        break
    out += batch
    if len(batch) < 100:
        break
    page += 1
json.dump(out, open("/home/mantapman/.hermes/profiles/atun/workspace/cache/ss_posts_terbit.json", "w"))
print(f"artikel terbit tersimpan: {len(out)}")
