#!/usr/bin/env python3
"""1) Buang 3 duplikat draft. 2) Ambil URL artikel AI/SEO untuk link internal."""
import base64, json, urllib.request, urllib.error

S = "https://studiosoft.id"
AUTH = base64.b64encode(b"bambangbot:q26r RePo BVy6 pwU2 F3z7 ouyl").decode()


def wp(path, method="GET", data=None):
    h = {"Authorization": f"Basic {AUTH}", "User-Agent": "Mozilla/5.0"}
    body = None
    if data is not None:
        body = json.dumps(data).encode(); h["Content-Type"] = "application/json"
    r = urllib.request.Request(S + path, data=body, headers=h, method=method)
    try:
        with urllib.request.urlopen(r, timeout=200) as x:
            return x.status, json.loads(x.read())
    except urllib.error.HTTPError as e:
        return e.code, e.read().decode()[:200]


print("=== 1. Buang duplikat ===")
for pid in (7548, 7549, 7550):
    st, res = wp(f"/wp-json/wp/v2/posts/{pid}", method="DELETE")
    st2, chk = wp(f"/wp-json/wp/v2/posts/{pid}?context=edit&_fields=status")
    print(f"  #{pid} DELETE {st} | status sekarang: {chk.get('status') if isinstance(chk, dict) else chk}")

print("\n=== 2. URL artikel klaster AI & SEO (untuk link internal) ===")
ids = [7007, 6999, 6997, 6993, 7329, 8021, 8023, 8025, 8027, 8029, 7331, 8067, 7857, 7855]
q = "&".join(f"include%5B%5D={i}" for i in ids)
st, d = wp(f"/wp-json/wp/v2/posts?per_page=20&{q}&_fields=id,link,title")
for p in sorted(d, key=lambda x: x["id"]):
    print(f"  #{p['id']:>5} {p['link']}")
print("\n=== halaman terkait ===")
for slug in ("konsultan-ai-surabaya", "jasa-seo-professional", "layanan", "portfolio"):
    st, pg = wp(f"/wp-json/wp/v2/pages?slug={slug}&_fields=id,link,title")
    if pg:
        print(f"  #{pg[0]['id']:>5} {pg[0]['link']}")
