#!/usr/bin/env python3
"""DMark: bandingkan render cache vs render asal (nowprocket) — untuk tahu apakah perubahan sudah masuk."""
import re, urllib.request, urllib.error
from collections import Counter

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


def ambil(u):
    try:
        r = urllib.request.urlopen(urllib.request.Request(u, headers=UA), timeout=90)
        return r.status, r.read().decode("utf-8", "replace"), dict(r.headers)
    except urllib.error.HTTPError as e:
        return e.code, "", dict(e.headers)
    except Exception as e:
        return 0, str(e)[:80], {}


for u in ("https://www.dmark.id/", "https://www.dmark.id/?nowprocket=1",
          "https://www.dmark.id/home-2/", "https://www.dmark.id/home-2/?nowprocket=1"):
    st, h, head = ambil(u)
    print(f"\n=== {u} ===")
    print(f"   status {st} | {len(h):,} byte | cf: {head.get('cf-cache-status')}")
    if not h:
        continue
    tag_kutipan = re.findall(r"<h([1-6])[^>]*>\s*Creativity is Intelegence", h)
    print(f"   tag kutipan: {['h'+t for t in tag_kutipan] or 'tidak terlihat'}")
    print(f"   sebaran tag: {dict(Counter(re.findall(r'<h([1-6])[^>]*>', h)))}")
    print(f"   judul halaman: {(re.search(r'<title[^>]*>(.*?)</title>', h, re.S) or [None,'-'])[1][:50] if re.search(r'<title[^>]*>(.*?)</title>', h, re.S) else '-'}")
    print(f"   ada 'Demand Generation': {'Demand Generation' in h} | ada 'nowprocket' di skrip: {'nowprocket' in h}")
