Delete update.py
Browse files
update.py
DELETED
@@ -1,139 +0,0 @@
|
|
1 |
-
"""
|
2 |
-
API密钥更新模块 - 提供API密钥的验证和更新功能
|
3 |
-
"""
|
4 |
-
import json
|
5 |
-
import os
|
6 |
-
import sqlite3
|
7 |
-
from datetime import datetime
|
8 |
-
from core.api_manager import get_api_manager
|
9 |
-
from utils.db import get_db_connection
|
10 |
-
from config import API_KEYS_FILE
|
11 |
-
|
12 |
-
def update(key_id):
|
13 |
-
"""
|
14 |
-
更新指定ID的API密钥
|
15 |
-
|
16 |
-
Args:
|
17 |
-
key_id (str): 要更新的API密钥ID
|
18 |
-
|
19 |
-
Returns:
|
20 |
-
dict: 包含更新结果的字典,成功时返回更新后的密钥信息,失败时返回错误信息
|
21 |
-
"""
|
22 |
-
# 从SQLite数据库中获取密钥信息
|
23 |
-
conn = get_db_connection()
|
24 |
-
try:
|
25 |
-
cursor = conn.cursor()
|
26 |
-
cursor.execute('SELECT * FROM api_keys WHERE id = ?', (key_id,))
|
27 |
-
row = cursor.fetchone()
|
28 |
-
|
29 |
-
if row is None:
|
30 |
-
# 数据库中找不到,尝试从JSON文件加载
|
31 |
-
# 这是为了支持旧版本的兼容
|
32 |
-
if os.path.exists(API_KEYS_FILE):
|
33 |
-
try:
|
34 |
-
with open(API_KEYS_FILE, "r", encoding="utf-8") as f:
|
35 |
-
data = json.load(f)
|
36 |
-
|
37 |
-
for key in data.get("api_keys", []):
|
38 |
-
if key.get("id") == key_id:
|
39 |
-
key_data = key
|
40 |
-
break
|
41 |
-
else:
|
42 |
-
return {"success": False, "message": f"未找到ID为 {key_id} 的API密钥"}
|
43 |
-
except Exception as e:
|
44 |
-
return {"success": False, "message": f"读取API密钥文件失败: {str(e)}"}
|
45 |
-
else:
|
46 |
-
return {"success": False, "message": f"未找到ID为 {key_id} 的API密钥"}
|
47 |
-
else:
|
48 |
-
key_data = dict(row)
|
49 |
-
|
50 |
-
# 获取平台和密钥
|
51 |
-
platform = key_data.get("platform")
|
52 |
-
api_key = key_data.get("key")
|
53 |
-
|
54 |
-
# 获取API管理器
|
55 |
-
api_manager = get_api_manager()
|
56 |
-
|
57 |
-
# 调用API管理器验证密钥
|
58 |
-
try:
|
59 |
-
result = api_manager.execute(platform, "validate_api_key", api_key)
|
60 |
-
except Exception as e:
|
61 |
-
return {"success": False, "message": f"验证API密钥时出错: {str(e)}"}
|
62 |
-
|
63 |
-
# 当前时间
|
64 |
-
current_time = datetime.now().isoformat()
|
65 |
-
|
66 |
-
# 将布尔值转换为整数
|
67 |
-
success_int = 1 if result.get("success", False) else 0
|
68 |
-
|
69 |
-
# 更新密钥信息到SQLite数据库
|
70 |
-
try:
|
71 |
-
cursor.execute('''
|
72 |
-
UPDATE api_keys
|
73 |
-
SET states = ?, balance = ?, success = ?, return_message = ?, updated_at = ?
|
74 |
-
WHERE id = ?
|
75 |
-
''', (
|
76 |
-
result.get("states", ""),
|
77 |
-
result.get("balance", 0),
|
78 |
-
success_int,
|
79 |
-
result.get("return_message", ""),
|
80 |
-
current_time,
|
81 |
-
key_id
|
82 |
-
))
|
83 |
-
|
84 |
-
# 如果数据库中没有此记录(可能是旧的JSON格式数据),则插入新记录
|
85 |
-
if cursor.rowcount == 0 and 'id' in key_data:
|
86 |
-
cursor.execute('''
|
87 |
-
INSERT OR REPLACE INTO api_keys
|
88 |
-
(id, platform, name, key, created_at, updated_at, success, return_message, states, balance)
|
89 |
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
90 |
-
''', (
|
91 |
-
key_data.get("id"),
|
92 |
-
key_data.get("platform"),
|
93 |
-
key_data.get("name"),
|
94 |
-
key_data.get("key"),
|
95 |
-
key_data.get("created_at", current_time),
|
96 |
-
current_time,
|
97 |
-
success_int,
|
98 |
-
result.get("return_message", ""),
|
99 |
-
result.get("states", ""),
|
100 |
-
result.get("balance", 0)
|
101 |
-
))
|
102 |
-
|
103 |
-
conn.commit()
|
104 |
-
|
105 |
-
# 获取更新后的完整记录
|
106 |
-
cursor.execute('SELECT * FROM api_keys WHERE id = ?', (key_id,))
|
107 |
-
updated_row = cursor.fetchone()
|
108 |
-
|
109 |
-
if updated_row:
|
110 |
-
updated_data = dict(updated_row)
|
111 |
-
# 将布尔值转换为布尔类型
|
112 |
-
updated_data['success'] = bool(updated_data['success'])
|
113 |
-
|
114 |
-
return {
|
115 |
-
"success": True,
|
116 |
-
"message": "API密钥更新成功",
|
117 |
-
"data": updated_data
|
118 |
-
}
|
119 |
-
else:
|
120 |
-
# 如果以某种方式在更新过程中密钥被删除
|
121 |
-
return {"success": False, "message": f"更新期间ID为 {key_id} 的API密钥已被删除"}
|
122 |
-
|
123 |
-
except sqlite3.Error as e:
|
124 |
-
conn.rollback()
|
125 |
-
return {"success": False, "message": f"更新数据库中的API密钥失败: {str(e)}"}
|
126 |
-
|
127 |
-
except sqlite3.Error as e:
|
128 |
-
return {"success": False, "message": f"从数据库获取API密钥时出错: {str(e)}"}
|
129 |
-
finally:
|
130 |
-
if conn:
|
131 |
-
conn.close()
|
132 |
-
|
133 |
-
if __name__ == "__main__":
|
134 |
-
# 可以在这里添加命令行参数解析的代码,用于直接从命令行调用
|
135 |
-
import sys
|
136 |
-
if len(sys.argv) > 1:
|
137 |
-
key_id = sys.argv[1]
|
138 |
-
result = update(key_id)
|
139 |
-
print(json.dumps(result, indent=2, ensure_ascii=False))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|