#!/usr/bin/env python3
"""TSLaw: lihat markup mentah beranda (H1/H2) + halaman hubungi, dan bongkar email yang disamarkan Cloudflare."""
import base64, html, json, re, time, urllib.request, urllib.error

DOM = "tslaw.id"
AUTH = base64.b64encode(b"bambangbot:EFKm EYGx VQCP Y9go ncPr uM2Y").decode()


def wp(path):
    r = urllib.request.Request(f"https://{DOM}{path}", headers={"Authorization": f"Basic {AUTH}", "User-Agent": "Mozilla/5.0"})
    for _ in range(3):
        try:
            with urllib.request.urlopen(r, timeout=120) as x:
                return json.loads(x.read())
        except urllib.error.HTTPError as e:
            return {"__err": e.code, "pesan": e.read().decode()[:200]}
        except Exception:
            time.sleep(4)
    return {"__err": "gagal"}


print("=== 1. halaman depan (id 833) ===")
p = wp("/wp-json/wp/v2/pages/833?context=edit&_fields=id,title,content,meta,modified")
raw = p["content"]["raw"] if isinstance(p.get("content"), dict) else ""
print(f"   panjang konten mentah: {len(raw)} karakter")
print("   elemen h1 & h2 di konten:")
for m in re.finditer(r"<(h1|h2)\b[^>]*>.*?</\1>", raw, re.S | re.I):
    tag = re.sub(r"\s+", " ", m.group(0))
    print(f"     {tag[:190]}")

print("\n=== 2. halaman hubungi (id 10) ===")
p2 = wp("/wp-json/wp/v2/pages/10?context=edit&_fields=id,content")
raw2 = p2["content"]["raw"] if isinstance(p2.get("content"), dict) else ""
print(f"   panjang konten mentah: {len(raw2)} karakter")
i = raw2.lower().find("vestibulum")
print("   sekitar lorem ipsum:")
print("   ", re.sub(r"\s+", " ", raw2[max(0, i-300):i+400]))

print("\n=== 3. bongkar email yang disamarkan Cloudflare ===")
h = urllib.request.urlopen(urllib.request.Request(f"https://www.{DOM}/hubungi/", headers={"User-Agent": "Mozilla/5.0"}), timeout=90).read().decode("utf-8", "replace")
for m in re.finditer(r'<a[^>]*href="/cdn-cgi/l/email-protection#([0-9a-f]+)"[^>]*>(?:<span[^>]*>)?([^<]*)', h):
    heks, teks = m.group(1), m.group(2)
    try:
        b = bytes.fromhex(heks)
        email = "".join(chr(c ^ b[0]) for c in b[1:])
        print(f"   tersamar: {teks[:30]:<32} → asli: {email}")
    except Exception as e:
        print("   gagal bongkar:", str(e)[:60])
for m in re.finditer(r'data-cfemail="([0-9a-f]+)"', h):
    b = bytes.fromhex(m.group(1))
    print("   data-cfemail →", "".join(chr(c ^ b[0]) for c in b[1:]))

print("\n=== 4. nomor telepon & WhatsApp yang tampil di /hubungi/ ===")
teks = re.sub(r"\s+", " ", html.unescape(re.sub(r"<[^>]+>", " | ", h)))
for pola in (r"\+?\d[\d\s\-]{8,16}", r"wa\.me/\d+"):
    print("  ", sorted(set(re.findall(pola, teks)))[:10])
