#!/usr/bin/env python3
"""Cari artikel FRESH (belum pernah muncul di jadwal mana pun) per domain, plus judulnya."""
import json, re, subprocess
from datetime import datetime, timedelta, timezone

WIB = timezone(timedelta(hours=7))
docs = json.load(open("/tmp/scheds_1011.json"))
URL_RE = re.compile(r"https?://[^\s\)\]\"]+")
seen = set()
for d in docs:
    txt = " ".join(str(d.get(k) or "") for k in ("description", "title", "topic", "medias"))
    for u in URL_RE.findall(txt):
        seen.add(u.rstrip("/.,").lower())


def wp(domain):
    out = []
    for p in range(1, 6):
        url = f"https://{domain}/wp-json/wp/v2/posts?per_page=100&page={p}&_fields=link,title,date&orderby=date&order=desc"
        r = subprocess.run(["curl", "-4", "-s", "-m", "30", "-A", "Mozilla/5.0", url],
                           capture_output=True, text=True)
        try:
            data = json.loads(r.stdout)
        except Exception:
            print(f"  !! {domain} page {p} gagal: {r.stdout[:100]}")
            break
        if isinstance(data, dict):
            print(f"  !! {domain} page {p}: {str(data)[:120]}")
            break
        if not data:
            break
        out.extend(data)
        if len(data) < 100:
            break
    return out


for dom, label in [("studiosoft.id", "Studiosoft"), ("www.tslaw.id", "TSLaw"),
                   ("www.theskincodeclinic.com", "TSC"), ("dasindonesia.com", "DAS")]:
    posts = wp(dom)
    fresh = [p for p in posts if p["link"].rstrip("/").lower() not in seen]
    print(f"\n===== {label}: {len(posts)} artikel, {len(fresh)} FRESH =====")
    for p in fresh[:25]:
        t = re.sub(r"<[^>]+>", "", p["title"]["rendered"]) if isinstance(p["title"], dict) else str(p["title"])
        print(f"  {p['date'][:10]}  {p['link']}")
        print(f"      {t}")
