#!/usr/bin/env python3
"""Cari tahu apa yang mengubah URL TSLaw jadi berakhiran .htm: plugin, atau pengaturan WordPress."""
import base64, json, time, urllib.error, urllib.request

DOM = "www.tslaw.id"
AUTH = base64.b64encode(b"bambangbot:EFKm EYGx VQCP Y9go ncPr uM2Y").decode()
h = {"Authorization": f"Basic {AUTH}", "User-Agent": "Mozilla/5.0"}


def ambil(path):
    r = urllib.request.Request(f"https://{DOM}{path}", headers=h)
    try:
        with urllib.request.urlopen(r, timeout=90) as x:
            return json.loads(x.read())
    except urllib.error.HTTPError as e:
        return {"err": e.code, "msg": e.read().decode()[:150]}
    except Exception as e:
        return {"err": str(e)[:80]}


print("=== plugin aktif (kalau boleh dibaca) ===")
d = ambil("/wp-json/wp/v2/plugins?per_page=100&_fields=plugin,name,status,version")
if isinstance(d, list):
    for p in d:
        tanda = "AKTIF" if p.get("status") == "active" else "mati "
        print(f"   [{tanda}] {p.get('name')} {p.get('version')} · {p.get('plugin')}")
else:
    print("   tidak bisa dibaca:", str(d)[:120])

print("\n=== nama rute REST yang menyangkut permalink/redirect ===")
try:
    idx = json.loads(urllib.request.urlopen(urllib.request.Request(f"https://{DOM}/wp-json/", headers=h), timeout=60).read())
    ns = idx.get("namespaces", [])
    print("   namespaces:", ns)
    rute = [r for r in (idx.get("routes") or {}) if any(k in r.lower() for k in ("permalink", "redirect", "seo", "yoast", "rankmath"))]
    for r in sorted(rute)[:15]:
        print("   ", r)
except Exception as e:
    print("   gagal:", str(e)[:80])

print("\n=== pengaturan situs lewat REST ===")
s = ambil("/wp-json/wp/v2/settings")
if isinstance(s, dict) and "err" not in s:
    for k in sorted(s):
        if "permalink" in k or "url" in k or "home" in k:
            print(f"   {k}: {s[k]!r}")
else:
    print("   gagal:", str(s)[:120])
