#!/usr/bin/env python3
"""Bandingkan halaman RSIA Kendangsari yang peringkat 1 dengan halaman StudioSoft: apa bedanya."""
import html as H, json, re, time, urllib.error, urllib.request

WS = "/home/mantapman/.hermes/profiles/atun/workspace"
KUNCI = json.load(open(f"{WS}/.serper_token.json"))["key"]
HS = {"X-API-KEY": KUNCI, "Content-Type": "application/json", "User-Agent": "Mozilla/5.0"}
UA = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/124.0 Safari/537.36"}


def cari(q, jumlah=10):
    body = json.dumps({"q": q, "gl": "id", "hl": "id", "num": jumlah}).encode()
    try:
        r = urllib.request.urlopen(urllib.request.Request("https://google.serper.dev/search", data=body, headers=HS), timeout=90)
        return json.loads(r.read()).get("organic") or []
    except Exception as e:
        print(f"   gagal «{q}»: {str(e)[:60]}")
        return []


def profil(u):
    """Ambil judul, H1, panjang isi, dan skema dari sebuah halaman."""
    try:
        r = urllib.request.urlopen(urllib.request.Request(u, headers=UA), timeout=90)
        h = r.read().decode("utf-8", "replace")
    except Exception as e:
        return {"err": str(e)[:60]}
    t = re.search(r"<title[^>]*>(.*?)</title>", h, re.S)
    h1 = re.findall(r"<h1[^>]*>(.*?)</h1>", h, re.S)
    de = re.search(r'<meta name="description" content="([^"]*)"', h, re.I)
    bersih = lambda x: H.unescape(re.sub(r"\s+", " ", re.sub(r"<[^>]+>", "", x))).strip()
    teks = re.sub(r"<script.*?</script>|<style.*?</style>", " ", h, flags=re.S | re.I)
    kata = len(bersih(teks).split())
    return {"status": r.status, "judul": bersih(t.group(1)) if t else "", "h1": [bersih(x) for x in h1[:2]],
            "desc": bersih(de.group(1)) if de else "", "kata": kata,
            "schema": sorted(set(re.findall(r'"@type"\s*:\s*"([A-Za-z]+)"', h)))[:8],
            "h2": len(re.findall(r"<h2", h, re.I)), "gambar": len(re.findall(r"<img", h, re.I)),
            "internal": len(set(re.findall(r'href="(?:https?://[^"]*?)?"?(/[a-z0-9\-/]{3,40}/)"?', h, re.I)))}


print("=== 1. siapa yang menempati posisi teratas ===")
for q in ("rsia surabaya", "rumah sakit ibu dan anak surabaya", "digital agency surabaya", "jasa seo surabaya"):
    print(f"\n   «{q}»")
    for i, h in enumerate(cari(q)[:5], 1):
        print(f"      {i}. {str(h.get('link'))[:95]}")
    time.sleep(0.6)

print("\n=== 2. profil halaman yang menang (RSIA Kendangsari) ===")
for u in ("https://rsia.kendangsari.com/",):
    p = profil(u)
    print(f"   {u}")
    for k, v in p.items():
        print(f"      {k:<12}: {str(v)[:130]}")

print("\n=== 3. profil halaman StudioSoft ===")
for u in ("https://studiosoft.id/", "https://studiosoft.id/jasa-seo-surabaya-paket-harga/",
          "https://studiosoft.id/digital-agency-terbaik-di-surabaya/"):
    p = profil(u)
    print(f"\n   {u}")
    if p.get("err"):
        print(f"      gagal: {p['err']}")
        continue
    for k, v in p.items():
        print(f"      {k:<12}: {str(v)[:130]}")
