Update app.py
Browse files
app.py
CHANGED
|
@@ -8,54 +8,51 @@ from apscheduler.schedulers.background import BackgroundScheduler
|
|
| 8 |
logging.basicConfig(level=logging.INFO,
|
| 9 |
format='%(asctime)s - %(levelname)s - %(message)s')
|
| 10 |
|
| 11 |
-
API_ENDPOINT = "https://api.siliconflow.cn/v1/usage/
|
| 12 |
|
| 13 |
-
def
|
| 14 |
"""
|
| 15 |
-
|
| 16 |
|
| 17 |
Args:
|
| 18 |
-
|
| 19 |
|
| 20 |
Returns:
|
| 21 |
-
|
| 22 |
"""
|
| 23 |
headers = {
|
| 24 |
-
"Authorization": f"Bearer {
|
|
|
|
| 25 |
}
|
| 26 |
try:
|
| 27 |
response = requests.get(API_ENDPOINT, headers=headers)
|
| 28 |
-
response.raise_for_status() #
|
| 29 |
-
|
| 30 |
-
return data
|
| 31 |
except requests.exceptions.RequestException as e:
|
| 32 |
-
logging.error(f"
|
| 33 |
-
|
| 34 |
-
logging.error(f"解析 JSON 响应时出错,key:{key}, 错误信息:{e}")
|
| 35 |
-
return None
|
| 36 |
|
| 37 |
-
def
|
| 38 |
"""
|
| 39 |
-
从环境变量中加载 keys
|
| 40 |
"""
|
| 41 |
keys_str = os.environ.get("KEYS")
|
| 42 |
if keys_str:
|
| 43 |
keys = [key.strip() for key in keys_str.split(',')]
|
| 44 |
logging.info(f"加载的 keys:{keys}")
|
|
|
|
| 45 |
for key in keys:
|
| 46 |
-
|
| 47 |
-
if
|
| 48 |
-
logging.info(f"Key
|
| 49 |
-
else:
|
| 50 |
-
logging.warning(f"获取 key:{key} 的额度信息失败。")
|
| 51 |
else:
|
| 52 |
logging.warning("环境变量 KEYS 未设置。")
|
| 53 |
|
| 54 |
# 创建一个后台调度器
|
| 55 |
scheduler = BackgroundScheduler()
|
| 56 |
|
| 57 |
-
# 添加定时任务,每小时执行一次
|
| 58 |
-
scheduler.add_job(
|
| 59 |
|
| 60 |
# 启动调度器
|
| 61 |
scheduler.start()
|
|
|
|
| 8 |
logging.basicConfig(level=logging.INFO,
|
| 9 |
format='%(asctime)s - %(levelname)s - %(message)s')
|
| 10 |
|
| 11 |
+
API_ENDPOINT = "https://api.siliconflow.cn/v1/usage/credit_summary"
|
| 12 |
|
| 13 |
+
def get_credit_summary(api_key):
|
| 14 |
"""
|
| 15 |
+
使用 API 密钥获取额度信息。
|
| 16 |
|
| 17 |
Args:
|
| 18 |
+
api_key: 用于访问 API 的密钥。
|
| 19 |
|
| 20 |
Returns:
|
| 21 |
+
包含额度信息的字典,如果请求失败则返回 None。
|
| 22 |
"""
|
| 23 |
headers = {
|
| 24 |
+
"Authorization": f"Bearer {api_key}",
|
| 25 |
+
"Content-Type": "application/json"
|
| 26 |
}
|
| 27 |
try:
|
| 28 |
response = requests.get(API_ENDPOINT, headers=headers)
|
| 29 |
+
response.raise_for_status() # 如果响应状态码不是 2xx,则引发异常
|
| 30 |
+
return response.json()
|
|
|
|
| 31 |
except requests.exceptions.RequestException as e:
|
| 32 |
+
logging.error(f"获取额度信息失败,API Key:{api_key},错误信息:{e}")
|
| 33 |
+
return None
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
def load_keys():
|
| 36 |
"""
|
| 37 |
+
从环境变量中加载 keys,并记录到日志中,同时获取并记录每个 key 的额度信息。
|
| 38 |
"""
|
| 39 |
keys_str = os.environ.get("KEYS")
|
| 40 |
if keys_str:
|
| 41 |
keys = [key.strip() for key in keys_str.split(',')]
|
| 42 |
logging.info(f"加载的 keys:{keys}")
|
| 43 |
+
|
| 44 |
for key in keys:
|
| 45 |
+
credit_summary = get_credit_summary(key)
|
| 46 |
+
if credit_summary:
|
| 47 |
+
logging.info(f"API Key:{key},额度信息:{credit_summary}")
|
|
|
|
|
|
|
| 48 |
else:
|
| 49 |
logging.warning("环境变量 KEYS 未设置。")
|
| 50 |
|
| 51 |
# 创建一个后台调度器
|
| 52 |
scheduler = BackgroundScheduler()
|
| 53 |
|
| 54 |
+
# 添加定时任务,每小时执行一次 load_keys 函数
|
| 55 |
+
scheduler.add_job(load_keys, 'interval', hours=1)
|
| 56 |
|
| 57 |
# 启动调度器
|
| 58 |
scheduler.start()
|