import json, time, urllib.request, os, glob
eids = {}
for p in glob.glob('cfr_search/*.eid'):
    eid = int(open(p).read())
    name = os.path.basename(p).replace('.eid','')
    eids[eid] = name

os.makedirs('cfr_rels', exist_ok=True)
done = {int(f.split('_')[0]) for f in os.listdir('cfr_rels') if f.endswith('.json') and f.split('_')[0].isdigit()}
print(f"Already have: {len(done)}")
for eid, name in sorted(eids.items()):
    if eid in done: continue
    time.sleep(1.2)
    url = f"https://littlesis.org/api/entities/{eid}/relationships?page=1"
    try:
        req = urllib.request.Request(url, headers={'User-Agent':'Mozilla/5.0','Accept':'application/json'})
        with urllib.request.urlopen(req, timeout=20) as r:
            data = r.read()
        path = f'cfr_rels/{eid}_{name}.json'
        with open(path, 'wb') as f: f.write(data)
        n = len(json.loads(data).get('data',[]))
        print(f"  {name}: {n} rels")
    except Exception as e:
        print(f"  {name}: ERR {e}")
