From c96dd85b226a30a29c885e96fc361256eadd70d0 Mon Sep 17 00:00:00 2001 From: Azide Date: Tue, 19 Mar 2024 10:27:37 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E7=8B=AC=E7=AB=8B=20bilibili-live?= =?UTF-8?q?=20=E7=9A=84=20SchedConf=20(#473)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * :sparkles: 独立 bilibili-live 的 SchedConf * :white_check_mark: 修正测试 * :sparkles: bilibili和bilibili-live共用http_client * :bug: 不在__init__中修改_client_refresh_time * :sparkles: 将刷新浏览器变更为类方法 * :recycle: seperate BilibiliClient Co-authored-by: felinea98 --------- Co-authored-by: felinea98 --- nonebot_bison/platform/bilibili.py | 58 ++++++++++++++++++--------- tests/platforms/test_bilibili_live.py | 21 ++++++++++ tests/scheduler/test_scheduler.py | 22 +++++----- 3 files changed, 72 insertions(+), 29 deletions(-) diff --git a/nonebot_bison/platform/bilibili.py b/nonebot_bison/platform/bilibili.py index 90d9178..7e1e7ab 100644 --- a/nonebot_bison/platform/bilibili.py +++ b/nonebot_bison/platform/bilibili.py @@ -1,5 +1,6 @@ import re import json +from abc import ABC from typing import Any from copy import deepcopy from enum import Enum, unique @@ -14,41 +15,62 @@ from nonebot.compat import type_validate_python from nonebot_bison.compat import model_rebuild from ..post import Post -from ..utils import SchedulerConfig, text_similarity from ..types import Tag, Target, RawPost, ApiError, Category +from ..utils import SchedulerConfig, http_client, text_similarity from .platform import NewMessage, StatusChange, CategoryNotSupport, CategoryNotRecognize -class BilibiliSchedConf(SchedulerConfig): - name = "bilibili.com" - schedule_type = "interval" - schedule_setting = {"seconds": 10} - - _client_refresh_time: datetime +class BilibiliClient: + _client: AsyncClient + _refresh_time: datetime cookie_expire_time = timedelta(hours=5) - def __init__(self): - self._client_refresh_time = datetime(year=2000, month=1, day=1) # an expired time - super().__init__() + def __init__(self) -> None: + self._client = http_client() + self._refresh_time = datetime(year=2000, month=1, day=1) # an expired time async def _init_session(self): - res = await self.default_http_client.get("https://www.bilibili.com/") + res = await self._client.get("https://www.bilibili.com/") if res.status_code != 200: logger.warning("unable to refresh temp cookie") else: - self._client_refresh_time = datetime.now() + self._refresh_time = datetime.now() async def _refresh_client(self): - if datetime.now() - self._client_refresh_time > self.cookie_expire_time: + if datetime.now() - self._refresh_time > self.cookie_expire_time: await self._init_session() - async def get_client(self, target: Target) -> AsyncClient: + async def get_client(self) -> AsyncClient: await self._refresh_client() - return await super().get_client(target) + return self._client + + +bilibili_client = BilibiliClient() + + +class BaseSchedConf(ABC, SchedulerConfig): + schedule_type = "interval" + bilibili_client: BilibiliClient + + def __init__(self): + super().__init__() + self.bilibili_client = bilibili_client + + async def get_client(self, _: Target) -> AsyncClient: + return await self.bilibili_client.get_client() async def get_query_name_client(self) -> AsyncClient: - await self._refresh_client() - return await super().get_query_name_client() + return await self.bilibili_client.get_client() + + +class BilibiliSchedConf(BaseSchedConf): + name = "bilibili.com" + schedule_setting = {"seconds": 10} + + +class BililiveSchedConf(BaseSchedConf): + name = "live.bilibili.com" + schedule_setting = {"seconds": 3} class Bilibili(NewMessage): @@ -205,7 +227,7 @@ class Bilibililive(StatusChange): enable_tag = False enabled = True is_common = True - scheduler = BilibiliSchedConf + scheduler = BililiveSchedConf name = "Bilibili直播" has_target = True use_batch = True diff --git a/tests/platforms/test_bilibili_live.py b/tests/platforms/test_bilibili_live.py index be46f1a..f6208cc 100644 --- a/tests/platforms/test_bilibili_live.py +++ b/tests/platforms/test_bilibili_live.py @@ -30,6 +30,27 @@ def dummy_only_open_user_subinfo(app: App): return UserSubInfo(user=user, categories=[1], tags=[]) +@pytest.mark.asyncio +async def test_http_client_equal(app: App): + from nonebot_bison.types import Target + from nonebot_bison.utils import ProcessContext + from nonebot_bison.platform import platform_manager + + empty_target = Target("0") + + bilibili = platform_manager["bilibili"](ProcessContext(), AsyncClient()) + bilibili_live = platform_manager["bilibili-live"](ProcessContext(), AsyncClient()) + + bilibili_scheduler = bilibili.scheduler() + bilibili_live_scheduler = bilibili_live.scheduler() + + assert await bilibili_scheduler.get_client(empty_target) == await bilibili_live_scheduler.get_client(empty_target) + assert await bilibili_live_scheduler.get_client(empty_target) != bilibili_live_scheduler.default_http_client + + assert await bilibili_scheduler.get_query_name_client() == await bilibili_live_scheduler.get_query_name_client() + assert await bilibili_scheduler.get_query_name_client() != bilibili_live_scheduler.default_http_client + + @pytest.mark.asyncio @respx.mock async def test_fetch_bililive_no_room(bili_live, dummy_only_open_user_subinfo): diff --git a/tests/scheduler/test_scheduler.py b/tests/scheduler/test_scheduler.py index 69bd1bb..198883c 100644 --- a/tests/scheduler/test_scheduler.py +++ b/tests/scheduler/test_scheduler.py @@ -34,22 +34,22 @@ async def test_scheduler_without_time(init_scheduler): await config.add_subscribe(TargetQQGroup(group_id=123), T_Target("t1"), "target1", "bilibili", [], []) await config.add_subscribe(TargetQQGroup(group_id=123), T_Target("t2"), "target1", "bilibili", [], []) - await config.add_subscribe(TargetQQGroup(group_id=123), T_Target("t2"), "target1", "bilibili-live", [], []) + await config.add_subscribe(TargetQQGroup(group_id=123), T_Target("t2"), "target1", "bilibili-bangumi", [], []) await config.update_time_weight_config(T_Target("t2"), "bilibili", WeightConfig(default=20, time_config=[])) - await config.update_time_weight_config(T_Target("t2"), "bilibili-live", WeightConfig(default=30, time_config=[])) + await config.update_time_weight_config(T_Target("t2"), "bilibili-bangumi", WeightConfig(default=30, time_config=[])) await init_scheduler() static_res = await get_schedule_times(BilibiliSchedConf, 6) assert static_res["bilibili-t1"] == 1 assert static_res["bilibili-t2"] == 2 - assert static_res["bilibili-live-t2"] == 3 + assert static_res["bilibili-bangumi-t2"] == 3 static_res = await get_schedule_times(BilibiliSchedConf, 6) assert static_res["bilibili-t1"] == 1 assert static_res["bilibili-t2"] == 2 - assert static_res["bilibili-live-t2"] == 3 + assert static_res["bilibili-bangumi-t2"] == 3 async def test_scheduler_batch_api(init_scheduler, mocker: MockerFixture): @@ -60,12 +60,12 @@ async def test_scheduler_batch_api(init_scheduler, mocker: MockerFixture): from nonebot_bison.scheduler import scheduler_dict from nonebot_bison.types import Target as T_Target from nonebot_bison.scheduler.manager import init_scheduler - from nonebot_bison.platform.bilibili import BilibiliSchedConf + from nonebot_bison.platform.bilibili import BililiveSchedConf await config.add_subscribe(TargetQQGroup(group_id=123), T_Target("t1"), "target1", "bilibili-live", [], []) await config.add_subscribe(TargetQQGroup(group_id=123), T_Target("t2"), "target2", "bilibili-live", [], []) - mocker.patch.object(BilibiliSchedConf, "get_client", return_value=AsyncClient()) + mocker.patch.object(BililiveSchedConf, "get_client", return_value=AsyncClient()) await init_scheduler() @@ -81,7 +81,7 @@ async def test_scheduler_batch_api(init_scheduler, mocker: MockerFixture): {"bilibili-live": mocker.Mock(return_value=fake_platform_obj)}, ) - await scheduler_dict[BilibiliSchedConf].exec_fetch() + await scheduler_dict[BililiveSchedConf].exec_fetch() batch_fetch_mock.assert_called_once_with([ (T_Target("t1"), [UserSubInfo(user=TargetQQGroup(group_id=123), categories=[], tags=[])]), @@ -100,7 +100,7 @@ async def test_scheduler_with_time(app: App, init_scheduler, mocker: MockerFixtu await config.add_subscribe(TargetQQGroup(group_id=123), T_Target("t1"), "target1", "bilibili", [], []) await config.add_subscribe(TargetQQGroup(group_id=123), T_Target("t2"), "target1", "bilibili", [], []) - await config.add_subscribe(TargetQQGroup(group_id=123), T_Target("t2"), "target1", "bilibili-live", [], []) + await config.add_subscribe(TargetQQGroup(group_id=123), T_Target("t2"), "target1", "bilibili-bangumi", [], []) await config.update_time_weight_config( T_Target("t2"), @@ -110,7 +110,7 @@ async def test_scheduler_with_time(app: App, init_scheduler, mocker: MockerFixtu time_config=[TimeWeightConfig(start_time=time(10), end_time=time(11), weight=1000)], ), ) - await config.update_time_weight_config(T_Target("t2"), "bilibili-live", WeightConfig(default=30, time_config=[])) + await config.update_time_weight_config(T_Target("t2"), "bilibili-bangumi", WeightConfig(default=30, time_config=[])) await init_scheduler() @@ -119,12 +119,12 @@ async def test_scheduler_with_time(app: App, init_scheduler, mocker: MockerFixtu static_res = await get_schedule_times(BilibiliSchedConf, 6) assert static_res["bilibili-t1"] == 1 assert static_res["bilibili-t2"] == 2 - assert static_res["bilibili-live-t2"] == 3 + assert static_res["bilibili-bangumi-t2"] == 3 static_res = await get_schedule_times(BilibiliSchedConf, 6) assert static_res["bilibili-t1"] == 1 assert static_res["bilibili-t2"] == 2 - assert static_res["bilibili-live-t2"] == 3 + assert static_res["bilibili-bangumi-t2"] == 3 mocker.patch.object(db_config, "_get_time", return_value=time(10, 30))