From 3039ba7bf954e2c136f7fa11b4d1b4e97b51364d Mon Sep 17 00:00:00 2001 From: AzideCupric <57004769+AzideCupric@users.noreply.github.com> Date: Mon, 4 Sep 2023 20:43:51 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=20=E5=A4=84=E7=90=86topic=5Finfo=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E7=BC=BA=E5=A4=B1=E7=9A=84=E9=97=AE=E9=A2=98=20(#354)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * :bug: 处理topic_info字段缺失的问题 * :white_check_mark: 补充测试 * :recycle: dict get 默认为 None Co-authored-by: felinae98 <731499577@qq.com> * :recycle: 原地tp? Co-authored-by: felinae98 <731499577@qq.com> --------- Co-authored-by: felinae98 <731499577@qq.com> --- nonebot_bison/platform/bilibili.py | 6 +++++- tests/platforms/test_bilibili.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/nonebot_bison/platform/bilibili.py b/nonebot_bison/platform/bilibili.py index 75d898e..369cf11 100644 --- a/nonebot_bison/platform/bilibili.py +++ b/nonebot_bison/platform/bilibili.py @@ -123,7 +123,11 @@ class Bilibili(NewMessage): return self._do_get_category(post_type) def get_tags(self, raw_post: RawPost) -> list[Tag]: - return [*(tp["topic_name"] for tp in raw_post["display"]["topic_info"]["topic_details"])] + # FIXME: 更深的原因可能是返回格式的变动,需要进一步确认 + if topic_info := raw_post["display"].get("topic_info"): + return [tp["topic_name"] for tp in topic_info["topic_details"]] + + return [] def _text_process(self, dynamic: str, desc: str, title: str) -> str: similarity = 1.0 if len(dynamic) == 0 or len(desc) == 0 else text_similarity(dynamic, desc) diff --git a/tests/platforms/test_bilibili.py b/tests/platforms/test_bilibili.py index 0b10cc6..1e5f418 100644 --- a/tests/platforms/test_bilibili.py +++ b/tests/platforms/test_bilibili.py @@ -40,6 +40,36 @@ def without_dynamic(app: App): } +@pytest.mark.asyncio +async def test_get_tag_without_topic_info(bilibili, bing_dy_list): + simple_raw_post = { + "display": { + "topic_info": { + "topic_details": [ + { + "topic_name": "可露希尔的秘密档案", + }, + { + "topic_name": "罗德岛相簿", + }, + ], + }, + }, + } + + simple_raw_post_without_topic_info = { + "display": { + "damedane": "dameyo", + }, + } + + res1 = bilibili.get_tags(simple_raw_post) + assert res1 == ["可露希尔的秘密档案", "罗德岛相簿"] + + res2 = bilibili.get_tags(simple_raw_post_without_topic_info) + assert res2 == [] + + @pytest.mark.asyncio async def test_video_forward(bilibili, bing_dy_list): post = await bilibili.parse(bing_dy_list[1])