适配新赛季 rank

This commit is contained in:
2024-08-24 21:06:45 +08:00
parent 97e2abed78
commit 6293d088db
21 changed files with 778 additions and 193 deletions

View File

@@ -0,0 +1,33 @@
from asyncio import Lock, sleep
from collections.abc import Callable, Coroutine
from datetime import timedelta
from functools import wraps
from time import time
from typing import Any, ParamSpec, TypeVar
from nonebot.log import logger
P = ParamSpec('P')
T = TypeVar('T')
def limit(limit: timedelta) -> Callable[[Callable[P, Coroutine[Any, Any, T]]], Callable[P, Coroutine[Any, Any, T]]]:
limit_seconds = limit.total_seconds()
def decorator(func: Callable[P, Coroutine[Any, Any, T]]) -> Callable[P, Coroutine[Any, Any, T]]:
last_call = 0.0
lock = Lock()
@wraps(func)
async def wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
nonlocal last_call
async with lock:
if (diff := (time() - last_call)) < limit_seconds:
logger.debug(f'request limit {(limit_time:=limit_seconds-diff)}s')
await sleep(limit_time)
last_call = time()
return await func(*args, **kwargs)
return wrapper
return decorator

View File

@@ -117,6 +117,7 @@ class Request:
async def request(
self,
url: URL,
extra_headers: dict | None = None,
*,
is_json: bool = True,
enable_anti_cloudflare: bool = False,
@@ -128,6 +129,7 @@ class Request:
else:
cookies = None
headers = None
headers = headers if extra_headers is None else extra_headers if headers is None else headers | extra_headers
try:
async with AsyncClient(cookies=cookies, timeout=config.tetris.request_timeout, proxy=self.proxy) as session:
response = await session.get(str(url), headers=headers)