#!/usr/bin/env python3
"""Verifikasi pasca-pembersihan klinikmetamorf.com: kategori sisa, halaman depan, jejak judi."""
import base64, html, json, re, time, urllib.request, urllib.error

SITE = "https://klinikmetamorf.com"
UA = {"User-Agent": "Mozilla/5.0"}
AUTH = base64.b64encode(b"bambangbot:whWA hVlW omQk 9FSL 8rkz VMHy").decode()
JUDI = re.compile(r"casino|kasyn|kasino|slot|spin|bet|bonus|jackpot|казино|автомат|bahis|casino|"
                  r"1win|1xbet|22bet|mostbet|daddy|дэдди|gambling|wetten|sportwetten", re.I)


def wp(path):
    r = urllib.request.Request(SITE + path, headers={"Authorization": f"Basic {AUTH}", **UA})
    for _ in range(3):
        try:
            with urllib.request.urlopen(r, timeout=150) as x:
                return json.loads(x.read())
        except Exception:
            time.sleep(3)
    return []


print("=== kategori yang tersisa ===")
cats, page = [], 1
while True:
    d = wp(f"/wp-json/wp/v2/categories?per_page=100&page={page}&_fields=id,name,count")
    if not isinstance(d, list):
        break
    cats += d
    if len(d) < 100:
        break
    page += 1
sisa_judi = [c for c in cats if JUDI.search(html.unescape(c["name"]))]
print(f"  total kategori: {len(cats)} | masih berbau judi: {len(sisa_judi)}")
for c in sisa_judi[:12]:
    print(f"    #{c['id']} {html.unescape(c['name'])[:50]} ({c['count']})")
if cats:
    print("  contoh kategori yang tersisa (seharusnya klinik):")
    for c in sorted(cats, key=lambda x: -x["count"])[:6]:
        print(f"    {html.unescape(c['name'])[:44]:<46} {c['count']} post")

print("\n=== halaman depan situs ===")
try:
    r = urllib.request.Request(SITE + "/", headers=UA)
    h = urllib.request.urlopen(r, timeout=90).read().decode("utf-8", "replace")
    kata_judi = len(JUDI.findall(h))
    print(f"  kata berbau judi di HTML: {kata_judi}")
    link_luar = sorted(set(re.findall(r'href="https?://(?!klinikmetamorf\.com)([^/"]+)', h)))
    print(f"  domain luar yang ditautkan: {link_luar[:10]}")
    t = re.search(r"<title>(.*?)</title>", h, re.S)
    print(f"  judul: {t.group(1)[:80] if t else '-'}")
except Exception as e:
    print("  gagal:", str(e)[:80])

print("\n=== halaman arsip blog (kalau ada) ===")
for u in ("/blog/", "/artikel/", "/?post_type=post", "/sitemap_index.xml", "/robots.txt"):
    try:
        r = urllib.request.Request(SITE + u, headers=UA)
        resp = urllib.request.urlopen(r, timeout=60)
        b = resp.read().decode("utf-8", "replace")
        n = len(JUDI.findall(b))
        print(f"  {resp.status} | kata judi: {n:>3} | {u}")
    except urllib.error.HTTPError as e:
        print(f"  {e.code} | {u}")
    except Exception as e:
        print(f"  ERR {str(e)[:40]} | {u}")
