#!/usr/bin/env python3
"""Coba set Yoast social (Open Graph) image post 4147 via bulk_editor/update_social."""
import base64, json, urllib.request, urllib.error

SITE = "https://www.leitsurabaya.com"
AUTH = base64.b64encode(b"bambangbot:zibd dLIV xY8x onP9 39qL hiNr").decode()
URL = "https://www.leitsurabaya.com/wp-content/uploads/2026/09/bahasa-inggris-untuk-anak-metode-seru-pexels-1000x567-1.webp"


def req(method, path, 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(SITE + path, data=body, headers=h, method=method)
    try:
        with urllib.request.urlopen(r, timeout=120) as resp:
            return resp.status, resp.read().decode()[:400]
    except urllib.error.HTTPError as e:
        return e.code, e.read().decode()[:400]


for items in (
    [{"id": 4147, "opengraph-image": URL, "opengraph-image-id": 4216}],
    [{"id": 4147, "open_graph_image": URL, "open_graph_image_id": 4216}],
    [{"id": 4147, "opengraph_image": URL}],
):
    st, body = req("POST", "/wp-json/yoast/v1/bulk_editor/update_social", {"items": items})
    print(json.dumps(items[0])[:90], "->", st, body[:160])
    if st == 200:
        break

st, body = req("GET", "/wp-json/wp/v2/posts/4147?context=edit&_fields=yoast_head_json")
try:
    print("og_image now:", json.dumps(json.loads(body)["yoast_head_json"].get("og_image"), ensure_ascii=False))
except Exception:
    print("verify:", st, body[:200])
