From d6b8d3b44e8edf5340560bde19e3093d15665aaa Mon Sep 17 00:00:00 2001 From: suyiiyii Date: Tue, 29 Oct 2024 23:13:26 +0800 Subject: [PATCH] :pencil2: small fixs --- nonebot_bison/platform/bilibili/scheduler.py | 4 +--- nonebot_bison/scheduler/scheduler.py | 2 +- nonebot_bison/sub_manager/__init__.py | 2 ++ nonebot_bison/sub_manager/utils.py | 2 +- nonebot_bison/utils/site.py | 17 ++++++++++------- 5 files changed, 15 insertions(+), 12 deletions(-) diff --git a/nonebot_bison/platform/bilibili/scheduler.py b/nonebot_bison/platform/bilibili/scheduler.py index ea94545..1dddbec 100644 --- a/nonebot_bison/platform/bilibili/scheduler.py +++ b/nonebot_bison/platform/bilibili/scheduler.py @@ -24,9 +24,8 @@ B = TypeVar("B", bound="Bilibili") class BilibiliClientManager(CookieClientManager): - _default_cookie_cd: int = timedelta(seconds=120) + _default_cookie_cd = timedelta(seconds=120) - @classmethod async def _get_cookies(self) -> list[Cookie]: browser = await get_browser() async with await browser.new_page() as page: @@ -38,7 +37,6 @@ class BilibiliClientManager(CookieClientManager): return cookies - @classmethod def _gen_json_cookie(self, cookies: list[Cookie]): cookie_dict = {} for cookie in cookies: diff --git a/nonebot_bison/scheduler/scheduler.py b/nonebot_bison/scheduler/scheduler.py index 3b2df21..f94ea87 100644 --- a/nonebot_bison/scheduler/scheduler.py +++ b/nonebot_bison/scheduler/scheduler.py @@ -40,8 +40,8 @@ class Scheduler: logger.error(f"scheduler config [{self.name}] not found, exiting") raise RuntimeError(f"{self.name} not found") self.scheduler_config = scheduler_config - self.scheduler_config_obj = self.scheduler_config() self.client_mgr = scheduler_config.client_mgr() + self.scheduler_config_obj = self.scheduler_config() self.schedulable_list = [] self.batch_platform_name_targets_cache = defaultdict(list) diff --git a/nonebot_bison/sub_manager/__init__.py b/nonebot_bison/sub_manager/__init__.py index 6791b09..9a3e5c2 100644 --- a/nonebot_bison/sub_manager/__init__.py +++ b/nonebot_bison/sub_manager/__init__.py @@ -30,10 +30,12 @@ add_sub_matcher = on_command( add_sub_matcher.handle()(set_target_user_info) do_add_sub(add_sub_matcher) + query_sub_matcher = on_command("查询订阅", rule=configurable_to_me, priority=5, block=True) query_sub_matcher.handle()(set_target_user_info) do_query_sub(query_sub_matcher) + del_sub_matcher = on_command( "删除订阅", rule=configurable_to_me, diff --git a/nonebot_bison/sub_manager/utils.py b/nonebot_bison/sub_manager/utils.py index 45958d6..8126b9a 100644 --- a/nonebot_bison/sub_manager/utils.py +++ b/nonebot_bison/sub_manager/utils.py @@ -70,7 +70,7 @@ def admin_permission(): async def generate_sub_list_text( matcher: type[Matcher], state: T_State, - user_info: PlatformTarget = None, + user_info: PlatformTarget | None = None, is_index=False, is_show_cookie=False, is_hide_no_cookie_platfrom=False, diff --git a/nonebot_bison/utils/site.py b/nonebot_bison/utils/site.py index f9e4308..141c717 100644 --- a/nonebot_bison/utils/site.py +++ b/nonebot_bison/utils/site.py @@ -2,6 +2,7 @@ import json from typing import Literal from json import JSONDecodeError from abc import ABC, abstractmethod +from collections.abc import Callable from datetime import datetime, timedelta import httpx @@ -42,8 +43,14 @@ class DefaultClientManager(ClientManager): pass +class SkipRequestException(Exception): + """跳过请求异常,如果需要在选择 Cookie 时跳过此次请求,可以抛出此异常""" + + pass + + class CookieClientManager(ClientManager): - _default_cookie_cd: int = timedelta(seconds=15) + _default_cookie_cd = timedelta(seconds=15) _site_name: str = "" async def _generate_anonymous_cookie(self) -> Cookie: @@ -98,7 +105,7 @@ class CookieClientManager(ClientManager): return False return True - def _generate_hook(self, cookie: Cookie) -> callable: + def _generate_hook(self, cookie: Cookie) -> Callable: """hook 函数生成器,用于回写请求状态到数据库""" async def _response_hook(resp: httpx.Response): @@ -132,7 +139,7 @@ class CookieClientManager(ClientManager): return await self._assemble_client(client, cookie) async def _assemble_client(self, client, cookie) -> AsyncClient: - """组装 client,可以自定义 cookie 对象的 content 装配到 client 中的方式""" + """组装 client,可以自定义 cookie 对象装配到 client 中的方式""" cookies = httpx.Cookies() if cookie: cookies.update(json.loads(cookie.content)) @@ -185,7 +192,3 @@ def anonymous_site(schedule_type: Literal["date", "interval", "cron"], schedule_ "client_mgr": DefaultClientManager, }, ) - - -class SkipRequestException(Exception): - pass