Skip to content

Commit

Permalink
✨ 使用随机/特殊 UA
Browse files Browse the repository at this point in the history
  • Loading branch information
shoucandanghehe committed Dec 18, 2024
1 parent 2167fe1 commit 0059844
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
9 changes: 8 additions & 1 deletion nonebot_plugin_tetris_stats/games/tetrio/api/leaderboards.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from typing import Literal, overload
from uuid import UUID

from nonebot import __version__ as __nonebot_version__
from nonebot.compat import type_validate_json
from yarl import URL

from ....utils.exception import RequestError
from ....version import __version__
from ..constant import BASE_URL
from .cache import Cache
from .schemas.base import FailedModel
Expand All @@ -22,7 +24,12 @@ async def by(
await get(
BASE_URL / f'users/by/{by_type}',
parameter,
{'X-Session-ID': str(x_session_id)} if x_session_id is not None else None,
{
'X-Session-ID': str(x_session_id),
'User-Agent': f'nonebot-plugin-tetris-stats/{__version__} (Windows NT 10.0; Win64; x64) NoneBot2/{__nonebot_version__}',
}
if x_session_id is not None
else None,
),
)
if isinstance(model, FailedModel):
Expand Down
27 changes: 18 additions & 9 deletions nonebot_plugin_tetris_stats/utils/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from http import HTTPStatus
from typing import Any

from fake_useragent import UserAgent
from httpx import AsyncClient, HTTPError
from msgspec import DecodeError, Struct, json
from nonebot import get_driver
Expand Down Expand Up @@ -113,6 +114,8 @@ class Request:
def __init__(self, proxy: str | None) -> None:
self.proxy = proxy
self.anti_cloudflares: dict[str, AntiCloudflare] = {}
self.client = AsyncClient(timeout=config.tetris.request_timeout, proxy=self.proxy)
self.ua = UserAgent()

async def request(
self,
Expand All @@ -129,23 +132,29 @@ async def request(
else:
cookies = None
headers = None
headers = headers if extra_headers is None else extra_headers if headers is None else headers | extra_headers
if headers is None:
headers = {}
if extra_headers:
headers.update(extra_headers)
headers.setdefault('User-Agent', self.ua.random)
try:
async with AsyncClient(cookies=cookies, timeout=config.tetris.request_timeout, proxy=self.proxy) as session:
response = await session.get(str(url), headers=headers)
if response.status_code != HTTPStatus.OK:
msg = f'请求错误 code: {response.status_code} {HTTPStatus(response.status_code).phrase}\n{response.text}'
raise RequestError(msg, status_code=response.status_code)
if is_json:
decoder.decode(response.content)
return response.content
response = await self.client.get(str(url), cookies=cookies, headers=headers)
if response.status_code != HTTPStatus.OK:
msg = (
f'请求错误 code: {response.status_code} {HTTPStatus(response.status_code).phrase}\n{response.text}'
)
raise RequestError(msg, status_code=response.status_code)
if is_json:
decoder.decode(response.content)
except HTTPError as e:
msg = f'请求错误 \n{e!r}'
raise RequestError(msg) from e
except DecodeError: # 由于捕获的是 DecodeError 所以一定是 is_json = True
if enable_anti_cloudflare and url.host is not None:
return await self.anti_cloudflares.setdefault(url.host, AntiCloudflare(url.host))(str(url), self.proxy)
raise
else:
return response.content

async def failover_request(
self,
Expand Down

0 comments on commit 0059844

Please sign in to comment.