Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Post 添加 category 字段使消息首部可以显示分类信息 #471

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions nonebot_bison/platform/bilibili.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,35 +165,43 @@ def _get_info(self, post_type: Category, card) -> tuple[str, list]:

async def parse(self, raw_post: RawPost) -> Post:
card_content = json.loads(raw_post["card"])
post_type = self.get_category(raw_post)
post_category = self.get_category(raw_post)
orig_category = Category(0)
target_name = raw_post["desc"]["user_profile"]["info"]["uname"]
if post_type >= 1 and post_type < 5:
if post_category >= 1 and post_category < 5:
AzideCupric marked this conversation as resolved.
Show resolved Hide resolved
url = ""
if post_type == 1:
if post_category == 1:
# 一般动态
url = "https://t.bilibili.com/{}".format(raw_post["desc"]["dynamic_id_str"])
elif post_type == 2:
elif post_category == 2:
# 专栏文章
url = "https://www.bilibili.com/read/cv{}".format(raw_post["desc"]["rid"])
elif post_type == 3:
elif post_category == 3:
# 视频
url = "https://www.bilibili.com/video/{}".format(raw_post["desc"]["bvid"])
elif post_type == 4:
elif post_category == 4:
# 纯文字
url = "https://t.bilibili.com/{}".format(raw_post["desc"]["dynamic_id_str"])
text, pic = self._get_info(post_type, card_content)
elif post_type == 5:
text, pic = self._get_info(post_category, card_content)
elif post_category == 5:
# 转发
url = "https://t.bilibili.com/{}".format(raw_post["desc"]["dynamic_id_str"])
text = card_content["item"]["content"]
orig_type = card_content["item"]["orig_type"]
orig_category = self._do_get_category(card_content["item"]["orig_type"])
orig = json.loads(card_content["origin"])
orig_text, pic = self._get_info(self._do_get_category(orig_type), orig)
orig_text, pic = self._get_info(orig_category, orig)
text += "\n--------------\n"
text += orig_text
else:
raise CategoryNotSupport(post_type)
return Post("bilibili", text=text, url=url, pics=pic, target_name=target_name)
raise CategoryNotSupport(post_category)
return Post(
"bilibili",
text=text,
url=url,
pics=pic,
target_name=target_name,
category=self.categories[post_category] if not orig_category else f"转发自{self.categories[orig_category]}",
)


class Bilibililive(StatusChange):
Expand Down Expand Up @@ -331,14 +339,14 @@ def get_category(self, status: Info) -> Category:
async def parse(self, raw_post: Info) -> Post:
url = f"https://live.bilibili.com/{raw_post.room_id}"
pic = [raw_post.cover] if raw_post.category == Category(1) else [raw_post.keyframe]
title = f"[{self.categories[raw_post.category].rstrip('提醒')}] {raw_post.title}"
target_name = f"{raw_post.uname} {raw_post.area_name}"
return Post(
self.name,
text=title,
text=raw_post.title,
url=url,
pics=list(pic),
target_name=target_name,
category=self.categories[self.get_category(raw_post)].rstrip("提醒"),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个 rstrip 是 why

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

沿袭历史

async def parse(self, raw_post: Info) -> Post:
url = f"https://live.bilibili.com/{raw_post.room_id}"
pic = [raw_post.cover] if raw_post.category == Category(1) else [raw_post.keyframe]
title = f"[{self.categories[raw_post.category].rstrip('提醒')}] {raw_post.title}"
target_name = f"{raw_post.uname} {raw_post.area_name}"

compress=True,
)

Expand Down
1 change: 1 addition & 0 deletions nonebot_bison/platform/mcbbsnews.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ async def parse(self, post: RawPost) -> Post:
url=post_url,
pics=list(pics),
target_name=post["category"],
category=self.categories[self.get_category(post)],
)

async def _news_render(self, url: str, selector: str) -> list[bytes]:
Expand Down
1 change: 1 addition & 0 deletions nonebot_bison/platform/weibo.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,5 @@ async def parse(self, raw_post: RawPost) -> Post:
url=detail_url,
pics=pics,
target_name=info["user"]["screen_name"],
category=self.categories[self.get_category(raw_post)],
)
6 changes: 4 additions & 2 deletions nonebot_bison/post/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class _Post(BasePost):
url: str | None = None
target_name: str | None = None
pics: list[str | bytes] = field(default_factory=list)
category: str | None = None
"""展现在消息开头的分类信息,如“视频”、“转发”等。"""

_message: list[MessageSegmentFactory] | None = None
_pic_message: list[MessageSegmentFactory] | None = None
Expand Down Expand Up @@ -101,7 +103,7 @@ async def generate_text_messages(self) -> list[MessageSegmentFactory]:
if self._message is None:
await self._pic_merge()
msg_segments: list[MessageSegmentFactory] = []
text = ""
text = f"[{self.category}] " if self.category else ""
if self.text:
text += "{}".format(self.text if len(self.text) < 500 else self.text[:500] + "...")
if text:
Expand Down Expand Up @@ -142,7 +144,7 @@ def __str__(self):
self.target_name,
self.text if len(self.text) < 500 else self.text[:500] + "...",
self.url,
", ".join("b64img" if isinstance(x, bytes) or x.startswith("base64") else x for x in self.pics),
", ".join("b64img" if not isinstance(x, str) or x.startswith("base64") else x for x in self.pics),
)


