💥 插件配置现在使用 ScopedConfig

This commit is contained in:
2024-08-16 18:52:47 +08:00
parent 9fb176b4bc
commit c8907a47a4
3 changed files with 9 additions and 7 deletions

View File

@@ -1,13 +1,15 @@
from pathlib import Path from pathlib import Path
from nonebot_plugin_localstore import get_cache_dir from nonebot_plugin_localstore import get_cache_dir
from pydantic import BaseModel from pydantic import BaseModel, Field
CACHE_PATH: Path = get_cache_dir('nonebot_plugin_tetris_stats') CACHE_PATH: Path = get_cache_dir('nonebot_plugin_tetris_stats')
class Config(BaseModel): class ScopedConfig(BaseModel):
"""配置类""" request_timeout: float = 30.0
screenshot_quality: float = 2
tetris_req_timeout: float = 30.0
tetris_screenshot_quality: float = 2 class Config(BaseModel):
tetris: ScopedConfig = Field(default_factory=ScopedConfig)

View File

@@ -119,7 +119,7 @@ class Request:
async def request(cls, url: str, *, is_json: bool = True) -> bytes: async def request(cls, url: str, *, is_json: bool = True) -> bytes:
"""请求api""" """请求api"""
try: try:
async with AsyncClient(cookies=cls._cookies, timeout=config.tetris_req_timeout) as session: async with AsyncClient(cookies=cls._cookies, timeout=config.tetris.request_timeout) as session:
response = await session.get(url, headers=cls._headers) response = await session.get(url, headers=cls._headers)
if response.status_code != HTTPStatus.OK: if response.status_code != HTTPStatus.OK:
msg = f'请求错误 code: {response.status_code} {HTTPStatus(response.status_code).phrase}\n{response.text}' msg = f'请求错误 code: {response.status_code} {HTTPStatus(response.status_code).phrase}\n{response.text}'

View File

@@ -14,7 +14,7 @@ config = get_plugin_config(Config)
async def screenshot(url: str) -> bytes: async def screenshot(url: str) -> bytes:
browser = await BrowserManager.get_browser() browser = await BrowserManager.get_browser()
async with ( async with (
await browser.new_page(device_scale_factor=config.tetris_screenshot_quality) as page, await browser.new_page(device_scale_factor=config.tetris.screenshot_quality) as page,
): ):
await page.goto(url) await page.goto(url)
await page.wait_for_load_state('networkidle') await page.wait_for_load_state('networkidle')