import json, re, urllib.request, subprocess
from repliz_client import HEADERS, BASE, ACC

UA = {'User-Agent': 'Mozilla/5.0'}
# 1. semua artikel live dasindonesia.com
live = []
page = 1
while True:
    url = f"https://dasindonesia.com/wp-json/wp/v2/posts?per_page=100&page={page}&_fields=link,title,date&status=publish"
    req = urllib.request.Request(url, headers=UA)
    try:
        with urllib.request.urlopen(req, timeout=40) as r:
            data = json.loads(r.read())
    except Exception as e:
        print("stop paging:", e)
        break
    if not data:
        break
    for p in data:
        live.append((p['date'][:10], p['link'].rstrip('/'), p['title']['rendered']))
    page += 1
print("total artikel live dasindonesia.com:", len(live))

# 2. semua link yang pernah dipakai di akun DAS FB (semua status)
used = set()
page = 1
while True:
    req = urllib.request.Request(f'{BASE}/public/schedule?page={page}&limit=100', headers=HEADERS, method='GET')
    with urllib.request.urlopen(req, timeout=40) as r:
        data = json.loads(r.read())
    for d in data.get('docs', []):
        if d.get('accountId') != ACC.DAS_FB:
            continue
        for l in re.findall(r'https?://dasindonesia\.com[^\s]*', (d.get('description') or '')):
            used.add(l.rstrip('/'))
    if page >= data.get('totalPages', 1):
        break
    page += 1

# 3. history file
hist = json.load(open('post_das_fb_history.json'))
for p in hist['posts']:
    used.add((p.get('article_url') or '').rstrip('/'))
print("total link pernah dipakai (Repliz+history):", len(used))

# 4. artikel live yang BELUM pernah dipakai
unused = [x for x in live if x[1] not in used]
print("\n=== BELUM DIPAKAI (%d) ===" % len(unused))
for d, l, t in sorted(unused, reverse=True):
    print(f"{d}  {l}")
    print(f"          {t}")
