import os, time, json, urllib.request no_px = urllib.request.build_opener(urllib.request.ProxyHandler({})) ports = list(range(16969, 16986)) shm_p = '/dev/shm/jc_desktop_ani_pool.json' live_p = '/tmp/live_ani_pool.json' def run_once(): ready = {} for p in ports: try: with no_px.open(f'http://127.0.0.1:{p}/?backend=macos', timeout=0.8) as r: j = json.loads(r.read().decode('utf-8', 'ignore')) if j.get('X-Apple-I-MD') and j.get('X-Apple-I-MD-M'): j['_ts'] = time.time() j['_local'] = True j['_port'] = p ready[p] = j except Exception: pass if not ready and os.path.exists(shm_p): try: d = json.load(open(shm_p)) for idx, t in enumerate(d.get('tokens', [])): if t.get('X-Apple-I-MD') and t.get('X-Apple-I-MD-M'): t['_ts'] = time.time() t['_local'] = True t['_port'] = 16970 + idx ready[16970 + idx] = t except Exception: pass if ready: payload = {'ts': time.time(), 'tokens': list(ready.values()), 'cooldowns': {}} raw = json.dumps(payload) try: with open(shm_p + '.exp.tmp', 'w') as f: f.write(raw) os.replace(shm_p + '.exp.tmp', shm_p) with open(live_p + '.tmp', 'w') as f: f.write(raw) os.chmod(live_p + '.tmp', 0o644) os.replace(live_p + '.tmp', live_p) except Exception: pass return len(ready) if __name__ == '__main__': import sys if '--once' in sys.argv: print(f'EXP_CNT={run_once()}') else: while True: run_once() time.sleep(2.5)