#!/usr/bin/env python3
"""Kandidat link BEBAS (belum pernah dipakai lintas-akun Threads) untuk tslaw/tsc/das.
+ cek HTTP 200. Output: /tmp/kandidat_free.txt
"""
import json, subprocess, sys

USED = json.load(open("/tmp/threads_used_all.json"))
GLOB = set(USED.get("_global", []))
PER = {k: set(USED.get(k, [])) for k in ("tslaw", "tsc", "das")}


def norm(u):
    return u.rstrip("/.,)").lower()


def http_ok(u):
    r = subprocess.run(["curl", "-s", "-o", "/dev/null", "-w", "%{http_code}",
                        "--http1.1", "--max-time", "90", "-L", u],
                       capture_output=True, text=True)
    return r.stdout.strip()


for nm in ("tslaw", "tsc", "das"):
    links = [l.strip() for l in open(f"/tmp/links_{nm}.txt") if l.strip()]
    free = [l for l in links if norm(l) not in GLOB]
    print(f"\n===== {nm}: {len(links)} artikel WP, {len(free)} BEBAS lintas-akun")
    for l in free:
        code = http_ok(l)
        mark = "OK " if code == "200" else code
        print(f"  [{mark}] {l}")
