#!/usr/bin/env python3
"""Baca seluruh teks beranda sampoernajayabaja.com + cari halaman lain + periksa teknologi & kontak di dalam halaman."""
import html, json, re, time, urllib.parse, urllib.request, urllib.error

UA = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0 Safari/537.36"}
DOM = "https://sampoernajayabaja.com"


def ambil(u, timeout=60):
    try:
        with urllib.request.urlopen(urllib.request.Request(u, headers=UA), timeout=timeout) as x:
            return x.status, x.read().decode("utf-8", "replace"), dict(x.headers)
    except urllib.error.HTTPError as e:
        return e.code, "", dict(e.headers)
    except Exception as e:
        return 0, str(e)[:100], {}


def teks(t):
    t = re.sub(r"<script.*?</script>|<style.*?</style>|<!--.*?-->", " ", t, flags=re.S | re.I)
    t = re.sub(r"</(p|div|li|h[1-6]|section|tr)>", "\n", t, flags=re.I)
    t = re.sub(r"<br\s*/?>", "\n", t, flags=re.I)
    t = html.unescape(re.sub(r"<[^>]+>", " ", t))
    return re.sub(r"[ \t]+", " ", t)


st, h, head = ambil(DOM + "/")
print("=== teknologi ===")
print("   header:", {k: v for k, v in list(head.items())[:10] if k.lower() in
                    ("server", "x-powered-by", "cf-cache-status", "content-type", "x-vercel-id", "x-nextjs-cache")})
print("   framework:", sorted(set(re.findall(r'(next|nuxt|astro|gatsby|react|vue|svelte|bootstrap|tailwind)[\.\-/]', h, re.I)))[:8])
print("   file js/css:", sorted(set(re.findall(r'(?:src|href)="([^"]+\.(?:js|css))"', h)))[:8])

print("\n=== TEKS BERANDA ===")
t = teks(h)
baris = [b.strip() for b in t.split("\n") if b.strip()]
for b in baris[:90]:
    print("   " + b[:150])

print("\n=== halaman lain ===")
for p in ("/kontak", "/contact", "/tentang", "/about", "/layanan", "/services", "/produk", "/products",
          "/tentang-kami", "/hubungi-kami", "/blog", "/sitemap", "/en", "/id"):
    st2, isi2, _ = ambil(DOM + p, timeout=25)
    if st2 == 200 and len(isi2) > 1000:
        j = re.search(r"<title[^>]*>(.*?)</title>", isi2, re.S)
        print(f"   {p:<16} 200 | {len(isi2)} byte | {html.unescape(re.sub('<[^>]+>', '', j.group(1)))[:70] if j else '-'}")
    else:
        print(f"   {p:<16} {st2}")
