#!/usr/bin/env python3
"""Potret beranda TSLaw: judul, H1/H2, schema, CTA, NAP (nama-alamat-telepon), berat halaman."""
import html, json, re, urllib.request

UA = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/124 Safari/537.36"}
h = urllib.request.urlopen(urllib.request.Request("https://www.tslaw.id/", headers=UA), timeout=120).read().decode("utf-8", "replace")

print("=== 1. judul & meta ===")
t = re.search(r"<title>(.*?)</title>", h, re.S)
d = re.search(r'<meta name="description" content="([^"]*)"', h)
print("   judul:", t.group(1) if t else "-")
print(f"   meta ({len(d.group(1)) if d else 0} char): {d.group(1)[:170] if d else '-'}")

print("\n=== 2. struktur heading ===")
for tag in ("h1", "h2", "h3"):
    isi = re.findall(rf"<{tag}[^>]*>(.*?)</{tag}>", h, re.S | re.I)
    bersih = [html.unescape(re.sub(r"\s+", " ", re.sub(r"<[^>]+>", "", x))).strip() for x in isi]
    bersih = [x for x in bersih if x]
    print(f"   {tag.upper()} ({len(bersih)}):")
    for x in bersih[:10]:
        print(f"      - {x[:78]}")

print("\n=== 3. berat halaman ===")
teks = html.unescape(re.sub(r"<[^>]+>", " ", re.sub(r"<(script|style)[^>]*>.*?</\1>", " ", h, flags=re.S | re.I)))
kata = len([w for w in re.split(r"\s+", teks) if w])
print(f"   ukuran HTML: {len(h)//1024} KB | jumlah kata (tanpa script/style): {kata}")
print(f"   <img>: {len(re.findall(r'<img', h))} | tanpa loading=lazy: "
      f"{len([m for m in re.findall(r'<img[^>]+>', h) if 'loading=' not in m])}")
n_css = len(re.findall(r"rel=[\"']stylesheet", h))
print(f"   script: {len(re.findall(r'<script', h))} | CSS: {n_css}")
print(f"   total <a>: {len(re.findall(r'<a ', h))}")

print("\n=== 4. CTA & kontak ===")
print("   tautan wa.me   :", sorted(set(re.findall(r'https?://wa\.me/(\d+)', h))) or "TIDAK ADA")
print("   tel: / telp    :", sorted(set(re.findall(r'href="tel:([^"]+)"', h)))[:5] or "TIDAK ADA")
print("   email          :", sorted(set(re.findall(r'mailto:([^"?]+)', h)))[:4] or "TIDAK ADA")
print("   kata 'konsultasi' muncul:", len(re.findall(r"konsultasi", teks, re.I)))
print("   peta/maps      :", "ADA" if "google.com/maps" in h else "TIDAK ADA")
print("   alamat Surabaya:", "ADA" if re.search(r"surabaya", teks, re.I) else "tidak")
print("   form           :", "ADA" if re.search(r"<form", h, re.I) else "TIDAK ADA")

print("\n=== 5. schema terpasang ===")
for m in re.finditer(r'<script type="application/ld\+json"[^>]*>(.*?)</script>', h, re.S):
    try:
        data = json.loads(m.group(1))
    except Exception:
        print("   (satu blok JSON-LD tidak valid)"); continue
    def tipe(x):
        if isinstance(x, dict):
            return x.get("@type")
        if isinstance(x, list):
            return [tipe(i) for i in x]
        return None
    print("   tipe:", tipe(data), "| kunci:", list(data.keys())[:8] if isinstance(data, dict) else "-")
    if isinstance(data, dict) and "@graph" in data:
        print("      @graph:", [tipe(g) for g in data["@graph"]])

print("\n=== 6. teks pembuka (200 kata pertama) ===")
print("  ", re.sub(r"\s+", " ", teks).strip()[:700])
