#!/usr/bin/env python3
"""TSLaw: (1) cek job rank-tracker yang ada, (2) periksa pasangan artikel kembar (duplikat topik)."""
import base64, html, json, re, time, urllib.request, importlib.util

WS = "/home/mantapman/.hermes/profiles/atun/workspace"

print("=== job rank-tracker-mingguan ===")
d = json.load(open("/home/mantapman/.hermes/cron/jobs.json"))
for j in d.get("jobs", []):
    if j.get("id") in ("fefd8c1c61d1", "70d3a72f87a7"):
        print(f"\n   {j.get('name')} | {j.get('id')} | {j.get('schedule', {}).get('display')}")
        print("   script:", j.get("script"))
        teks = str(j.get("prompt") or "")
        print("   prompt:", (teks[:700] if teks else "(kosong / no_agent)"))

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)
DOM, AUTH = "www.tslaw.id", base64.b64encode(f'{m.CFG["user"]}:{m.CFG["pass"]}'.encode()).decode()
UA = {"User-Agent": "Mozilla/5.0"}


def rest(p):
    r = urllib.request.Request(f"https://{DOM}{p}", headers={"Authorization": f"Basic {AUTH}", **UA})
    for _ in range(3):
        try:
            with urllib.request.urlopen(r, timeout=180) as x:
                return json.loads(x.read())
        except Exception:
            time.sleep(4)
    return []


KEMBAR = [("merek", ["cara-daftar-merek-dagang", "cara-daftar-merek-dagang-2"]),
          ("hak cipta", ["cara-daftar-hak-cipta", "cara-daftar-hak-cipta-2026"])]

print("\n=== pasangan artikel kembar ===")
for nama, slug_list in KEMBAR:
    print(f"\n   ■ {nama.upper()}")
    for s in slug_list:
        d = rest(f"/wp-json/wp/v2/posts?slug={s}&status=publish&context=edit&_fields=id,slug,link,title,content,date,modified")
        if not isinstance(d, list) or not d:
            print(f"      /{s} → tidak ditemukan")
            continue
        p = d[0]
        raw = p["content"]["raw"] or ""
        kata = len(re.sub(r"<[^>]+>", " ", raw).split())
        print(f"      #{p['id']:>5} {kata:>4} kata | {p['date'][:10]} (diubah {p['modified'][:10]}) | {p['link']}")
        print(f"             judul: {re.sub('<[^>]+>', '', p['title']['raw'])[:80]}")
        print(f"             tombol WA: {len(re.findall('wa.me', raw))} | FAQPage: {'ada' if 'FAQPage' in raw else 'tidak'}")
