#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Cek status artikel studiosoft.id by slug via WP REST (auth, context=edit)."""
import base64, json, sys, time, urllib.request, urllib.error

S = "https://studiosoft.id"
AUTH = base64.b64encode(b"bambangbot:q26r RePo BVy6 pwU2 F3z7 ouyl").decode()
slug = sys.argv[1] if len(sys.argv) > 1 else "bikin-konten-pakai-ai-boleh-asal-tahu-batasnya"
cb = str(int(time.time()))

def api(path):
    h = {"Authorization": f"Basic {AUTH}", "User-Agent": "Mozilla/5.0"}
    r = urllib.request.Request(S + path, headers=h, method="GET")
    try:
        with urllib.request.urlopen(r, timeout=120) as x:
            return x.status, x.read()
    except urllib.error.HTTPError as e:
        return e.code, e.read()[:300]
    except Exception as e:
        return 0, str(e).encode()

for q in (f"/wp-json/wp/v2/posts?slug={slug}&status=any&context=edit&cb={cb}",
          f"/wp-json/wp/v2/posts?slug={slug}&status=any&cb={cb}"):
    st, body = api(q)
    print("GET", q, "->", st)
    try:
        d = json.loads(body)
    except Exception:
        print("  body:", body[:200]); continue
    if not d:
        print("  (0 hasil)")
        continue
    for p in d:
        print("  id", p.get("id"), "| status", p.get("status"), "| date", p.get("date"),
              "| slug", p.get("slug"), "| link", p.get("link"))
    break
