#!/usr/bin/env python3
"""DMark: cek kanonik (www vs apex), sitemap WordPress, dan data terstruktur di halaman."""
import html, re, urllib.request, urllib.error

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


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


print("=== kanonik & duplikasi www vs apex ===")
for u in ("https://www.dmark.id/", "https://dmark.id/"):
    st, h, head = ambil(u)
    can = re.findall(r'<link rel="canonical" href="([^"]+)"', h)
    rob = re.findall(r"<meta name=.robots. content=.([^\"']+)", h)
    print(f"   {u:<28} {st} | kanonik: {can} | robots: {rob}")
    print(f"      redirect dari server: {head.get('location') or '-'}")

print("\n=== sitemap WordPress ===")
for u in ("https://www.dmark.id/wp-sitemap.xml", "https://www.dmark.id/wp-sitemap-posts-post-1.xml"):
    st, h, _ = ambil(u)
    n = len(re.findall(r"<loc>", h))
    print(f"   {u.replace('https://www.dmark.id','')[:40]:<42} {st} | {n} URL")

print("\n=== data terstruktur di beranda & satu artikel ===")
for u in ("https://www.dmark.id/", "https://www.dmark.id/7-ide-konten-kreatif-untuk-marketing-bisnis-kecil/"):
    st, h, _ = ambil(u)
    jenis = re.findall(r'"@type"\s*:\s*"([A-Za-z]+)"', h)
    print(f"   {u.replace('https://www.dmark.id','')[:52]:<54} {st} | blok ld+json: {h.count('application/ld+json')} | tipe: {sorted(set(jenis))[:8]}")

print("\n=== on-page beranda ===")
st, h, _ = ambil("https://www.dmark.id/")
t = re.search(r"<title[^>]*>(.*?)</title>", h, re.S)
de = re.search(r'<meta name="description" content="([^"]*)"', h, re.I)
h1 = re.findall(r"<h1[^>]*>(.*?)</h1>", h, re.S)
h2 = re.findall(r"<h2[^>]*>(.*?)</h2>", h, re.S)
bersih = lambda x: html.unescape(re.sub(r"\s+", " ", re.sub(r"<[^>]+>", "", x))).strip()
print(f"   <title>     : {bersih(t.group(1))[:110] if t else '-'}")
print(f"   description : {bersih(de.group(1))[:150] if de else '(TIDAK ADA)'}")
print(f"   H1          : {[bersih(x)[:70] for x in h1[:2]]}")
print(f"   jumlah H2   : {len(h2)} | H3: {len(re.findall('<h3', h, re.I))}")
print(f"   gambar tanpa alt: {len([x for x in re.findall(r'<img[^>]*>', h, re.I) if 'alt=' not in x])} dari {len(re.findall(r'<img', h, re.I))}")
print(f"   ukuran HTML : {len(h):,} byte | skrip: {len(re.findall(r'<script', h, re.I))} | css: {len(re.findall(r'rel=.stylesheet', h, re.I))}")
print(f"   tautan WhatsApp: {len(re.findall(r'wa.me|whatsapp', h, re.I))}")
print(f"   pelacak: GA4 {sorted(set(re.findall(r'G-[A-Z0-9]{8,}', h)))} | GTM {sorted(set(re.findall(r'GTM-[A-Z0-9]+', h)))}")
print(f"   pelacak lain (gtag/analytics.js): {'gtag' if 'gtag(' in h else ''} {'analytics.js' if 'analytics.js' in h else ''}")
