#!/usr/bin/env python3
"""Cek ulang apakah skrip suntikan masih ada di dasindonesia.com (beranda + beberapa halaman)."""
import re, time, urllib.error, urllib.request

SITUS = "https://dasindonesia.com"
UA = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/124.0 Safari/537.36"}
POLA = re.compile(r"livedashboardkit|35862e74a4a697bb6d998c471eade297|_b4a7b|_86b24|_8e635|track-\"", re.I)


def ambil(u):
    try:
        with urllib.request.urlopen(urllib.request.Request(u, headers=UA), timeout=45) as x:
            return x.status, x.read().decode("utf-8", "replace")
    except urllib.error.HTTPError as e:
        return e.code, e.read().decode("utf-8", "replace")[:200]
    except Exception as e:
        return "GAGAL", str(e)[:40]


urls = [f"{SITUS}/"]
sm_st, sm = ambil(f"{SITUS}/sitemap_index.xml")
if sm_st == 200:
    subs = [u for u in re.findall(r"<loc>([^<]+)</loc>", sm) if "post-sitemap" in u or "page-sitemap" in u]
    kandidat = []
    for s in subs:
        st, t = ambil(s)
        if st == 200:
            kandidat += re.findall(r"<loc>([^<]+)</loc>", t)
    urls += kandidat[:8]

print(f"halaman diperiksa: {len(urls)}\n")
ketemu = []
for u in urls:
    st, t = ambil(u)
    if st != 200:
        print(f"   {st} · {u.replace('https://','')[:70]}")
        continue
    m = POLA.findall(t)
    kecil = len(re.findall(r"overflow:\s*hidden;\s*height:\s*1", t, re.I))
    status = "⚠ MASIH ADA" if m else "bersih"
    print(f"   {status} · {len(t):>7} char · {u.replace('https://','')[:66]}")
    if m:
        ketemu.append((u, m[:2]))
    time.sleep(0.5)

print(f"\nkesimpulan: {'MASIH ADA skrip suntikan di ' + str(len(ketemu)) + ' halaman' if ketemu else 'tidak ditemukan lagi skrip suntikan'}")
for u, m in ketemu[:5]:
    print(f"   {u} → {m}")
