forked from skernelx/tavily-key-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
63 lines (50 loc) · 1.98 KB
/
utils.py
File metadata and controls
63 lines (50 loc) · 1.98 KB
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
61
62
63
"""
工具函数
"""
import time
from datetime import datetime
from config import API_KEYS_FILE
def save_api_key(email, api_key, password=None):
"""保存API key和账户信息到文件,并自动上传到 Proxy"""
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
account_line = f"{email},{password if password else 'N/A'},{api_key},{timestamp};\n"
try:
with open(API_KEYS_FILE, 'a', encoding='utf-8') as f:
f.write(account_line)
except FileNotFoundError:
with open(API_KEYS_FILE, 'w', encoding='utf-8') as f:
f.write(account_line)
print(f"✅ 账户信息已保存到 {API_KEYS_FILE}")
print(f"📧 邮箱: {email}")
print(f"🔐 密码: {password if password else 'N/A'}")
print(f"🔑 API Key: {api_key}")
print(f"⏰ 时间: {timestamp}")
# 自动上传到 Proxy
upload_to_proxy(api_key, email)
def upload_to_proxy(api_key, email=""):
"""将 API Key 上传到 Proxy 网关"""
try:
from config import PROXY_AUTO_UPLOAD, PROXY_URL, PROXY_ADMIN_PASSWORD
except ImportError:
return
if not PROXY_AUTO_UPLOAD or not PROXY_URL:
return
import urllib.request
import json
url = f"{PROXY_URL.rstrip('/')}/api/keys"
data = json.dumps({"key": api_key, "email": email}).encode()
req = urllib.request.Request(url, data=data, method="POST")
req.add_header("Content-Type", "application/json")
req.add_header("X-Admin-Password", PROXY_ADMIN_PASSWORD)
try:
with urllib.request.urlopen(req, timeout=10) as resp:
if resp.status == 200:
print(f"☁️ 已自动上传到 Proxy ({PROXY_URL})")
else:
print(f"⚠️ Proxy 上传失败: HTTP {resp.status}")
except Exception as e:
print(f"⚠️ Proxy 上传失败: {e}")
def wait_with_message(seconds, message="等待中"):
"""带消息的等待函数"""
print(f"⏳ {message},等待 {seconds} 秒...")
time.sleep(seconds)