Expand Down
4 changes: 2 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions tests/platforms/test_bilibili.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ async def test_video_forward(bilibili, bing_dy_list):
" \n档案类型:可见 \n档案描述:今天请了病假在宿舍休息。很舒适。"
" \n提供者:赫默\n=================\n《可露希尔的秘密档案》11话:来宿舍休息一下吧"
)
assert post.category == "转发自视频"


@pytest.mark.asyncio
Expand Down Expand Up @@ -115,6 +116,7 @@ async def test_article_forward(bilibili, bing_dy_list):
"心狠手辣)前言感谢楪筱祈ぺ的动态-哔哩哔哩 (bilibili.com) 对饼学的贡献!后续排期:9.17【风暴瞭望】、"
"10.01【玛莉娅·临光】复刻、10.1"
)
assert post.category == "转发自专栏文章"


@pytest.mark.asyncio
Expand All @@ -131,6 +133,7 @@ async def test_dynamic_forward(bilibili, bing_dy_list):
"星极自费参加了这项企划,尝试着用大众能接受的方式演绎天空之上的故事。\n\n_____________\n谦逊留给观众,"
"骄傲发自歌喉,此夜,唯我璀璨。 "
)
assert post.category == "转发自一般动态"


@pytest.mark.asyncio
Expand Down Expand Up @@ -178,6 +181,7 @@ async def test_fetch_new(bilibili, dummy_user_subinfo):
" 01:高脚羽兽烤串与罗德岛的领袖\r\n\r\n哔哩哔哩漫画阅读:https://manga.bilibili.com/detail/mc31998?from=manga_search\r\n\r\n关注并转发本动态,"
"我们将会在5月27日抽取10位博士赠送【兔兔奇境】周边礼盒一份。 互动抽奖"
)
assert post.category == "一般动态"


async def test_parse_target(bilibili: "Bilibili"):
Expand Down
21 changes: 14 additions & 7 deletions tests/platforms/test_bilibili_live.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ async def test_fetch_first_live(bili_live, dummy_only_open_user_subinfo):
assert len(res2) == 1
post = res2[0][1][0]
assert post.target_type == "Bilibili直播"
assert post.text == "[开播] 【Zc】从0挑战到15肉鸽!目前10难度"
assert post.text == "【Zc】从0挑战到15肉鸽!目前10难度"
assert post.url == "https://live.bilibili.com/3044248"
assert post.target_name == "魔法Zc目录 其他单机"
assert post.pics == ["https://i0.hdslb.com/bfs/live/new_room_cover/fd357f0f3cbbb48e9acfbcda616b946c2454c56c.jpg"]
assert post.compress is True
assert post.category == "开播"


