#!/usr/bin/env python3
"""hc58: kandidat artikel LI (belum di antrean LI pending) + artikel SS terbaru."""
import json, re, ssl, time, urllib.request
from datetime import datetime, timezone

docs = json.load(open("/tmp/repliz_all_20261007.json"))
WS = "/home/mantapman/.hermes/profiles/atun/workspace"
import sys
sys.path.insert(0, WS)
from repliz_client import ACC

url_re = re.compile(r"studiosoft\.id/([a-z0-9\-]+)")


def slugs_of(d):
    return url_re.findall((d.get("description") or "") + " " + (d.get("title") or ""))


li_pending = set()
li_all = set()
for d in docs:
    if d.get("accountId") == ACC.STUDIOSOFT_LINKEDIN:
        li_all |= set(slugs_of(d))
        if (d.get("status") or "").lower() in ("pending", "scheduled", "draft"):
            li_pending |= set(slugs_of(d))
print("LI pending slugs:", len(li_pending), "| LI semua:", len(li_all))

t_all = set()
t_pending = set()
for d in docs:
    if d.get("accountId") == ACC.STUDIOSOFT_THREADS:
        t_all |= set(slugs_of(d))
        if (d.get("status") or "").lower() in ("pending", "scheduled", "draft"):
            t_pending |= set(slugs_of(d))
print("Threads pending slugs:", len(t_pending))

ctx = ssl.create_default_context()
def raw(url, timeout=40):
    req = urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0"})
    with urllib.request.urlopen(req, timeout=timeout, context=ctx) as r:
        return r.read().decode("utf-8", "replace")

posts = []
for page in (1, 2):
    url = ("https://studiosoft.id/wp-json/wp/v2/posts?per_page=100&page=%d"
           "&_fields=id,date,slug,title&cb=%d" % (page, int(time.time())))
    data = json.loads(raw(url))
    if not isinstance(data, list) or not data:
        break
    posts.extend(data)
    time.sleep(0.3)
print("WP posts:", len(posts))

def clean(t):
    for a, b in [("&#8211;", "-"), ("&#038;", "&"), ("&amp;", "&"), ("&#8217;", "'"),
                 ("&#8220;", '"'), ("&#8221;", '"'), ("&#8212;", "-")]:
        t = t.replace(a, b)
    return re.sub(r"&#\d+;", "", t).strip()

posts.sort(key=lambda x: x["date"], reverse=True)
print("\n== 30 artikel terbaru ==  (LI-pending? / Threads-pending?)")
for a in posts[:30]:
    s = a["slug"]
    print("  %s  %-62s LIpend=%-5s Tpend=%s" % (a["date"][:10], s[:62], s in li_pending, s in t_pending))

print("\n== kandidat LI (belum pernah/ belum PENDING di LI) — 20 terbaru ==")
n = 0
for a in posts:
    s = a["slug"]
    if s in li_pending:
        continue
    n += 1
    print("  %s  %-62s  pernahLI=%s  pernahT=%s" % (a["date"][:10], s[:62], s in li_all, s in t_all))
    if n >= 20:
        break
