#!/usr/bin/env python3
"""Lihat respons mentah endpoint tag TSC untuk tahu kenapa kosong."""
import base64, importlib.util, json, urllib.error, urllib.request

WS = "/home/mantapman/.hermes/profiles/atun/workspace"
spec = importlib.util.spec_from_file_location("cfg", f"{WS}/cfg_tsc.py")
m = importlib.util.module_from_spec(spec)
spec.loader.exec_module(m)
CFG = m.CFG
AUTH = base64.b64encode(f"{CFG['user']}:{CFG['pass']}".encode()).decode()
h = {"Authorization": f"Basic {AUTH}", "User-Agent": "Mozilla/5.0"}

for path in ["/wp-json/wp/v2/tags?per_page=5",
             "/wp-json/wp/v2/categories?per_page=5",
             "/wp-json/wp/v2/tags?per_page=5&post=1735"]:
    r = urllib.request.Request("https://" + CFG["domain"] + path, headers=h)
    try:
        with urllib.request.urlopen(r, timeout=90) as x:
            d = json.loads(x.read())
        print(f"{path} → HTTP {x.status} · {len(d) if isinstance(d, list) else 'bukan list'}")
        if isinstance(d, list):
            for t in d[:3]:
                print("    ", t.get("id"), t.get("name"), "|", t.get("slug"))
    except urllib.error.HTTPError as e:
        print(f"{path} → HTTP {e.code} {e.read()[:160]}")
    except Exception as e:
        print(f"{path} → {str(e)[:80]}")
