#!/usr/bin/env python3
"""Verifikasi 2 artikel baru StudioSoft: meta, FAQ schema, link internal, kategori, jadwal."""
import base64, html, json, re, urllib.request

S = "https://studiosoft.id"; UA = {"User-Agent": "Mozilla/5.0"}
AUTH = base64.b64encode(b"bambangbot:q26r RePo BVy6 pwU2 F3z7 ouyl").decode()


def get(p):
    r = urllib.request.Request(S + p, headers={"Authorization": f"Basic {AUTH}", **UA})
    with urllib.request.urlopen(r, timeout=150) as x:
        return json.loads(x.read())


for pid in (9120, 9122):
    p = get(f"/wp-json/wp/v2/posts/{pid}?context=edit&_fields=id,title,slug,status,date,link,meta,content,categories,tags,featured_media")
    raw = p["content"]["raw"]
    teks = html.unescape(re.sub(r"<[^>]+>", " ", re.sub(r"<script.*?</script>", " ", raw, flags=re.S)))
    kata = len([w for w in re.split(r"\s+", teks) if w])
    links = re.findall(r'href="(https://studiosoft\.id[^"]+)"', raw)
    faq_html = re.findall(r"<h3>(.*?)</h3>", raw)
    m = re.search(r'<script type="application/ld\+json">(.*?)</script>', raw, re.S)
    try:
        sch = json.loads(m.group(1)); n_sch = len(sch["mainEntity"]); sch_ok = True
    except Exception as e:
        sch_ok = False; n_sch = 0
    print(f"\n=== #{pid} {html.unescape(p['title']['rendered'])}")
    print(f"    slug     : /{p['slug']}/")
    print(f"    status   : {p['status']} | tayang {p['date']} (site time)")
    print(f"    kata     : {kata} (tanpa schema/CTA)")
    print(f"    fokus    : {p['meta'].get('_yoast_wpseo_focuskw')}")
    print(f"    meta desc: {len(p['meta'].get('_yoast_wpseo_metadesc',''))} karakter → {p['meta'].get('_yoast_wpseo_metadesc')}")
    print(f"    seo title: {len(p['meta'].get('_yoast_wpseo_title',''))} karakter → {p['meta'].get('_yoast_wpseo_title')}")
    print(f"    FAQ      : {len(faq_html)} pertanyaan tampil | schema valid={sch_ok} ({n_sch} entri)")
    print(f"    CTA WA   : {'ada' if 'ss-cta-wa' in raw else 'TIDAK ADA'}")
    print(f"    gambar   : {p['featured_media']}")
    print(f"    kategori : {p['categories']}")
    print(f"    link internal ({len(set(links))}):")
    for l in sorted(set(links)):
        print(f"       {l.replace(S, '')}")