@pytest.mark.asyncio
Expand All @@ -101,11 +102,12 @@ async def test_fetch_bililive_only_live_open(bili_live, dummy_only_open_user_sub
res2 = await bili_live.batch_fetch_new_post([(SubUnit(target, [dummy_only_open_user_subinfo]))])
post = res2[0][1][0]
assert post.target_type == "Bilibili直播"
assert post.text == "[开播] 【Zc】从0挑战到15肉鸽!目前10难度"
assert post.text == "【Zc】从0挑战到15肉鸽!目前10难度"
assert post.url == "https://live.bilibili.com/3044248"
assert post.target_name == "魔法Zc目录 其他单机"
assert post.pics == ["https://i0.hdslb.com/bfs/live/new_room_cover/fd357f0f3cbbb48e9acfbcda616b946c2454c56c.jpg"]
assert post.compress is True
assert post.category == "开播"
# 标题变更
mock_bili_live_status["data"][target]["title"] = "【Zc】从0挑战到15肉鸽!目前11难度"
bili_live_router.mock(return_value=Response(200, json=mock_bili_live_status))
Expand Down Expand Up @@ -165,11 +167,12 @@ async def test_fetch_bililive_only_title_change(bili_live, dummy_only_title_user
res3 = await bili_live.batch_fetch_new_post([(SubUnit(target, [dummy_only_title_user_subinfo]))])
post = res3[0][1][0]
assert post.target_type == "Bilibili直播"
assert post.text == "[标题更新] 【Zc】从0挑战到15肉鸽!目前12难度"
assert post.text == "【Zc】从0挑战到15肉鸽!目前12难度"
assert post.url == "https://live.bilibili.com/3044248"
assert post.target_name == "魔法Zc目录 其他单机"
assert post.pics == ["https://i0.hdslb.com/bfs/live-key-frame/keyframe10170435000003044248mwowx0.jpg"]
assert post.compress is True
assert post.category == "标题更新"
# 直播状态更新-下播
mock_bili_live_status["data"][target]["live_status"] = 0
bili_live_router.mock(return_value=Response(200, json=mock_bili_live_status))
Expand Down Expand Up @@ -230,11 +233,12 @@ async def test_fetch_bililive_only_close(bili_live, dummy_only_close_user_subinf
assert bili_live_router.call_count == 5
post = res4[0][1][0]
assert post.target_type == "Bilibili直播"
assert post.text == "[下播] 【Zc】从0挑战到15肉鸽!目前12难度"
assert post.text == "【Zc】从0挑战到15肉鸽!目前12难度"
assert post.url == "https://live.bilibili.com/3044248"
assert post.target_name == "魔法Zc目录 其他单机"
assert post.pics == ["https://i0.hdslb.com/bfs/live-key-frame/keyframe10170435000003044248mwowx0.jpg"]
assert post.compress is True
assert post.category == "下播"


@pytest.fixture()
Expand Down Expand Up @@ -276,30 +280,33 @@ async def test_fetch_bililive_combo(bili_live, dummy_bililive_user_subinfo):
res2 = await bili_live.batch_fetch_new_post([(SubUnit(target, [dummy_bililive_user_subinfo]))])
post2 = res2[0][1][0]
assert post2.target_type == "Bilibili直播"
assert post2.text == "[开播] 【Zc】从0挑战到15肉鸽!目前11难度"
assert post2.text == "【Zc】从0挑战到15肉鸽!目前11难度"
assert post2.url == "https://live.bilibili.com/3044248"
assert post2.target_name == "魔法Zc目录 其他单机"
assert post2.pics == ["https://i0.hdslb.com/bfs/live/new_room_cover/fd357f0f3cbbb48e9acfbcda616b946c2454c56c.jpg"]
assert post2.compress is True
assert post2.category == "开播"
# 标题变更
mock_bili_live_status["data"][target]["title"] = "【Zc】从0挑战到15肉鸽!目前12难度"
bili_live_router.mock(return_value=Response(200, json=mock_bili_live_status))
res3 = await bili_live.batch_fetch_new_post([(SubUnit(target, [dummy_bililive_user_subinfo]))])
post3 = res3[0][1][0]
assert post3.target_type == "Bilibili直播"
assert post3.text == "[标题更新] 【Zc】从0挑战到15肉鸽!目前12难度"
assert post3.text == "【Zc】从0挑战到15肉鸽!目前12难度"
assert post3.url == "https://live.bilibili.com/3044248"
assert post3.target_name == "魔法Zc目录 其他单机"
assert post3.pics == ["https://i0.hdslb.com/bfs/live-key-frame/keyframe10170435000003044248mwowx0.jpg"]
assert post3.compress is True
assert post3.category == "标题更新"
# 直播状态更新-下播
mock_bili_live_status["data"][target]["live_status"] = 0
bili_live_router.mock(return_value=Response(200, json=mock_bili_live_status))
res4 = await bili_live.batch_fetch_new_post([(SubUnit(target, [dummy_bililive_user_subinfo]))])
post4 = res4[0][1][0]
assert post4.target_type == "Bilibili直播"
assert post4.text == "[下播] 【Zc】从0挑战到15肉鸽!目前12难度"
assert post4.text == "【Zc】从0挑战到15肉鸽!目前12难度"
assert post4.url == "https://live.bilibili.com/3044248"
assert post4.target_name == "魔法Zc目录 其他单机"
assert post4.pics == ["https://i0.hdslb.com/bfs/live-key-frame/keyframe10170435000003044248mwowx0.jpg"]
assert post4.compress is True
assert post4.category == "下播"
1 change: 1 addition & 0 deletions tests/platforms/test_mcbbsnews.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ async def test_fetch_new(mcbbsnews, dummy_user_subinfo, raw_post_list):
assert post.url == "https://www.mcbbs.net/{}".format(raw_post["url"])
assert post.target_name == raw_post["category"]
assert len(post.pics) == 1
assert post.category == "Java版资讯"


@pytest.mark.asyncio
Expand Down
2 changes: 1 addition & 1 deletion tests/platforms/test_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ class BatchStatusChange(StatusChange):
enable_tag = False
schedule_type = "interval"
schedule_kw = {"seconds": 10}
has_target = False
has_target = True
categories = {
Category(1): "转发",
Category(2): "视频",
Expand Down
1 change: 1 addition & 0 deletions tests/platforms/test_weibo.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ async def test_fetch_new(weibo, dummy_user_subinfo):
assert post.url == "https://weibo.com/6279793937/KkBtUx2dv"
assert post.target_name == "明日方舟Arknights"
assert len(post.pics) == 1
assert post.category == "图文"


@pytest.mark.asyncio
Expand Down
20 changes: 20 additions & 0 deletions tests/post/test_generate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from nonebug import App


async def test_gen_text(app: App):
from nonebot_bison.post import Post

p = Post(
target_type="bili",
text="111",
)
p_text = await p.generate_text_messages()
assert p_text[0].data["text"] == "111\n来源: bili"

p_c = Post(
target_type="bili",
text="222",
category="动态",
)
p_c_text = await p_c.generate_text_messages()
assert p_c_text[0].data["text"] == "[动态] 222\n来源: bili"
Loading