#!/usr/bin/env python3
"""Verifikasi publik #2928 setelah perbaikan: HTML 200, H2 baru, FAQ, skema FAQPage,
tombol WA, tautan, meta description, judul tidak berubah."""
import json, re, time, urllib.request, urllib.error

URL = "https://www.tslaw.id/7-langkah-hukum-melaporkan-pencemaran-nama-baik-di-media-sosial/"
UA = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/124 Safari/537.36"}
FOLDER = "/home/mantapman/.hermes/profiles/atun/workspace/tslaw_pemulihan/perbaikan_20261025"

u = f"{URL}?nocache={int(time.time())}"
req = urllib.request.Request(u, headers=UA)
with urllib.request.urlopen(req, timeout=120) as x:
    st, h = x.status, x.read().decode("utf-8", "replace")
print("HTTP", st, "| bytes", len(h))
open(f"{FOLDER}/halaman_2928_sesudah.html", "w").write(h)

print("\njudul halaman:", re.search(r"<title>(.*?)</title>", h, re.S).group(1).strip()[:110])
md = re.search(r'<meta name="description" content="([^"]*)"', h)
print("meta description:", (md.group(1) if md else "-")[:160])

h2 = re.findall(r"<h2[^>]*>(.*?)</h2>", h, re.S)
h2 = [re.sub(r"<[^>]+>", "", t).strip() for t in h2]
print(f"\nH2 tampil ({len(h2)}):")
for t in h2:
    print("  -", t[:80])

wajib = ["Dasar Hukum Pencemaran Nama Baik Setelah KUHP Baru Berlaku",
         "Apa yang Berubah pada 2 Januari 2026", "Unsur yang Harus Terbukti",
         "Jalur Perdata untuk Ganti Rugi", "Pertanyaan yang Sering Diajukan"]
print("\nH2 wajib ada:", {w: (w in h2) for w in wajib})

print("\nPasal 433 muncul:", h.count("Pasal 433"), "| Pasal 434:", h.count("Pasal 434"),
      "| Pasal 27A:", h.count("Pasal 27A"), "| 'Pasal 310 KUHP lama':", h.count("Pasal 310 KUHP lama"))
print("tombol WA 6281909222026:", len(re.findall(r"wa\.me/6281909222026", h)))
print("tautan pilar law-firm-surabaya:", len(re.findall(r"law-firm-surabaya", h)))

sk = re.findall(r'<script type="application/ld\+json">(.*?)</script>', h, re.S)
faq = []
for s in sk:
    try:
        d = json.loads(s)
    except Exception:
        continue
    if isinstance(d, dict) and d.get("@type") == "FAQPage":
        faq.append(d)
print("\nblok ld+json:", len(sk), "| FAQPage valid:", len(faq))
if faq:
    items = faq[0]["mainEntity"]
    print("  item skema:", len(items))
    tampil = re.findall(r"<h3[^>]*>(.*?)</h3>", h, re.S)
    tampil = [re.sub(r"<[^>]+>", "", t).strip() for t in tampil]
    print("  h3 tampil:", len(tampil))
    for it in items:
        print("   ", "OK " if it["name"] in tampil else "TIDAK TAMPIL", it["name"][:70])
    print("  skema == 7:", len(items) == 7)

links = sorted(set(re.findall(r'href="(https://(?:www\.)?tslaw\.id[^"]+)"', h)))
print(f"\ntautan internal di halaman: {len(links)}")
for l in links:
    try:
        with urllib.request.urlopen(urllib.request.Request(l, headers=UA), timeout=60) as y:
            s = y.status
    except urllib.error.HTTPError as e:
        s = e.code
    except Exception:
        s = 0
    if s != 200:
        print("   MATI", s, l)
print("selesai cek tautan")
