#!/usr/bin/env python3
"""DAS: cek cepat keadaan situs sebelum pengukuran ulang — status, cache Cloudflare, Rocket Loader, aset berat."""
import re, time, urllib.request

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

t0 = time.time()
try:
    r = urllib.request.urlopen(urllib.request.Request("https://dasindonesia.com/?v=%d" % int(time.time()), headers=UA), timeout=120)
    h = r.read().decode("utf-8", "replace")
    print(f"beranda: {r.status} | {time.time()-t0:.1f}s | {len(h)} byte | cf-cache-status: {r.headers.get('cf-cache-status')}")
except Exception as e:
    print("beranda gagal:", str(e)[:90])
    h = ""

if h:
    tanda = {
        "Rocket Loader aktif": len(re.findall(r"rocket-loader|/cdn-cgi/scripts/[^\"']*rocket", h, re.I)),
        "gtag.js": len(re.findall(r"googletagmanager\.com/gtag/js", h)),
        "Google Tag /qhuv/": len(re.findall(r"/qhuv/", h)),
        "RevSlider": len(re.findall(r"revslider", h, re.I)),
        "icomoon font": len(re.findall(r"icomoon", h, re.I)),
        "FontAwesome": len(re.findall(r"font-awesome|fontawesome", h, re.I)),
        "ElegantIcons": len(re.findall(r"eleganticons", h, re.I)),
        "mediaelement": len(re.findall(r"mediaelement", h, re.I)),
        "Trustindex": len(re.findall(r"trustindex", h, re.I)),
        "gambar promo DAS-19.png": len(re.findall(r"DAS-19\.png", h)),
        "gambar tanpa dimensi": len([t for t in re.findall(r"<img[^>]*>", h, re.I) if "uploads/" in t and 'width="' not in t]),
        "total <img> dari uploads": len([t for t in re.findall(r"<img[^>]*>", h, re.I) if "uploads/" in t]),
        "gambar ber-srcset": len(re.findall(r"srcset=", h)),
        "gambar lazy (Rocket)": len(re.findall(r"data-lazy-src=", h)),
        "tema versi": sorted(set(re.findall(r"modules\.min\.js\?ver=([\w\.\-]+)", h))),
    }
    for k, v in tanda.items():
        print(f"   {k:<28} {v}")

print("\n=== waktu muat beberapa halaman ===")
for u in ("/", "/bengkel-spesialis-nissan-surabaya/", "/wp-json/"):
    t = time.time()
    try:
        r = urllib.request.urlopen(urllib.request.Request(f"https://dasindonesia.com{u}", headers=UA), timeout=90)
        print(f"   {r.status} | {time.time()-t:5.1f}s | {len(r.read()):>7} byte | {u}")
    except Exception as e:
        print(f"   GAGAL {type(e).__name__} | {time.time()-t:5.1f}s | {u}")
