import base64, json, urllib.request, time
WP_URL = "https://yasmanatura.co.id"
WP_AUTH = base64.b64encode(b"bambangbot:LFp4 LXBh kMYG 84qC 2DDc 5bLc").decode()
POST_ID = 4532
MEDIA_ID = 4541
ALT = "Ilustrasi maklon minuman serbuk dengan bahan herbal alami Yasma Natura"

def req(method, path, body=None, ctype=None, timeout=60, tries=3):
    for i in range(tries):
        try:
            headers = {"Authorization": f"Basic {WP_AUTH}", "User-Agent":"Mozilla/5.0"}
            if ctype: headers["Content-Type"] = ctype
            r = urllib.request.Request(f"{WP_URL}{path}", data=body, headers=headers, method=method)
            with urllib.request.urlopen(r, timeout=timeout) as resp:
                return json.loads(resp.read())
        except Exception as e:
            print(f"   retry {i+1}: {e}")
            time.sleep(8)
    return None

# alt text
print("Set alt text...")
r = req("POST", f"/wp-json/wp/v2/media/{MEDIA_ID}", json.dumps({"alt_text": ALT}).encode(), "application/json")
print("   ->", "OK" if r else "GAGAL")

# featured media
print("Set featured image post...")
r2 = req("POST", f"/wp-json/wp/v2/posts/{POST_ID}", json.dumps({"featured_media": MEDIA_ID}).encode(), "application/json")
if r2:
    print(f"   -> Post #{POST_ID} featured_media = {r2.get('featured_media')}")
else:
    print("   -> GAGAL")
print("SELESAI")
