#!/usr/bin/env python3
"""Sisir semua tautan di situs StudioSoft: mana yang mati, di artikel mana, dan jangkarnya apa.

Hasil: drafts/ss_tautan_mati.json + ringkasan di layar.
"""
import base64, json, re, time, urllib.error, urllib.request

WS = "/home/mantapman/.hermes/profiles/atun/workspace"
DOM = "studiosoft.id"
S = f"https://{DOM}"
AUTH = base64.b64encode(b"bambangbot:q26r RePo BVy6 pwU2 F3z7 ouyl").decode()
H = {"Authorization": f"Basic {AUTH}", "User-Agent": "Mozilla/5.0"}
UA = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/124.0 Safari/537.36"}


def rest(p):
    akhir = "belum"
    for percobaan in range(4):
        try:
            return json.loads(urllib.request.urlopen(urllib.request.Request(S + p, headers=H), timeout=180).read())
        except Exception as e:
            akhir = str(e)[:60]
            time.sleep(8 + percobaan * 6)
    raise RuntimeError(akhir)


print("=== kumpulkan artikel & halaman ===")
konten = []
for jenis in ("posts", "pages"):
    hal = 1
    while hal <= 12:
        d = rest(f"/wp-json/wp/v2/{jenis}?per_page=20&page={hal}&status=publish"
                 "&_fields=id,slug,link,title,content")
        if not isinstance(d, list) or not d:
            break
        konten += [{"jenis": jenis, **x} for x in d]
        if len(d) < 20:
            break
        hal += 1
        time.sleep(1)
print(f"   {len(konten)} artikel/halaman")


def isi_dari(p):
    c = p.get("content")
    if isinstance(c, dict):
        return c.get("raw") or c.get("rendered") or ""
    return c or ""


tautan = {}
for p in konten:
    for mo in re.finditer(r'<a[^>]+href="([^"#]+)"[^>]*>(.*?)</a>', isi_dari(p), re.S):
        u = mo.group(1).strip()
        if u.startswith("#") or u.startswith("mailto:") or u.startswith("tel:") or u.startswith("whatsapp"):
            continue
        jangkar = re.sub(r"\s+", " ", re.sub("<[^>]+>", " ", mo.group(2))).strip()[:90]
        tautan.setdefault(u, {"jumlah": 0, "dari": []})
        tautan[u]["jumlah"] += 1
        if len(tautan[u]["dari"]) < 4:
            tautan[u]["dari"].append({"pid": p["id"], "jenis": p["jenis"], "slug": p["slug"], "jangkar": jangkar})
print(f"   {len(tautan)} tautan unik")

print("\n=== periksa satu per satu ===")
mati = {}
for i, (u, info) in enumerate(sorted(tautan.items()), 1):
    coba = u
    if coba.startswith("/"):
        coba = S + coba
    if not coba.startswith("http"):
        continue
    status = None
    for percobaan in range(2):
        try:
            req = urllib.request.Request(coba, headers=UA, method="GET")
            with urllib.request.urlopen(req, timeout=25) as r:
                status = r.status
            break
        except urllib.error.HTTPError as e:
            status = e.code
            break
        except Exception as e:
            status = f"GAGAL {str(e)[:30]}"
            time.sleep(2)
    if status == 200 or (isinstance(status, int) and 301 <= status <= 399):
        continue
    mati[u] = {**info, "status": status}
    print(f"   [{i}/{len(tautan)}] {status} · {u[:96]}")
    for d in info["dari"][:2]:
        print(f"        dari #{d['pid']} {d['jenis']} «{d['jangkar'][:60]}»")

json.dump(mati, open(f"{WS}/drafts/ss_tautan_mati.json", "w"), ensure_ascii=False, indent=1)
print(f"\n=== hasil: {len(mati)} tautan bermasalah dari {len(tautan)} tautan unik ===")
print("→ drafts/ss_tautan_mati.json")
