mirror of
https://github.com/A-Minos/nonebot-plugin-tetris-stats.git
synced 2026-03-05 05:36:54 +08:00
* ➕ 添加依赖 yarl * ➕ 添加依赖 msgspec * ➖ 移除依赖 ujson * ♻️ 重构 request 使其支持分别设置代理 * ♻️ 重构 resource 接口 * ⚡️ 不再重复获取 Config * ♻️ 使用 yarl 替换 urllib.parse * ⚡️ 给 get_self_netloc 加个 cache * ✨ request 使用 proxy * ✨ 更新模板使用 proxy * 🐛 修复删除 ujson 依赖后 迁移脚本报错的bug
56 lines
1.6 KiB
Python
56 lines
1.6 KiB
Python
from typing import Literal, NamedTuple, overload
|
|
|
|
from msgspec import Struct, to_builtins
|
|
from nonebot.compat import type_validate_json
|
|
|
|
from ....utils.exception import RequestError
|
|
from ..constant import BASE_URL
|
|
from .cache import Cache
|
|
from .schemas.base import FailedModel
|
|
from .schemas.tetra_league import TetraLeague, TetraLeagueSuccess
|
|
|
|
|
|
class Parameter(Struct, omit_defaults=True):
|
|
after: float | None = None
|
|
before: float | None = None
|
|
limit: int | None = None
|
|
country: str | None = None
|
|
|
|
|
|
async def leaderboard(parameter: Parameter | None = None) -> TetraLeagueSuccess:
|
|
league: TetraLeague = type_validate_json(
|
|
TetraLeague, # type: ignore[arg-type]
|
|
(await Cache.get(BASE_URL / 'users/lists/league' % to_builtins(parameter))),
|
|
)
|
|
if isinstance(league, FailedModel):
|
|
msg = f'排行榜数据请求错误:\n{league.error}'
|
|
raise RequestError(msg)
|
|
return league
|
|
|
|
|
|
class FullExport(NamedTuple):
|
|
model: TetraLeagueSuccess
|
|
original: bytes
|
|
|
|
|
|
@overload
|
|
async def full_export(*, with_original: Literal[False]) -> TetraLeagueSuccess: ...
|
|
|
|
|
|
@overload
|
|
async def full_export(*, with_original: Literal[True]) -> FullExport: ...
|
|
|
|
|
|
async def full_export(*, with_original: bool) -> TetraLeagueSuccess | FullExport:
|
|
full: TetraLeague = type_validate_json(
|
|
TetraLeague, # type: ignore[arg-type]
|
|
(data := await Cache.get(BASE_URL / 'users/lists/league/all')),
|
|
)
|
|
|
|
if isinstance(full, FailedModel):
|
|
msg = f'排行榜数据请求错误:\n{full.error}'
|
|
raise RequestError(msg)
|
|
if with_original:
|
|
return FullExport(full, data)
|
|
return full
|