#!/usr/bin/env python3
"""Verifikasi perbaikan 3057 (tslaw 2026-10-07): baca ulang dari REST + halaman live."""
import base64, html as htmlmod, json, re, sys, urllib.request
WS = "/home/mantapman/.hermes/profiles/atun/workspace"
sys.path.insert(0, WS)
from cfg_tslaw import CFG

AUTH = base64.b64encode(f"{CFG['user']}:{CFG['pass']}".encode()).decode()
HDR = {"Authorization": f"Basic {AUTH}", "User-Agent": "Mozilla/5.0"}
OUT = f"{WS}/tslaw_pemulihan/perbaikan_20261007"


def wc(s):
    t = htmlmod.unescape(re.sub(r"<(script|style).*?</\1>", " ", s, flags=re.S))
    return len(re.sub(r"<[^>]+>", " ", t).split())


req = urllib.request.Request("https://tslaw.id/wp-json/wp/v2/posts/3057?context=edit", headers=HDR)
with urllib.request.urlopen(req, timeout=150) as r:
    p = json.load(r)
raw = p["content"]["raw"]
m = p.get("meta") or {}
json.dump(p, open(f"{OUT}/post_3057_POST_REST.json", "w"), ensure_ascii=False, indent=1)

h3_faq = re.findall(r"<h3>(.*?)</h3>", raw)
jml_q = raw.count('"@type": "Question"')
v = {
    "id": 3057, "slug": p["slug"], "status": p["status"], "modified": p["modified"],
    "judul_h1": p["title"]["raw"],
    "seo_title": m.get("_yoast_wpseo_title"), "fokus_kw": m.get("_yoast_wpseo_focuskw"),
    "meta_desc": m.get("_yoast_wpseo_metadesc"),
    "kata_audit_sebelum": 562, "kata_sebelum_cek_ulang": None, "kata_sesudah": wc(raw),
    "h2": len(re.findall(r"<h2", raw)), "h3_berkelas": len(re.findall(r'<h3 class="wp-block-heading"', raw)),
    "faq_tampil": len(h3_faq), "schema_question": jml_q, "schema_faqpage": raw.count('"@type": "FAQPage"'),
    "tautan_pilar_utf": raw.count("https://www.tslaw.id/law-firm-surabaya/"),
    "tombol_wa": raw.count("wa.me/6281909222026"),
    "tslaw_sisa": raw.lower().count("tslaw"), "kamu_sisa": len(re.findall(r"\bkamu\b", raw, flags=re.I)),
    "h2_terpotong": raw.count("sudah ma.") + raw.count("memberikan ."),
    "p_bersarang": len(re.findall(r"<p>[^<]*<p>", raw)) + raw.count("</p></p>"), "p_kosong": raw.count("<p></p>"),
    "placeholder": re.findall(r"\{[A-Za-z_\[\]0-9]+\}", raw),
}
print(json.dumps(v, ensure_ascii=False, indent=1))

# halaman live (tidak pakai cache browser)
req_live = urllib.request.Request(f"https://www.tslaw.id/{p['slug']}/",
                                  headers={"User-Agent": "Mozilla/5.0", "Cache-Control": "no-cache"})
with urllib.request.urlopen(req_live, timeout=150) as r:
    live = r.read().decode("utf-8", "ignore")
open(f"{OUT}/live_3057_sesudah.html", "w").write(live)
cek_live = {
    "h1_live": bool(re.search(r"Pengacara Surabaya Terbaik: 7 Hal", live)),
    "seo_title_live": bool(re.search(r"<title[^>]*>Pengacara Surabaya Terbaik", live)),
    "meta_desc_live": "dinilai dari izin praktik" in live,
    "faq_live": live.count('"@type": "FAQPage"'),
    "wa_live": "wa.me/6281909222026" in live,
    "pilar_live": "https://www.tslaw.id/law-firm-surabaya/" in live,
    "tslaw_live": live.count("TSLaw"),
}
print(json.dumps(cek_live, ensure_ascii=False, indent=1))
v["live"] = cek_live
json.dump(v, open(f"{OUT}/post_3057_VERIFIKASI.json", "w"), ensure_ascii=False, indent=1)
print("\n-> tslaw_pemulihan/perbaikan_20261007/")
