Files
keyhunter-parser/pat_check.py

45 lines
1.4 KiB
Python

"""Validate PAT pool: GET /user with each token, keep 200s."""
import os
KF_BASE = os.path.dirname(os.path.abspath(__file__))
import sys
from concurrent.futures import ThreadPoolExecutor, as_completed
import requests
SRC = os.path.join(KF_BASE, "gh_tokens.txt")
OUT = os.path.join(KF_BASE, "gh_tokens_valid.txt")
def check(tok):
try:
r = requests.get("https://api.github.com/user",
headers={"Authorization": f"Bearer {tok}",
"User-Agent": "keyfarm"},
timeout=15)
return tok, r.status_code == 200
except Exception:
return tok, False
def main():
toks = [l.strip() for l in open(SRC, encoding="utf-8")
if l.strip().startswith("ghp_")]
print(f"checking {len(toks)} PATs...", flush=True)
alive = []
with ThreadPoolExecutor(max_workers=40) as ex:
futs = [ex.submit(check, t) for t in toks]
for i, fut in enumerate(as_completed(futs), 1):
tok, ok = fut.result()
if ok:
alive.append(tok)
if i % 200 == 0:
print(f" {i}/{len(toks)} checked, alive={len(alive)}",
flush=True)
with open(OUT, "w", encoding="utf-8") as f:
for t in sorted(alive):
f.write(t + "\n")
print(f"DONE: {len(alive)}/{len(toks)} alive -> {OUT}", flush=True)
if __name__ == "__main__":
sys.exit(main())