🐛 B站转发动态补充 DeletedItem 类型解析 (#659)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Azide 2024-12-04 20:09:01 +08:00 committed by GitHub
parent 630845fe0d
commit 22bf05949d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 250 additions and 8 deletions

View File

@ -131,7 +131,7 @@ class PostAPI(APIBase):
basic: "PostAPI.Basic"
id_str: str
modules: "PostAPI.Modules"
orig: "PostAPI.Item | None" = None
orig: "PostAPI.Item | PostAPI.DeletedItem | None" = None
topic: "PostAPI.Topic | None" = None
type: DynamicType
@ -141,6 +141,14 @@ class PostAPI(APIBase):
modules: "PostAPI.Modules"
type: Literal["DYNAMIC_TYPE_NONE"]
def to_item(self) -> "PostAPI.Item":
return PostAPI.Item(
basic=self.basic,
id_str="",
modules=self.modules,
type=self.type,
)
class Data(Base):
items: "list[PostAPI.Item | PostAPI.DeletedItem] | None" = None

View File

@ -32,6 +32,7 @@ from .models import (
DynamicType,
ArticleMajor,
CoursesMajor,
DeletedMajor,
UnknownMajor,
LiveRecommendMajor,
)
@ -243,6 +244,13 @@ class Bilibili(NewMessage):
pics=[courses.cover],
url=URL(courses.jump_url).with_scheme("https").human_repr(),
)
case DeletedMajor(none=none):
return _ParsedMojarPost(
title="",
content=none.tips,
pics=[],
url=None,
)
case UnknownMajor(type=unknown_type):
raise CategoryNotSupport(unknown_type)
case None: # 没有major的情况
@ -259,10 +267,13 @@ class Bilibili(NewMessage):
parsed_raw_post = self.pre_parse_by_mojar(raw_post)
parsed_raw_repost = None
if self._do_get_category(raw_post.type) == Category(5):
if raw_post.orig:
parsed_raw_repost = self.pre_parse_by_mojar(raw_post.orig)
else:
logger.warning(f"转发动态{raw_post.id_str}没有原动态")
match raw_post.orig:
case PostAPI.Item() as orig:
parsed_raw_repost = self.pre_parse_by_mojar(orig)
case PostAPI.DeletedItem() as orig:
parsed_raw_repost = self.pre_parse_by_mojar(orig.to_item())
case None:
logger.warning(f"转发动态{raw_post.id_str}没有原动态")
post = Post(
self,
@ -275,8 +286,14 @@ class Bilibili(NewMessage):
nickname=raw_post.modules.module_author.name,
)
if parsed_raw_repost:
orig = raw_post.orig
assert orig
match raw_post.orig:
case PostAPI.Item() as orig:
orig = orig
case PostAPI.DeletedItem() as orig:
orig = orig.to_item()
case None:
raise ValueError("转发动态没有原动态")
post.repost = Post(
self,
content=decode_unicode_escapes(parsed_raw_repost.content),

View File

@ -2,6 +2,7 @@ import reprlib
from io import BytesIO
from pathlib import Path
from typing import TYPE_CHECKING
from collections.abc import Sequence
from dataclasses import fields, dataclass
from nonebot.log import logger
@ -30,7 +31,7 @@ class Post(AbstractPost, PlainContentSupport):
"""文本内容"""
title: str | None = None
"""标题"""
images: list[str | bytes | Path | BytesIO] | None = None
images: Sequence[str | bytes | Path | BytesIO] | None = None
"""图片列表"""
timestamp: float | None = None
"""发布/获取时间戳, 秒"""

View File

@ -4053,6 +4053,210 @@
},
"type": "DYNAMIC_TYPE_DRAW",
"visible": true
},
{
"basic": {
"comment_id_str": "965806534205374473",
"comment_type": 17,
"like_icon": {
"action_url": "https://i0.hdslb.com/bfs/garb/item/8860c7c01179f9984f88fb61bc55cab9dc1d28cb.bin",
"end_url": "",
"id": 33772,
"start_url": ""
},
"rid_str": "965806534205374473"
},
"id_str": "965806534205374473",
"modules": {
"module_author": {
"avatar": {
"container_size": {
"height": 1.35,
"width": 1.35
},
"fallback_layers": {
"is_critical_group": true,
"layers": [
{
"general_spec": {
"pos_spec": {
"axis_x": 0.675,
"axis_y": 0.675,
"coordinate_pos": 2
},
"render_spec": {
"opacity": 1
},
"size_spec": {
"height": 1,
"width": 1
}
},
"layer_config": {
"is_critical": true,
"tags": {
"AVATAR_LAYER": {},
"GENERAL_CFG": {
"config_type": 1,
"general_config": {
"web_css_style": {
"borderRadius": "50%"
}
}
}
}
},
"resource": {
"res_image": {
"image_src": {
"placeholder": 6,
"remote": {
"bfs_style": "widget-layer-avatar",
"url": "https://i0.hdslb.com/bfs/face/5d255a47deae6c5214f93cdbbf3b01f23cac4a5e.jpg"
},
"src_type": 1
}
},
"res_type": 3
},
"visible": true
}
]
},
"mid": "6050499"
},
"face": "https://i0.hdslb.com/bfs/face/5d255a47deae6c5214f93cdbbf3b01f23cac4a5e.jpg",
"face_nft": false,
"following": null,
"jump_url": "//space.bilibili.com/6050499/dynamic",
"label": "",
"mid": 6050499,
"name": "血毒嘿咻",
"official_verify": {
"desc": "",
"type": -1
},
"pendant": {
"expire": 0,
"image": "",
"image_enhance": "",
"image_enhance_frame": "",
"n_pid": 0,
"name": "",
"pid": 0
},
"pub_action": "",
"pub_location_text": "",
"pub_time": "08月15日",
"pub_ts": 1723707757,
"type": "AUTHOR_TYPE_NORMAL",
"vip": {
"avatar_subscript": 0,
"avatar_subscript_url": "",
"due_date": 1648224000000,
"label": {
"bg_color": "",
"bg_style": 0,
"border_color": "",
"img_label_uri_hans": "",
"img_label_uri_hans_static": "https://i0.hdslb.com/bfs/vip/d7b702ef65a976b20ed854cbd04cb9e27341bb79.png",
"img_label_uri_hant": "",
"img_label_uri_hant_static": "https://i0.hdslb.com/bfs/activity-plat/static/20220614/e369244d0b14644f5e1a06431e22a4d5/KJunwh19T5.png",
"label_theme": "",
"path": "",
"text": "",
"text_color": "",
"use_img_label": true
},
"nickname_color": "",
"status": 0,
"theme_type": 0,
"type": 1
}
},
"module_dynamic": {
"additional": null,
"desc": {
"rich_text_nodes": [
{
"orig_text": "转发动态",
"text": "转发动态",
"type": "RICH_TEXT_NODE_TYPE_TEXT"
}
],
"text": "转发动态"
},
"major": null,
"topic": null
},
"module_more": {
"three_point_items": [
{
"label": "举报",
"type": "THREE_POINT_REPORT"
}
]
},
"module_stat": {
"comment": {
"count": 0,
"forbidden": false
},
"forward": {
"count": 0,
"forbidden": false
},
"like": {
"count": 0,
"forbidden": false,
"status": false
}
}
},
"orig": {
"basic": {
"comment_id_str": "",
"comment_type": 0,
"like_icon": {
"action_url": "",
"end_url": "",
"id": 0,
"start_url": ""
},
"rid_str": ""
},
"id_str": null,
"modules": {
"module_author": {
"face": "",
"face_nft": false,
"following": false,
"jump_url": "",
"label": "",
"mid": 0,
"name": "",
"pub_action": "",
"pub_time": "",
"pub_ts": 0,
"type": "AUTHOR_TYPE_NORMAL"
},
"module_dynamic": {
"additional": null,
"desc": null,
"major": {
"none": {
"tips": "源动态已被作者删除"
},
"type": "MAJOR_TYPE_NONE"
},
"topic": null
}
},
"type": "DYNAMIC_TYPE_NONE",
"visible": true
},
"type": "DYNAMIC_TYPE_FORWARD",
"visible": true
}
],
"offset": "915793667264872453",

View File

@ -407,6 +407,18 @@ async def test_dynamic_forward(bilibili: "Bilibili", bing_dy_list: list):
assert rp.url == "https://t.bilibili.com/915793667264872453"
async def test_dynamic_forword_deleted(bilibili: "Bilibili", bing_dy_list: list):
from nonebot_bison.post import Post
post: Post = await bilibili.parse(bing_dy_list[12])
assert post.content == "转发动态"
assert post.url == "https://t.bilibili.com/965806534205374473"
assert (repost := post.repost)
assert repost.url is None
assert not repost.title
assert repost.content == "源动态已被作者删除"
@pytest.mark.asyncio
@respx.mock
async def test_fetch_new_without_dynamic(bilibili, dummy_user_subinfo, without_dynamic):