From 218dad3b860497d998c9515d1e4c797b0b7837af Mon Sep 17 00:00:00 2001 From: HibiKier <775757368@qq.com> Date: Mon, 6 Sep 2021 21:03:50 +0800 Subject: [PATCH] fix bilibili_sub --- plugins/ai/data_source.py | 6 ++-- plugins/bilibili_sub/__init__.py | 6 ++-- plugins/bilibili_sub/data_source.py | 55 ++++++++++++++++------------- 3 files changed, 36 insertions(+), 31 deletions(-) diff --git a/plugins/ai/data_source.py b/plugins/ai/data_source.py index f4e07af3..3ad6faef 100644 --- a/plugins/ai/data_source.py +++ b/plugins/ai/data_source.py @@ -132,7 +132,7 @@ def hello() -> str: ( "哦豁?!", "你好!Ov<", - f"库库库,呼唤{list(get_bot().config.nickname)[0]}做什么呢", + f"库库库,呼唤{NICKNAME}做什么呢", "我在呢!", "呼呼,叫俺干嘛", ) @@ -170,8 +170,8 @@ async def check_text(text: str, sess: ClientSession) -> str: async with sess.get(check_url, timeout=2, params=params) as response: data = await response.json() if data["code"] == 200: - if data["conclusion_type"] == 2: + if data['data']["conclusion_type"] == 2: return '' except Exception as e: - logger.error(f"检测违规文本错误...e:{e}") + logger.error(f"检测违规文本错误...{type(e)}:{e}") return text diff --git a/plugins/bilibili_sub/__init__.py b/plugins/bilibili_sub/__init__.py index 352a9f0d..7499abaf 100644 --- a/plugins/bilibili_sub/__init__.py +++ b/plugins/bilibili_sub/__init__.py @@ -80,7 +80,6 @@ async def _(bot: Bot, event: MessageEvent, state: T_State): if sub_type in ["season", "动漫", "番剧"]: rst = "*以为您找到以下番剧,请输入Id选择:*\n" state["season_data"] = await get_media_id(id_) - print(state["season_data"]) if len(state["season_data"]) == 0: await add_sub.finish(f"未找到番剧:{msg}") for i, x in enumerate(state["season_data"]): @@ -105,7 +104,6 @@ async def _(bot: Bot, event: MessageEvent, state: T_State): await add_sub.send(await add_season_sub(id_, sub_user)) else: await add_sub.finish("参数错误,第一参数必须为:主播/up/番剧!") - sub_manager.reload_flag = True logger.info( f"(USER {event.user_id}, GROUP " f"{event.group_id if isinstance(event, GroupMessageEvent) else 'private'})" @@ -151,8 +149,8 @@ async def _(bot: Bot, event: MessageEvent, state: T_State): f'\t当前集数:{x.season_current_episode}\n' \ f'------------------\n' live_rst = '当前订阅的直播:\n' + live_rst if live_rst else live_rst - up_rst = '当前订阅的UP:\n' if up_rst else up_rst - season_rst = '当前订阅的番剧:\n' if season_rst else season_rst + up_rst = '当前订阅的UP:\n' + up_rst if up_rst else up_rst + season_rst = '当前订阅的番剧:\n' + season_rst if season_rst else season_rst if not live_rst and not up_rst and not season_rst: live_rst = '您目前没有任何订阅...' await show_sub_info.send(live_rst + up_rst + season_rst) diff --git a/plugins/bilibili_sub/data_source.py b/plugins/bilibili_sub/data_source.py index d4032287..dfe8fefa 100644 --- a/plugins/bilibili_sub/data_source.py +++ b/plugins/bilibili_sub/data_source.py @@ -335,45 +335,52 @@ async def get_user_dynamic( class SubManager: def __init__(self): - self.reload_flag = True self.live_data = [] self.up_data = [] self.season_data = [] - self.sub_list = [] + self.current_index = -1 async def reload_sub_data(self): """ 重载数据 """ - if self.reload_flag or not self.sub_list: + if not self.live_data or not self.up_data or not self.season_data: ( - self.live_data, - self.up_data, - self.season_data, + _live_data, + _up_data, + _season_data, ) = await BilibiliSub.get_all_sub_data() - for x, i, j in zip(self.live_data, self.up_data, self.season_data): - self.sub_list.append(x) - self.sub_list.append(i) - self.sub_list.append(j) - self.reload_flag = False - - def append(self, data: BilibiliSub): - """ - 增加新数据 - :param data: 数据 - """ - self.sub_list.append(data) + if not self.live_data: + self.live_data = _live_data + if not self.up_data: + self.up_data = _up_data + if not self.season_data: + self.season_data = _season_data async def random_sub_data(self) -> Optional[BilibiliSub]: """ 随机获取一条数据 :return: """ - if not self.sub_list: - await self.reload_sub_data() - if self.sub_list: - sub = random.choice(self.sub_list) - self.sub_list.remove(sub) + sub = None + self.current_index += 1 + if self.current_index == 0: + if self.live_data: + sub = random.choice(self.live_data) + self.live_data.remove(sub) + elif self.current_index == 1: + if self.up_data: + sub = random.choice(self.up_data) + self.up_data.remove(sub) + elif self.current_index == 2: + if self.season_data: + sub = random.choice(self.season_data) + self.season_data.remove(sub) + else: + self.current_index = -1 + if sub: return sub - return None + await self.reload_sub_data() + return await self.random_sub_data() +