#!/usr/bin/env python3
"""Cari artikel TERBARU yang 0 hit di SEMUA jadwal LinkedIn StudioSoft."""
import json, re, subprocess, time
from datetime import datetime, timezone, timedelta
import sys
sys.path.insert(0, "/home/mantapman/.hermes/profiles/atun/workspace")
from repliz_client import ACC

docs = json.load(open("/tmp/hc65_sched.json"))
LI = ACC.STUDIOSOFT_LINKEDIN
li_docs = [d for d in docs if d.get("accountId") == LI]
print("total jadwal LI:", len(li_docs))
blob = json.dumps(li_docs, ensure_ascii=False)

# ambil sampai 100 artikel
def jina(path, tries=4):
    url = "https://r.jina.ai/https://studiosoft.id" + path
    for _ in range(tries):
        p = subprocess.run(["curl", "-s", "--max-time", "180", url], capture_output=True, text=True)
        raw = p.stdout or ""
        i = raw.find('[{"id"')
        if i < 0:
            i = raw.find('[{"')
        if i >= 0:
            j = raw.rfind('}]')
            try:
                return json.loads(raw[i:j+2])
            except Exception:
                pass
        time.sleep(3)
    return None

posts = jina("/wp-json/wp/v2/posts?per_page=100&_fields=id,date,slug,link,title")
if not posts:
    print("gagal ambil posts")
    sys.exit(1)
json.dump(posts, open("/tmp/hc65_posts100.json", "w"), indent=1)
print("posts:", len(posts))

zero = []
for p in posts:
    n = len(re.findall(re.escape(p["slug"]), blob))
    if n == 0:
        zero.append(p)
print("\n=== artikel 0 hit di LI (baru -> lama, maks 25) ===")
for p in zero[:25]:
    print(f"  {p['date'][:10]} #{p['id']:>5} {p['slug']}")

if zero:
    print("\nTERBARU 0-hit:", zero[0]["slug"], zero[0]["date"])
