Files
Onekey/common/check.py
2024-10-19 11:18:18 +08:00

34 lines
1.4 KiB
Python

import time
from aiohttp import ClientError, ConnectionTimeoutError
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 KeyboardInterrupt:
log.info("程序已退出")
except ClientError as e:
log.error(f'检查Github API 请求数失败,{stack_error(e)}')
except ConnectionTimeoutError as e:
log.error(f'检查Github API 请求数超时: {stack_error(e)}')
except Exception as e:
log.error(f'发生错误: {stack_error(e)}')