独立 bilibili-live 的 SchedConf (#473)

*  独立 bilibili-live 的 SchedConf

*  修正测试

*  bilibili和bilibili-live共用http_client

* 🐛 不在__init__中修改_client_refresh_time

*  将刷新浏览器变更为类方法

* ♻️ seperate BilibiliClient

Co-authored-by: felinea98 <me@felinae98.cn>

---------

Co-authored-by: felinea98 <me@felinae98.cn>
This commit is contained in:
Azide 2024-03-19 10:27:37 +08:00 committed by GitHub
parent b1d2acbe79
commit c96dd85b22
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 72 additions and 29 deletions

View File

@ -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

View File

@ -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):

View File

@ -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))