Files
Onekey/common/check.py
2024-10-01 11:54:32 +08:00

31 lines
1.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import time
import ujson as json
from aiohttp import ClientError
from .log import log
from .stack_error import stack_error
async def check_github_api_rate_limit(headers, session):
url = 'https://api.github.com/rate_limit'
try:
async with session.get(url, headers=headers, ssl=False) as r:
r_json = await r.json()
if r.status == 200:
rate_limit = r_json.get('rate', {})
remaining_requests = rate_limit.get('remaining', 0)
reset_time = rate_limit.get('reset', 0)
reset_time_formatted = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(reset_time))
log.info(f' 🔄 剩余请求次数: {remaining_requests}')
if remaining_requests == 0:
log.warning(f'⚠ GitHub API 请求数已用尽,将在 {reset_time_formatted} 重置,建议生成一个填在配置文件里')
else:
log.error('⚠ Github请求数检查失败网络错误')
except ClientError as e:
log.error(f'⚠ 检查Github API 请求数失败,{stack_error(e)}')
except Exception as e:
log.error(f'⚠ 发生错误: {stack_error(e)}')