#!/usr/bin/env python3
"""Cari id /tone-up-body-lotion/ (post atau page) + cek meta Yoast-nya sekarang."""
import base64, html, json, re, sys, time, urllib.request, importlib.util

WS = "/home/mantapman/.hermes/profiles/atun/workspace"
spec = importlib.util.spec_from_file_location("cfg_everybe", f"{WS}/cfg_everybe.py")
m = importlib.util.module_from_spec(spec); spec.loader.exec_module(m)
CFG = m.CFG
DOM = CFG.get("domain", "www.everybeofficial.com")
AUTH = base64.b64encode(f'{CFG["user"]}:{CFG["pass"]}'.encode()).decode()
UA = {"User-Agent": "Mozilla/5.0"}


def rest(p):
    r = urllib.request.Request(f"https://{DOM}{p}", headers={"Authorization": f"Basic {AUTH}", **UA})
    for _ in range(3):
        try:
            with urllib.request.urlopen(r, timeout=120) as x:
                return json.loads(x.read())
        except Exception:
            time.sleep(3)
    return []


for jenis in ("posts", "pages", "product"):
    d = rest(f"/wp-json/wp/v2/{jenis}?slug=tone-up-body-lotion&context=edit&_fields=id,slug,title,link,status")
    if isinstance(d, list) and d:
        print(f"   ditemukan di {jenis}: #{d[0]['id']} | {d[0]['status']} | {d[0]['link']}")
        print(f"      judul: {html.unescape(re.sub('<[^>]+>', '', d[0]['title']['raw']))[:90]}")
    else:
        print(f"   {jenis}: tidak ada")

print("\n=== cari semua yang slugnya memuat 'tone' atau 'lotion' ===")
d = rest("/wp-json/wp/v2/posts?search=lotion&per_page=10&context=edit&_fields=id,slug,title,link")
for p in (d if isinstance(d, list) else []):
    print(f"   #{p['id']} /{p['slug'][:50]} | {html.unescape(re.sub('<[^>]+>','',p['title']['raw']))[:60]}")
d2 = rest("/wp-json/wp/v2/pages?per_page=50&context=edit&_fields=id,slug,title")
print("\n=== semua halaman EveryBe ===")
for p in (d2 if isinstance(d2, list) else []):
    print(f"   #{p['id']} /{p['slug'][:40]} | {html.unescape(re.sub('<[^>]+>','',p['title']['raw']))[:50]}")
