#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Tampilkan kandidat artikel per domain dari cache hasil fetch 2 Okt + wp40 30 Sep."""
import json, re, sys

def show(fn, label):
    try:
        d = json.load(open(fn))
    except Exception as e:
        print(f"-- {label}: {fn} gagal {e}")
        return
    print(f"\n########## {label} ({fn})")
    if isinstance(d, dict) and all(k in ("fresh", "sudah") for k in list(d)[:1] + list(d)[:2]) and "fresh" in str(d)[:200]:
        pass
    if isinstance(d, dict):
        for dom, v in d.items():
            if isinstance(v, dict) and "fresh" in v:
                print(f"  [{dom}] fresh={len(v['fresh'])} sudah={len(v['sudah']) if isinstance(v.get('sudah'), list) else '?'}")
        return
    if isinstance(d, list):
        for p in d[:60]:
            t = p.get("title")
            t = t.get("rendered") if isinstance(t, dict) else t
            print(f"  {p.get('date','')[:10]} | {re.sub('<[^>]+>','',str(t))[:80]} | {p.get('link','')} | {p.get('status','')}")
        return

for fn, label in [("/tmp/threads_fresh_oct3.json", "fresh 2 Okt (threads)"),
                  ("/tmp/wp40_studiosoft.id.json", "wp40 SS"),
                  ("/tmp/wp40_www.tslaw.id.json", "wp40 TSLaw"),
                  ("/tmp/wp40_www.theskincodeclinic.com.json", "wp40 TSC"),
                  ("/tmp/wp40_dasindonesia.com.json", "wp40 DAS"),
                  ("/tmp/ss_articles.json", "ss_articles 3 Okt"),
                  ("/tmp/ss_ig_car_kandidat.json", "ss_ig_car_kandidat 3 Okt")]:
    show(fn, label)
