#!/usr/bin/env python3
"""Verifikasi halaman publik /cara-mengurus-sertifikat-tanah.htm sesudah perbaikan."""
import json, re, urllib.request

URL = "https://www.tslaw.id/cara-mengurus-sertifikat-tanah.htm"
H = {"User-Agent": "Mozilla/5.0"}


def get(u):
    req = urllib.request.Request(u, headers=H)
    try:
        with urllib.request.urlopen(req, timeout=120) as r:
            return r.status, r.read().decode("utf-8", "replace")
    except urllib.error.HTTPError as e:
        return e.code, ""


st, html = get(URL)
cek = {
    "url": URL,
    "status_http": st,
    "title": (re.search(r"<title>(.*?)</title>", html, re.S) or [None, ""])[1].strip(),
    "meta_description": (re.search(r'<meta name="description" content="(.*?)"', html, re.S) or [None, ""])[1].strip()[:200],
    "canonical": (re.search(r'<link rel="canonical" href="(.*?)"', html) or [None, ""])[1],
    "robots": (re.search(r'<meta name="robots" content="(.*?)"', html) or [None, ""])[1],
    "h1": re.findall(r"<h1[^>]*>(.*?)</h1>", html, re.S)[:2],
    "bagian_girik": "Tanah Girik, Letter C, dan Petok D" in html,
    "bagian_waris": "Mengurus Sertifikat Tanah Warisan" in html,
    "bagian_cek_online": "Cara Mengecek Status Pengurusan dan Kondisi Bidang Tanah" in html,
    "bagian_pajak": "Pajak yang Muncul saat Balik Nama Tanah" in html,
    "tombol_wa": html.count("wa.me/6281909222026"),
    "skema_faqpage": html.count("FAQPage"),
    "tautan_htm": html.count("cara-mengurus-sengketa-tanah-secara-hukum.htm"),
    "tautan_lama_404": html.count("cara-mengurus-sengketa-tanah-secara-hukum/"),
    "h2": len(re.findall(r"<h2", html)),
}
# tautan internal yang diperbaiki harus hidup
st2, _ = get("https://www.tslaw.id/cara-mengurus-sengketa-tanah-secara-hukum.htm")
cek["tautan_baca_juga_status"] = st2
json.dump(cek, open("/home/mantapman/.hermes/profiles/atun/workspace/tslaw_pemulihan/perbaikan_20261011/live_3228_verifikasi.json", "w"),
          ensure_ascii=False, indent=1)
print(json.dumps(cek, ensure_ascii=False, indent=1))
