#!/usr/bin/env python3
"""TSLaw 13 Okt 2026 — tarik semua artikel + uji bentuk alamat (slug vs .htm) + saring kandidat prioritas 2."""
import base64, json, re, time, urllib.request, urllib.error, importlib.util, sys

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


def req(url, tries=3):
    r = urllib.request.Request(url, headers={"Authorization": f"Basic {AUTH}", **UA})
    for i in range(tries):
        try:
            with urllib.request.urlopen(r, timeout=180) as x:
                return x.status, x.read()
        except urllib.error.HTTPError as e:
            return e.code, b""
        except Exception:
            time.sleep(4)
    return 0, b""


def head(url):
    """status kode untuk halaman publik, tanpa mengikuti pengalihan."""
    class NoRedir(urllib.request.HTTPRedirectHandler):
        def redirect_request(self, *a, **k):
            return None
    op = urllib.request.build_opener(NoRedir)
    r = urllib.request.Request(url, headers=UA, method="GET")
    try:
        with op.open(r, timeout=60) as x:
            return x.status, x.geturl()
    except urllib.error.HTTPError as e:
        return e.code, url
    except Exception as e:
        return -1, str(e)


st, body = req(f"{BASE}/wp-json/wp/v2/posts?per_page=100&status=publish,future,draft&context=edit"
               "&_fields=id,slug,link,title,content,meta,date,status")
if st != 200:
    print("GAGAL tarik posts:", st); sys.exit(1)
posts = json.loads(body)
json.dump(posts, open(f"{WS}/tslaw_posts_20261013.json", "w"), ensure_ascii=False, indent=1)
print(f"posts: {len(posts)}")
print("bentuk alamat /biaya-jasa-law-firm-surabaya/ :", head(f"{BASE}/biaya-jasa-law-firm-surabaya/"))
print("bentuk alamat .htm                           :", head(f"{BASE}/biaya-jasa-law-firm-surabaya.htm"))

print(f"\n{'id':>6} {'kata':>5} {'wa':>2} {'faq':>3} {'pilar':>5}  slug")
for p in sorted(posts, key=lambda z: len(re.sub(r"<[^>]+>", " ", (z["content"]["raw"] or "")).split())):
    raw = p["content"]["raw"] or ""
    kata = len(re.sub(r"<[^>]+>", " ", raw).split())
    wa = 1 if re.search(r"wa\.me/\d|api\.whatsapp\.com", raw) else 0
    faq = len(re.findall(r"FAQPage", raw))
    pilar = 0
    for h in re.findall(r'href="([^"]+)"', raw):
        if re.search(r"/law-firm-surabaya", h):
            pilar += 1
    if kata <= 960:
        print(f"{p['id']:>6} {kata:>5} {wa:>2} {faq:>3} {pilar:>5}  {p['slug'][:60]}")
