#!/usr/bin/env python3
"""DAS: (1) validasi robots.txt, (2) audit gambar di isi artikel (dimensi, lazy, bobot)."""
import base64, json, re, time, urllib.request, importlib.util
from collections import Counter

WS = "/home/mantapman/.hermes/profiles/atun/workspace"
spec = importlib.util.spec_from_file_location("cfg_das", f"{WS}/cfg_das.py")
m = importlib.util.module_from_spec(spec); spec.loader.exec_module(m)
CFG = m.CFG
DOM, AUTH = CFG["domain"], base64.b64encode(f'{CFG["user"]}:{CFG["pass"]}'.encode()).decode()
UA = {"User-Agent": "Mozilla/5.0"}

print("=== robots.txt mentah ===")
try:
    r = urllib.request.urlopen(urllib.request.Request(f"https://{DOM}/robots.txt", headers=UA), timeout=90)
    isi = r.read().decode("utf-8", "replace")
    print(f"   status {r.status} | {len(isi)} byte | tipe {r.headers.get('content-type')}")
    print("   " + isi.replace("\n", "\n   ")[:900])
except Exception as e:
    print("   gagal:", str(e)[:100])


def rest(p):
    r = urllib.request.Request(f"https://{DOM}{p}", headers={"Authorization": f"Basic {AUTH}", **UA})
    for _ in range(3):
        try:
            with urllib.request.urlopen(r, timeout=200) as x:
                return json.loads(x.read())
        except Exception:
            time.sleep(5)
    return []


posts, page = [], 1
while True:
    d = rest(f"/wp-json/wp/v2/posts?per_page=100&page={page}&status=publish&context=edit&_fields=id,slug,content")
    if not isinstance(d, list):
        break
    posts += d
    if len(d) < 100:
        break
    page += 1

total = tanpa_dimensi = tanpa_lazy = tanpa_alt = 0
gambar = Counter()
artikel_bermasalah = {}
for p in posts:
    raw = p.get("content", {}).get("raw", "") or ""
    for tag in re.findall(r"<img[^>]*>", raw, re.I):
        total += 1
        ada_w = re.search(r'\bwidth="\d+"', tag)
        ada_h = re.search(r'\bheight="\d+"', tag)
        if not (ada_w and ada_h):
            tanpa_dimensi += 1
        if "loading=" not in tag:
            tanpa_lazy += 1
        if not re.search(r'alt="[^"]+"', tag):
            tanpa_alt += 1
        src = re.search(r'src="([^"]+)"', tag)
        if src:
            gambar[src.group(1)] += 1
    bermasalah = len([t for t in re.findall(r"<img[^>]*>", raw, re.I) if "width=" not in t or "loading=" not in t])
    if bermasalah:
        artikel_bermasalah[p["id"]] = bermasalah

print(f"\n=== gambar di isi {len(posts)} artikel ===")
print(f"   total gambar            : {total}")
print(f"   tanpa width/height      : {tanpa_dimensi}")
print(f"   tanpa loading=\"lazy\"    : {tanpa_lazy}")
print(f"   tanpa alt               : {tanpa_alt}")
print(f"   artikel dengan gambar bermasalah: {len(artikel_bermasalah)}")

print("\n=== gambar yang paling sering dipakai ===")
for u, n in gambar.most_common(10):
    print(f"   {n:>4}x {u[:100]}")

print("\n=== bobot 12 gambar artikel terberat ===")
urls = [u for u, _ in gambar.most_common(40)]
bobot = []
for u in urls[:20]:
    u2 = u if u.startswith("http") else f"https://{DOM}{u}"
    try:
        req = urllib.request.Request(u2, headers=UA, method="HEAD")
        with urllib.request.urlopen(req, timeout=45) as x:
            n = int(x.headers.get("content-length") or 0)
            bobot.append((n, u2))
    except Exception:
        pass
for n, u in sorted(bobot, reverse=True)[:12]:
    print(f"   {round(n/1024):>6} KB  {u[:96]}")
