#!/usr/bin/env python3
"""Cek link di threads_content_today.json belum pernah dipakai (anti-redundant)."""
import json

used = json.load(open('/tmp/threads_used_all.json'))
content = json.load(open('/home/mantapman/.hermes/profiles/atun/workspace/threads_content_today.json'))
log = json.load(open('/home/mantapman/.hermes/profiles/atun/workspace/log_threads_harian.json'))


def norm(u):
    u = u.strip().rstrip('/').lower()
    for p in ('https://', 'http://', 'www.'):
        u = u.replace(p, '')
    return u


allseen = set()
for k, v in used.items():
    if isinstance(v, list):
        for u in v:
            allseen.add(norm(u if isinstance(u, str) else str(u)))
# log harian
for day, per in (log.get('threads_used_links') or {}).items():
    for acc, items in per.items():
        for it in items:
            allseen.add(norm(it.get('url', '')))

print("total URL pernah dipakai:", len(allseen))
ok = True
for a in content['accounts']:
    link = [w for w in a['text'].split() if w.startswith('http')][0]
    dup = norm(link) in allseen
    print(f"{a['account']:11s} {a['slot']:02d}:00 len={len(a['text']):3d} dup={'YA!' if dup else 'tidak'} {link}")
    ok = ok and not dup
print("AMAN" if ok else "ADA DUPLIKAT")
