#!/usr/bin/env python3
"""Kumpulkan bahan konten Kendangsari Hospital dari situsnya: identitas, layanan, dokter, fasilitas, kanal."""
import json, re, time, urllib.error, urllib.request

UA = {"User-Agent": "Mozilla/5.0 (compatible; AtunBot/1.0)"}
DOM = "rsia.kendangsari.com"
WS = "/home/mantapman/.hermes/profiles/atun/workspace"


def ambil(url):
    try:
        with urllib.request.urlopen(urllib.request.Request(url, headers=UA), timeout=60) as x:
            return x.read().decode("utf-8", "replace")
    except Exception as e:
        return f"GAGAL {str(e)[:50]}"


# 1. sitemap → daftar halaman
sm = ambil(f"https://{DOM}/sitemap_index.xml")
sub = re.findall(r"<loc>([^<]+)</loc>", sm)
urls = []
for s in sub:
    t = ambil(s)
    urls += re.findall(r"<loc>([^<]+)</loc>", t)
urls = sorted(set(u for u in urls if u.startswith("http")))
print(f"URL dari sitemap: {len(urls)}")
json.dump({"urls": urls}, open(f"{WS}/drafts/kh_url.json", "w"), ensure_ascii=False, indent=1)

# 2. halaman penting
PENTING = ["", "tentang-kami", "layanan", "dokter", "fasilitas", "kontak", "rawat-inap", "igd",
           "poliklinik", "jadwal-dokter", "promo"]
halaman = {}
for p in PENTING:
    u = f"https://{DOM}/{p}"
    t = ambil(u if p else u + "/")
    if t.startswith("GAGAL"):
        print(f"  {u} → {t}")
        continue
    judul = re.search(r"<title>(.*?)</title>", t, re.S)
    h2 = [re.sub(r"<[^>]+>", "", x).strip() for x in re.findall(r"<h[23][^>]*>(.*?)</h[23]>", t, re.S)]
    teks = re.sub(r"\s+", " ", re.sub(r"<[^>]+>", " ", re.sub(r"<script.*?</script>|<style.*?</style>", " ", t, flags=re.S)))
    halaman[u] = {"judul": re.sub(r"\s+", " ", judul.group(1)).strip() if judul else "",
                  "h2": [x for x in h2 if x][:25], "teks": teks[:6000]}
    print(f"  {u} → {halaman[u]['judul'][:70]}")
    time.sleep(1)

json.dump(halaman, open(f"{WS}/drafts/kh_halaman.json", "w"), ensure_ascii=False, indent=1)

# 3. fakta kunci dari beranda
beranda = halaman.get(f"https://{DOM}/", {}).get("teks", "")
print("\n=== cuplikan fakta dari beranda ===")
for pola in (r"(?:alamat|jl\.|jalan)[^.]{0,120}", r"(?:telepon|telp|whatsapp|wa)[^.]{0,80}",
             r"\b\d{2}[.:]\d{2}\s*[-–]\s*\d{2}[.:]\d{2}\b", r"@[a-z0-9_.]{4,30}"):
    for m in sorted(set(re.findall(pola, beranda, re.I)))[:6]:
        print(f"   {m[:110]}")
