#!/usr/bin/env python3
"""TSC: (1) pindai SEMUA artikel tanpa batas halaman untuk slug yang hilang,
(2) cek arsip web untuk menentukan jenis konten lamanya."""
import base64, json, re, time, urllib.request, urllib.error, importlib.util

WS = "/home/mantapman/.hermes/profiles/atun/workspace"
spec = importlib.util.spec_from_file_location("cfg_tsc", f"{WS}/cfg_tsc.py")
mod = importlib.util.module_from_spec(spec); spec.loader.exec_module(mod)
CFG = mod.CFG
DOM = CFG["domain"]
AUTH = base64.b64encode(f'{CFG["user"]}:{CFG["pass"]}'.encode()).decode()
UA = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/124 Safari/537.36"}


def rest(path):
    r = urllib.request.Request(f"https://www.{DOM}{path}", headers={"Authorization": f"Basic {AUTH}", **UA})
    for _ in range(3):
        try:
            with urllib.request.urlopen(r, timeout=180) as x:
                return x.status, json.loads(x.read()), dict(x.headers)
        except urllib.error.HTTPError as e:
            return e.code, e.read().decode()[:150], {}
        except Exception:
            time.sleep(4)
    return 0, "gagal", {}


print("=== 1. semua artikel (tanpa batas halaman) ===")
st, d, h = rest("/wp-json/wp/v2/posts?per_page=1&_fields=id")
print(f"   total artikel di WordPress: {h.get('X-WP-Total')}")

semua = []
page = 1
while True:
    st, d, h = rest(f"/wp-json/wp/v2/posts?per_page=100&page={page}&status=publish,draft,trash,private"
                    f"&_fields=id,slug,link,title,status")
    if not isinstance(d, list):
        break
    semua += d
    if len(d) < 100:
        break
    page += 1
print(f"   terbaca: {len(semua)} artikel")

CARI = ["jadwal", "profil", "dermatologi-anak", "tahi-lalat-laser", "klinik-dermatologi-terpercaya"]
for c in CARI:
    cocok = [p for p in semua if c in p["slug"]]
    print(f"   [{c}] {len(cocok)} artikel cocok")
    for p in cocok[:4]:
        print(f"        #{p['id']} [{p['status']}] /{p['slug'][:60]}")

print("\n=== 2. arsip web untuk 5 URL yang 404 ===")
for u in ("jadwal-dokter", "profil", "dermatologi-anak",
          "menghilangkan-tahi-lalat-laser-eksisi-atau-elektrokauter",
          "5-klinik-dermatologi-terpercaya-di-surabaya-untuk-perawatan-kulit"):
    for bentuk in (f"https://www.{DOM}/{u}/", f"https://{DOM}/{u}/"):
        try:
            api = f"https://archive.org/wayback/available?url={bentuk}"
            with urllib.request.urlopen(urllib.request.Request(api, headers=UA), timeout=45) as x:
                j = json.loads(x.read())
            snap = (j.get("archived_snapshots") or {}).get("closest")
            if snap:
                print(f"   ADA arsip /{u[:44]} → {snap.get('timestamp')} {snap.get('url')[:80]}")
                break
        except Exception:
            pass
    else:
        print(f"   tidak ada arsip /{u[:44]}")

print("\n=== 3. apakah ada tautan internal yang menunjuk ke URL mati itu? ===")
st, d, h = rest("/wp-json/wp/v2/posts?per_page=100&_fields=id,slug,content")
n = 0
for p in (d if isinstance(d, list) else []):
    raw = p.get("content", {}).get("rendered", "")
    for c in CARI:
        if f"/{c}" in raw:
            print(f"   artikel #{p['id']} /{p['slug'][:40]} menautkan sesuatu yang memuat '{c}'")
            n += 1
print(f"   total temuan tautan: {n} (dari 100 artikel pertama)")
