|
"""
|
|
API密钥更新模块 - 提供API密钥的验证和更新功能
|
|
"""
|
|
import json
|
|
import os
|
|
import sqlite3
|
|
import pytz
|
|
from datetime import datetime
|
|
from core.api_manager import get_api_manager
|
|
from utils.db import get_db_connection
|
|
from config import API_KEYS_FILE
|
|
|
|
def update(key_id):
|
|
"""
|
|
更新指定ID的API密钥
|
|
|
|
Args:
|
|
key_id (str): 要更新的API密钥ID
|
|
|
|
Returns:
|
|
dict: 包含更新结果的字典,成功时返回更新后的密钥信息,失败时返回错误信息
|
|
"""
|
|
|
|
conn = get_db_connection()
|
|
try:
|
|
cursor = conn.cursor()
|
|
cursor.execute('SELECT * FROM api_keys WHERE id = ?', (key_id,))
|
|
row = cursor.fetchone()
|
|
|
|
if row is None:
|
|
|
|
|
|
if os.path.exists(API_KEYS_FILE):
|
|
try:
|
|
with open(API_KEYS_FILE, "r", encoding="utf-8") as f:
|
|
data = json.load(f)
|
|
|
|
for key in data.get("api_keys", []):
|
|
if key.get("id") == key_id:
|
|
key_data = key
|
|
break
|
|
else:
|
|
return {"success": False, "message": f"未找到ID为 {key_id} 的API密钥"}
|
|
except Exception as e:
|
|
return {"success": False, "message": f"读取API密钥文件失败: {str(e)}"}
|
|
else:
|
|
return {"success": False, "message": f"未找到ID为 {key_id} 的API密钥"}
|
|
else:
|
|
key_data = dict(row)
|
|
|
|
|
|
platform = key_data.get("platform")
|
|
api_key = key_data.get("key")
|
|
|
|
|
|
api_manager = get_api_manager()
|
|
|
|
|
|
try:
|
|
result = api_manager.execute(platform, "validate_api_key", api_key)
|
|
except Exception as e:
|
|
return {"success": False, "message": f"验证API密钥时出错: {str(e)}"}
|
|
|
|
|
|
current_time = datetime.now(pytz.timezone('Asia/Shanghai')).isoformat()
|
|
|
|
|
|
success_int = 1 if result.get("success", False) else 0
|
|
|
|
|
|
try:
|
|
cursor.execute('''
|
|
UPDATE api_keys
|
|
SET states = ?, balance = ?, success = ?, return_message = ?, updated_at = ?
|
|
WHERE id = ?
|
|
''', (
|
|
result.get("states", ""),
|
|
result.get("balance", 0),
|
|
success_int,
|
|
result.get("return_message", ""),
|
|
current_time,
|
|
key_id
|
|
))
|
|
|
|
|
|
if cursor.rowcount == 0 and 'id' in key_data:
|
|
cursor.execute('''
|
|
INSERT OR REPLACE INTO api_keys
|
|
(id, platform, name, key, created_at, updated_at, success, return_message, states, balance)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
''', (
|
|
key_data.get("id"),
|
|
key_data.get("platform"),
|
|
key_data.get("name"),
|
|
key_data.get("key"),
|
|
key_data.get("created_at", current_time),
|
|
current_time,
|
|
success_int,
|
|
result.get("return_message", ""),
|
|
result.get("states", ""),
|
|
result.get("balance", 0)
|
|
))
|
|
|
|
conn.commit()
|
|
|
|
|
|
cursor.execute('SELECT * FROM api_keys WHERE id = ?', (key_id,))
|
|
updated_row = cursor.fetchone()
|
|
|
|
if updated_row:
|
|
updated_data = dict(updated_row)
|
|
|
|
updated_data['success'] = bool(updated_data['success'])
|
|
|
|
return {
|
|
"success": True,
|
|
"message": "API密钥更新成功",
|
|
"data": updated_data
|
|
}
|
|
else:
|
|
|
|
return {"success": False, "message": f"更新期间ID为 {key_id} 的API密钥已被删除"}
|
|
|
|
except sqlite3.Error as e:
|
|
conn.rollback()
|
|
return {"success": False, "message": f"更新数据库中的API密钥失败: {str(e)}"}
|
|
|
|
except sqlite3.Error as e:
|
|
return {"success": False, "message": f"从数据库获取API密钥时出错: {str(e)}"}
|
|
finally:
|
|
if conn:
|
|
conn.close()
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import sys
|
|
if len(sys.argv) > 1:
|
|
key_id = sys.argv[1]
|
|
result = update(key_id)
|
|
print(json.dumps(result, indent=2, ensure_ascii=False))
|
|
|