#!/usr/bin/env python3
"""Verifikasi LIVE meta description artikel LEIT (plain vs ?nocache) + skor Yoast ulang."""
import base64, json, re, urllib.request, urllib.error

SITE = "https://www.leitsurabaya.com"
AUTH = base64.b64encode(b"bambangbot:zibd dLIV xY8x onP9 39qL hiNr").decode()
URLS = [
    "/bahasa-inggris-untuk-anak-metode-seru/",
    "/kursus-bahasa-inggris-paling-terjangkau-surabaya/",
    "/kursus-bahasa-inggris-surabaya-timur/",
    "/toefl-vs-ielts-mana-yang-lebih-cocok-untuk-kamu/",
]


def fetch(url):
    r = urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0", "Cache-Control": "no-cache"})
    with urllib.request.urlopen(r, timeout=90) as x:
        return x.read().decode("utf-8", "replace")


def meta(html):
    m = re.search(r'<meta name="description" content="([^"]*)"', html)
    return m.group(1) if m else None


for u in URLS:
    try:
        plain = fetch(SITE + u)
    except urllib.error.HTTPError as e:
        print(u, "HTTP", e.code); continue
    noc = fetch(SITE + u + "?nocache=1")
    d1, d2 = meta(plain), meta(noc)
    print(f"{u}\n   plain  : {len(d1) if d1 else 0} char | {d1[:80] if d1 else 'TIDAK ADA'}")
    print(f"   nocache: {len(d2) if d2 else 0} char | {d2[:80] if d2 else 'TIDAK ADA'}")

# skor Yoast setelah perbaikan
def api(p):
    r = urllib.request.Request(SITE + p, headers={"Authorization": f"Basic {AUTH}", "User-Agent": "Mozilla/5.0"})
    with urllib.request.urlopen(r, timeout=120) as x:
        return json.loads(x.read())


print("\nSEO   :", [(s["name"], s["amount"]) for s in api("/wp-json/yoast/v1/seo_scores?contentType=post")["scores"]])
print("Read  :", [(s["name"], s["amount"]) for s in api("/wp-json/yoast/v1/readability_scores?contentType=post")["scores"]])
