#!/usr/bin/env python3
"""Cari link artikel FRESH (belum pernah dipakai di Threads akun itu) sebagai pengganti."""
import json, subprocess, re, time

UA = "Mozilla/5.0 (X11; Linux x86_64) Chrome/124"
SITES = {
    "studiosoft": "https://studiosoft.id",
    "tslaw": "https://www.tslaw.id",
    "tsc": "https://www.theskincodeclinic.com",
    "das": "https://dasindonesia.com",
}
MAP = {"ukyhandoko": "uky", "studiosoft.id": "studiosoft", "tslaw_firm": "tslaw",
       "theskincode_clinic": "tsc", "das_indonesia": "das"}


def jina(url):
    p = subprocess.run(["curl", "-4", "-sL", "--compressed", "--max-time", "80", "-A", UA,
                        f"https://r.jina.ai/{url}"], capture_output=True, text=True, errors="replace")
    return p.stdout or ""


def norm(u):
    return u.rstrip("/").lower().replace("www.", "")


used = {k: [norm(x) for x in v] for k, v in json.load(open("/tmp/threads_used_all.json")).items()}
hasil = {}
for kunci, base in SITES.items():
    url = f"{base}/wp-json/wp/v2/posts?per_page=40&_fields=date,link,title&cb={int(time.time())}"
    txt = jina(url)
    m = re.search(r"\[\{.*\}\]", txt, re.S)
    arr = []
    if m:
        try:
            arr = json.loads(m.group(0))
        except Exception:
            arr = []
    fresh, sudah = [], []
    for a in arr:
        lk = a.get("link", "")
        judul = re.sub(r"<[^>]+>", "", (a.get("title") or {}).get("rendered", ""))
        baris = f"{a.get('date','')[:10]} | {judul[:88]} | {lk}"
        (sudah if norm(lk) in used.get(kunci, []) else fresh).append(baris)
    hasil[kunci] = {"fresh": fresh, "sudah": sudah}
    print(f"\n=== {kunci}: {len(fresh)} fresh / {len(sudah)} sudah dipakai (dari {len(arr)} artikel)")
    for b in fresh[:20]:
        print("   FRESH  ", b)
json.dump(hasil, open("/tmp/artikel_fresh_20260930.json", "w"), ensure_ascii=False, indent=1)
