#!/usr/bin/env python3
"""Sapuan tautan internal .htm pasca-perubahan permalink TSLaw (13 Okt 2026)."""
import json, re, time, urllib.request, urllib.error
from collections import Counter

WS = "/home/mantapman/.hermes/profiles/atun/workspace"
UA = {"User-Agent": "Mozilla/5.0"}
posts = json.load(open(f"{WS}/tslaw_posts_20261013.json"))


def st(u):
    try:
        r = urllib.request.Request(u, headers=UA)
        op = urllib.request.build_opener()
        with op.open(r, timeout=45) as x:
            return x.status
    except urllib.error.HTTPError as e:
        return e.code
    except Exception as e:
        return type(e).__name__


htm, bersih = Counter(), Counter()
for p in posts:
    raw = p["content"]["raw"] or ""
    for h in re.findall(r'href="(https?://(?:www\.)?tslaw\.id/[^"#?]*)"', raw):
        if h.endswith(".htm"):
            htm[h] += 1
        else:
            bersih[h] += 1
print(f"artikel diperiksa: {len(posts)} ({len(set(p['status'] for p in posts))} status)")
print(f"tautan internal bentuk .htm  : {sum(htm.values())} pada {len(htm)} URL unik")
print(f"tautan internal bentuk /slug/: {sum(bersih.values())} pada {len(bersih)} URL unik")
print("\nuji sampel 8 URL .htm teratas:")
for u, n in htm.most_common(8):
    print(f"  {st(u)} ({n}x diartikel)  {u}")
print("\nuji 5 URL /slug/ yang paling sering:")
for u, n in bersih.most_common(5):
    print(f"  {st(u)} ({n}x)  {u}")
