#!/usr/bin/env python3
"""Pilih kandidat artikel FRESH (belum pernah dipakai di Threads) per situs."""
import json

CACHE = "/home/mantapman/.hermes/profiles/atun/workspace/_cache_artikel_wp_19okt.json"
USED = "/tmp/threads_used_now.json"

arts = json.load(open(CACHE))
used = json.load(open(USED))
glob = set(used.get("_global", []))


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


ACC_SITE = {"studiosoft": "studiosoft", "uky": "studiosoft", "tslaw": "tslaw",
            "tsc": "tsc", "das": "das"}

for acc, site in ACC_SITE.items():
    own_used = set(used.get(acc, []))
    fresh = []
    for a in arts.get(site, []):
        u = norm(a["link"])
        if not u:
            continue
        if u in glob or u in own_used:
            continue
        fresh.append(a)
    fresh.sort(key=lambda x: x.get("date") or "", reverse=True)
    print("=" * 70)
    print(f"{acc} (site {site}): total {len(arts.get(site, []))} artikel, fresh {len(fresh)}")
    for a in fresh[:15]:
        print(f"   {a.get('date','')[:10]}  {a['link']}")
        print(f"      {(a.get('title') or '')[:100]}")
    json.dump(fresh, open(f"/tmp/fresh_{acc}.json", "w"), ensure_ascii=False, indent=1)
