#!/usr/bin/env python3
"""Ambil berkas yang dimuat skrip suntikan DAS, memakai nama berkas sesuai rumus di skripnya."""
import re, time, urllib.error, urllib.request

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

# rumus di skrip suntikan: waktu dibulatkan ke kelipatan 600 detik, lalu dijadikan hex
now = int(time.time())
bucket = now - (now % 600)


def coba(label, nama):
    url = f"https://{DOMAIN}/{nama}"
    try:
        with urllib.request.urlopen(urllib.request.Request(url, headers=UA), timeout=40) as x:
            isi = x.read().decode("utf-8", "replace")
        print(f"\n=== {label}: HTTP {x.status} · {len(isi)} char")
        print(isi[:1200])
    except urllib.error.HTTPError as e:
        print(f"   {label}: HTTP {e.code} · {url}")
    except Exception as e:
        print(f"   {label}: GAGAL {str(e)[:50]}")


print(f"waktu sekarang: {now} · bucket: {bucket} (hex {bucket:x}) · hex bucket sebelumnya {(bucket-600):x}")
coba("bucket sekarang", f"track-{bucket:x}.js")
coba("bucket 10 menit lalu", f"track-{(bucket - 600):x}.js")

print("\n=== isi beranda domain itu ===")
try:
    t = urllib.request.urlopen(urllib.request.Request(f"https://{DOMAIN}/", headers=UA), timeout=40).read().decode("utf-8", "replace")
    judul = re.search(r"<title>(.*?)</title>", t, re.S)
    print(f"   judul: {re.sub(r'<[^>]+>','',judul.group(1)).strip()[:80] if judul else '-'} · {len(t)} char")
    teks = re.sub(r"\s+", " ", re.sub(r"<[^>]+>", " ", re.sub(r"<script.*?</script>|<style.*?</style>", " ", t, flags=re.S)))
    print(f"   cuplikan: {teks[:400].strip()}")
except Exception as e:
    print("   gagal:", str(e)[:60])
