Files

82 lines
2.4 KiB
Python
Executable File

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""ar_health.py — проба ключей api-route (обычный chat sol) и парковка мёртвых каналов.
usage: python3 ar_health.py [--apply]
"""
import json
import sqlite3
import sys
import time
from concurrent.futures import ThreadPoolExecutor
import urllib3
import requests
urllib3.disable_warnings()
DB = "/opt/grok-gateway/new-api/one-api.db"
POOL = "/root/ar_keys.jsonl"
BASE = "https://www.api-route.com"
MODEL = "gpt-6.1-sol"
APPLY = "--apply" in sys.argv
keys, seen = [], set()
for l in open(POOL, encoding="utf-8"):
l = l.strip()
if not l:
continue
try:
k = json.loads(l).get("key")
except Exception:
continue
if k and k not in seen:
seen.add(k)
keys.append(k)
def probe(k):
try:
r = requests.post(
BASE + "/v1/chat/completions",
timeout=60,
verify=False,
headers={"Authorization": "Bearer " + k, "Content-Type": "application/json"},
json={"model": MODEL, "messages": [{"role": "user", "content": "ok"}], "max_tokens": 4},
)
return k, r.status_code, r.text[:120]
except Exception as e:
return k, -1, type(e).__name__
alive, dead = [], []
t0 = time.time()
with ThreadPoolExecutor(max_workers=10) as ex:
for k, code, txt in ex.map(probe, keys):
if code == 200 and "choices" in txt:
alive.append(k)
else:
dead.append((k, code, txt.replace("\n", " ")[:90]))
print("probe: alive=%d dead=%d in %.0fs" % (len(alive), len(dead), time.time() - t0))
for d in dead[:8]:
print(" dead", d[1], d[2][:80])
if APPLY:
con = sqlite3.connect(DB, timeout=300)
con.execute("pragma busy_timeout=300000")
cur = con.cursor()
on = off = 0
for k in keys:
name = "ar-" + k[-6:]
want = 1 if k in alive else 2
cur.execute("update channels set status=? where name=?", (want, name))
cur.execute(
"""update abilities set enabled=? where channel_id=(select id from channels where name=?)""",
(1 if want == 1 else 0, name),
)
if want == 1:
on += 1
else:
off += 1
con.commit()
n = cur.execute("select count(*) from channels where tag='ar' and status=1").fetchone()[0]
con.close()
print("channels on=%d off=%d | ar active=%d" % (on, off, n))