#!/usr/bin/env python3
"""Penjaga kanibalisasi TSLaw untuk artikel #15: bandingkan calon (slug/judul/fokus) dengan
SEMUA artikel berstatus publish + future + draft, pakai skor Jaccard token dan fokus kata kunci identik."""
import base64, json, re, time, urllib.request, urllib.error

DOM = "tslaw.id"
AUTH = base64.b64encode(b"bambangbot:EFKm EYGx VQCP Y9go ncPr uM2Y").decode()
UA = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/124 Safari/537.36"}
CAL = {"slug": "pengacara-pinjol-ilegal-surabaya",
       "judul": "7 Pengacara Pinjaman Online dan Penagihan Utang Ilegal di Surabaya",
       "fokus": "pengacara pinjaman online surabaya"}
STOP = {"di", "dan", "yang", "untuk", "dengan", "pada", "ke", "dari", "the", "a", "7", "8", "6", "10", "5", "3", "4"}


def rest(path):
    h = {"Authorization": f"Basic {AUTH}", **UA}
    r = urllib.request.Request(f"https://{DOM}{path}", headers=h)
    for i in range(4):
        try:
            with urllib.request.urlopen(r, timeout=180) as x:
                return x.status, json.loads(x.read())
        except urllib.error.HTTPError as e:
            return e.code, e.read().decode()[:200]
        except Exception as e:
            print(f"  (ulang {i+1}: {str(e)[:60]})")
            time.sleep(8)
    return 0, "gagal"


def tok(s):
    return {w for w in re.split(r"[^a-z0-9]+", s.lower()) if w and w not in STOP and len(w) > 2}


semua = []
for status in ("publish", "future", "draft"):
    page = 1
    while page <= 6:
        st, d = rest(f"/wp-json/wp/v2/posts?status={status}&per_page=100&page={page}&context=edit"
                     f"&_fields=id,slug,link,title,status,meta")
        if not isinstance(d, list) or not d:
            break
        for p in d:
            p["_st"] = status
        semua += d
        page += 1
    time.sleep(1)

print(f"total artikel diperiksa (publish+future+draft): {len(semua)}")
cal_t = tok(CAL["slug"] + " " + CAL["judul"])
fokus_sama, dekat = [], []
for p in semua:
    judul = re.sub("<[^>]+>", "", p["title"]["raw"])
    f = (p.get("meta") or {}).get("_yoast_wpseo_focuskw") or ""
    skor = len(cal_t & tok(p["slug"] + " " + judul)) / max(1, len(cal_t | tok(p["slug"] + " " + judul)))
    if f.strip().lower() == CAL["fokus"]:
        fokus_sama.append((p["id"], p["_st"], f, judul))
    if skor >= 0.34:
        dekat.append((round(skor, 2), p["id"], p["_st"], f, judul))

print(f"\nFOKUS KATA KUNCI SAMA PERSIS: {len(fokus_sama)}")
for x in fokus_sama:
    print("   ", x)
print(f"\nSKOR JUDUL >= 0,34 (perlu dibaca, belum tentu benturan): {len(dekat)}")
for s, pid, st, f, j in sorted(dekat, reverse=True)[:10]:
    print(f"    {s}  #{pid} [{st}] fokus='{f}'  {j[:80]}")

print("\nDraft listikel TSLaw yang menunggu tayang:")
for p in semua:
    if p["_st"] in ("draft", "future") and p["id"] >= 3532:
        print(f"    #{p['id']} [{p['_st']}] {p['slug']}")
