#!/usr/bin/env python3
"""DAS: periksa rinci status situs — 404 itu asli atau dari cache, dan bagian mana yang masih hidup."""
import re, time, urllib.request, urllib.error

UA = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/124 Safari/537.36"}
UJI = ["/?v=%d" % int(time.time()), "/", "/sitemap_index.xml", "/robots.txt",
       "/bengkel-spesialis-nissan-surabaya/", "/wp-login.php", "/wp-json/wp/v2/posts?per_page=1"]

for u in UJI:
    t0 = time.time()
    try:
        r = urllib.request.urlopen(urllib.request.Request(f"https://dasindonesia.com{u}", headers=UA), timeout=40)
        isi = r.read()
        judul = re.search(r"<title[^>]*>(.*?)</title>", isi.decode("utf-8", "replace"), re.S)
        print(f"   {r.status} | {time.time()-t0:5.1f}s | {len(isi):>7} byte | cf={r.headers.get('cf-cache-status')} | {u[:52]}")
        if judul:
            print(f"        judul: {judul.group(1).strip()[:70]}")
    except urllib.error.HTTPError as e:
        isi = e.read()[:400].decode("utf-8", "replace")
        print(f"   {e.code} | {time.time()-t0:5.1f}s | cf={e.headers.get('cf-cache-status')} | server={e.headers.get('server')} | {u[:52]}")
        print(f"        isi: {re.sub(chr(10), ' ', isi)[:150]}")
    except Exception as e:
        print(f"   GAGAL {type(e).__name__} | {time.time()-t0:5.1f}s | {u[:52]}")

print("\n=== cek apakah 404 berasal dari halaman WordPress (bukan Cloudflare) ===")
for u in ("/halaman-yang-tidak-ada-xyz/", "/?p=424"):
    try:
        r = urllib.request.urlopen(urllib.request.Request(f"https://dasindonesia.com{u}", headers=UA), timeout=40)
        print(f"   {r.status} | {u} | {len(r.read())} byte")
    except urllib.error.HTTPError as e:
        print(f"   {e.code} | {u} | server={e.headers.get('server')}")
    except Exception as e:
        print(f"   GAGAL {type(e).__name__} | {u}")
