#!/usr/bin/env python3
"""DAS: cek apakah situs sedang down (522) dan seberapa luas dampaknya."""
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"}
URLS = ["https://dasindonesia.com/", "https://dasindonesia.com/bengkel-spesialis-nissan-surabaya/",
        "https://dasindonesia.com/wp-json/", "https://dasindonesia.com/wp-login.php",
        "https://dasindonesia.com/robots.txt", "https://dasindonesia.com/sitemap_index.xml"]
for u in URLS:
    t0 = time.time()
    try:
        r = urllib.request.urlopen(urllib.request.Request(u + ("?cek=%d" % int(time.time()) if u.endswith("/") else ""), headers=UA), timeout=60)
        b = r.read()
        print(f"   {r.status} | {round(time.time()-t0,2):>6}s | {len(b)//1024:>5} KB | cf={r.headers.get('cf-cache-status')} | {u[:62]}")
    except urllib.error.HTTPError as e:
        print(f"   {e.code} | {round(time.time()-t0,2):>6}s | cf={e.headers.get('cf-cache-status')} | {u[:62]}")
    except Exception as e:
        print(f"   ERR | {round(time.time()-t0,2):>6}s | {str(e)[:40]} | {u[:62]}")

print("\n=== ulangi 2x untuk memastikan bukan sekadar lambat ===")
for i in (1, 2):
    try:
        r = urllib.request.urlopen(urllib.request.Request(f"https://dasindonesia.com/?ulang={i}{int(time.time())}", headers=UA), timeout=60)
        print(f"   percobaan {i}: {r.status} | {len(r.read())//1024} KB | cf={r.headers.get('cf-cache-status')}")
    except urllib.error.HTTPError as e:
        print(f"   percobaan {i}: HTTP {e.code}")
    except Exception as e:
        print(f"   percobaan {i}: {str(e)[:50]}")
    time.sleep(3)
