#!/usr/bin/env python3
"""Ambil isi artikel kandidat TSLaw 19 Okt (snapshot sebelum diubah)."""
import base64, json, os, sys, urllib.request

DOM = "tslaw.id"
AUTH = base64.b64encode(b"bambangbot:EFKm EYGx VQCP Y9go ncPr uM2Y").decode()
DIR = "/home/mantapman/.hermes/profiles/atun/workspace/tslaw_pemulihan/perbaikan_20261019"
os.makedirs(DIR, exist_ok=True)

pid = sys.argv[1]
r = urllib.request.Request(
    f"https://{DOM}/wp-json/wp/v2/posts/{pid}?context=edit",
    headers={"Authorization": f"Basic {AUTH}", "User-Agent": "Mozilla/5.0"},
)
with urllib.request.urlopen(r, timeout=90) as x:
    p = json.loads(x.read())

json.dump(p, open(f"{DIR}/post_{pid}_SEBELUM.json", "w"), ensure_ascii=False, indent=1)
raw = p["content"]["raw"]
open(f"{DIR}/post_{pid}_SEBELUM.html", "w").write(raw)
m = p.get("meta") or {}
print("id", p["id"], "| status", p["status"], "| slug", p["slug"])
print("judul:", p["title"]["raw"])
print("link:", p["link"])
print("fokus:", m.get("_yoast_wpseo_focuskw"))
print("metadesc:", m.get("_yoast_wpseo_metadesc"))
print("chars:", len(raw))
