#!/usr/bin/env python3
"""Pastikan rsia.kendangsari.com (Kendangsari Hospital) vs www.rsiamerr.com (RSIA MERR) — dua entitas berbeda."""
import html, re, time, urllib.request, urllib.error

UA = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/124.0 Safari/537.36"}


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


def bersih(t):
    return html.unescape(re.sub(r"\s+", " ", re.sub(r"<[^>]+>", " ", t))).strip()


for u in ("https://rsia.kendangsari.com/", "https://www.rsiamerr.com/", "https://merr.kendangsari.com/"):
    st, h, detik, head = ambil(u)
    print(f"\n=== {u} ===")
    print(f"   status {st} | {detik:.1f}s | {len(h)} byte | server {head.get('server')}")
    if not h:
        continue
    t = re.search(r"<title[^>]*>(.*?)</title>", h, re.S)
    de = re.search(r'<meta[^>]+name="description"[^>]+content="([^"]*)"', h, re.I)
    print(f"   judul : {bersih(t.group(1))[:110] if t else '-'}")
    print(f"   meta  : {bersih(de.group(1))[:140] if de else '-'}")
    # jejak identitas
    for pola, label in ((r"rsia\s*merr|rsiamerr", "sebutan RSIA MERR"),
                        (r"kendangsari hospital", "sebutan Kendangsari Hospital"),
                        (r"\bmerr\b", "kata MERR"),
                        (r"kendangsari", "kata Kendangsari")):
        n = len(re.findall(pola, h, re.I))
        if n:
            print(f"   {label:<26} {n}x")
    alamat = re.findall(r"(?:jl\.?|jalan)[^<\n]{0,70}", bersih(h), re.I)[:2]
    if alamat:
        print(f"   alamat: {alamat}")
    WA_POLA = r"(?:wa\.me/|tel:|\+62)[0-9\-\s]{6,18}"
    print(f"   WA/telp: {sorted(set(re.findall(WA_POLA, h)))[:4]}")
