File size: 1,900 Bytes
9db1674
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python3
"""
A2A Consumer Rotator (HF VM Pull Script)
Pulls a fresh OpenAI token from SIN-Supabase when a rate-limit is hit.
Infinite Scaling Architecture.
"""

import os
import json
import requests
from pathlib import Path

SUPABASE_URL = os.environ.get("SUPABASE_URL")
SUPABASE_KEY = os.environ.get("SUPABASE_SERVICE_ROLE_KEY")
AUTH_FILE = Path.home() / ".local" / "share" / "opencode" / "auth.json"

def pull_fresh_token():
    if not SUPABASE_URL or not SUPABASE_KEY:
        print("❌ FEHLER: SUPABASE_URL oder SUPABASE_SERVICE_ROLE_KEY fehlen!")
        return False
        
    base_url = SUPABASE_URL.rstrip('/')
    headers = {
        "apikey": SUPABASE_KEY,
        "Authorization": f"Bearer {SUPABASE_KEY}",
        "Content-Type": "application/json",
        "Prefer": "return=representation"
    }

    print("🔍 Suche frischen Token im Pool...")
    get_url = f"{base_url}/rest/v1/openai_account_pool?status=eq.FRESH&limit=1"
    resp = requests.get(get_url, headers=headers, timeout=5)
    
    if resp.status_code != 200 or not resp.json():
        print("❌ FEHLER: Kein frischer Token im Pool verfügbar!")
        return False
        
    row = resp.json()[0]
    row_id = row["id"]
    new_auth_data = row["auth_json_data"]
    
    patch_url = f"{base_url}/rest/v1/openai_account_pool?id=eq.{row_id}"
    requests.patch(patch_url, headers=headers, json={"status": "IN_USE"}, timeout=5)

    AUTH_FILE.parent.mkdir(parents=True, exist_ok=True)
    current_data = {}
    if AUTH_FILE.exists():
        try:
            current_data = json.loads(AUTH_FILE.read_text())
        except:
            pass
            
    current_data["openai"] = new_auth_data
    AUTH_FILE.write_text(json.dumps(current_data, indent=2))
    print(f"🚀 ERFOLG: Lokale auth.json überschrieben. (ID: {row_id})")
    return True

if __name__ == "__main__":
    pull_fresh_token()