mirror of
https://github.com/suyiiyii/nonebot-bison.git
synced 2025-06-02 09:26:12 +08:00
🐛 B站转发动态补充 DeletedItem 类型解析 (#659)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
parent
7d65873d06
commit
74a3a0fab0
@ -131,7 +131,7 @@ class PostAPI(APIBase):
|
|||||||
basic: "PostAPI.Basic"
|
basic: "PostAPI.Basic"
|
||||||
id_str: str
|
id_str: str
|
||||||
modules: "PostAPI.Modules"
|
modules: "PostAPI.Modules"
|
||||||
orig: "PostAPI.Item | None" = None
|
orig: "PostAPI.Item | PostAPI.DeletedItem | None" = None
|
||||||
topic: "PostAPI.Topic | None" = None
|
topic: "PostAPI.Topic | None" = None
|
||||||
type: DynamicType
|
type: DynamicType
|
||||||
|
|
||||||
@ -141,6 +141,14 @@ class PostAPI(APIBase):
|
|||||||
modules: "PostAPI.Modules"
|
modules: "PostAPI.Modules"
|
||||||
type: Literal["DYNAMIC_TYPE_NONE"]
|
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):
|
class Data(Base):
|
||||||
items: "list[PostAPI.Item | PostAPI.DeletedItem] | None" = None
|
items: "list[PostAPI.Item | PostAPI.DeletedItem] | None" = None
|
||||||
|
|
||||||
|
@ -32,6 +32,7 @@ from .models import (
|
|||||||
DynamicType,
|
DynamicType,
|
||||||
ArticleMajor,
|
ArticleMajor,
|
||||||
CoursesMajor,
|
CoursesMajor,
|
||||||
|
DeletedMajor,
|
||||||
UnknownMajor,
|
UnknownMajor,
|
||||||
LiveRecommendMajor,
|
LiveRecommendMajor,
|
||||||
)
|
)
|
||||||
@ -243,6 +244,13 @@ class Bilibili(NewMessage):
|
|||||||
pics=[courses.cover],
|
pics=[courses.cover],
|
||||||
url=URL(courses.jump_url).with_scheme("https").human_repr(),
|
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):
|
case UnknownMajor(type=unknown_type):
|
||||||
raise CategoryNotSupport(unknown_type)
|
raise CategoryNotSupport(unknown_type)
|
||||||
case None: # 没有major的情况
|
case None: # 没有major的情况
|
||||||
@ -259,10 +267,13 @@ class Bilibili(NewMessage):
|
|||||||
parsed_raw_post = self.pre_parse_by_mojar(raw_post)
|
parsed_raw_post = self.pre_parse_by_mojar(raw_post)
|
||||||
parsed_raw_repost = None
|
parsed_raw_repost = None
|
||||||
if self._do_get_category(raw_post.type) == Category(5):
|
if self._do_get_category(raw_post.type) == Category(5):
|
||||||
if raw_post.orig:
|
match raw_post.orig:
|
||||||
parsed_raw_repost = self.pre_parse_by_mojar(raw_post.orig)
|
case PostAPI.Item() as orig:
|
||||||
else:
|
parsed_raw_repost = self.pre_parse_by_mojar(orig)
|
||||||
logger.warning(f"转发动态{raw_post.id_str}没有原动态")
|
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(
|
post = Post(
|
||||||
self,
|
self,
|
||||||
@ -275,8 +286,14 @@ class Bilibili(NewMessage):
|
|||||||
nickname=raw_post.modules.module_author.name,
|
nickname=raw_post.modules.module_author.name,
|
||||||
)
|
)
|
||||||
if parsed_raw_repost:
|
if parsed_raw_repost:
|
||||||
orig = raw_post.orig
|
match raw_post.orig:
|
||||||
assert orig
|
case PostAPI.Item() as orig:
|
||||||
|
orig = orig
|
||||||
|
case PostAPI.DeletedItem() as orig:
|
||||||
|
orig = orig.to_item()
|
||||||
|
case None:
|
||||||
|
raise ValueError("转发动态没有原动态")
|
||||||
|
|
||||||
post.repost = Post(
|
post.repost = Post(
|
||||||
self,
|
self,
|
||||||
content=decode_unicode_escapes(parsed_raw_repost.content),
|
content=decode_unicode_escapes(parsed_raw_repost.content),
|
||||||
|
@ -2,6 +2,7 @@ import reprlib
|
|||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
from collections.abc import Sequence
|
||||||
from dataclasses import fields, dataclass
|
from dataclasses import fields, dataclass
|
||||||
|
|
||||||
from nonebot.log import logger
|
from nonebot.log import logger
|
||||||
@ -30,7 +31,7 @@ class Post(AbstractPost, PlainContentSupport):
|
|||||||
"""文本内容"""
|
"""文本内容"""
|
||||||
title: str | None = None
|
title: str | None = None
|
||||||
"""标题"""
|
"""标题"""
|
||||||
images: list[str | bytes | Path | BytesIO] | None = None
|
images: Sequence[str | bytes | Path | BytesIO] | None = None
|
||||||
"""图片列表"""
|
"""图片列表"""
|
||||||
timestamp: float | None = None
|
timestamp: float | None = None
|
||||||
"""发布/获取时间戳, 秒"""
|
"""发布/获取时间戳, 秒"""
|
||||||
|
204
tests/platforms/static/bilibili-new.json
vendored
204
tests/platforms/static/bilibili-new.json
vendored
@ -4053,6 +4053,210 @@
|
|||||||
},
|
},
|
||||||
"type": "DYNAMIC_TYPE_DRAW",
|
"type": "DYNAMIC_TYPE_DRAW",
|
||||||
"visible": true
|
"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",
|
"offset": "915793667264872453",
|
||||||
|
@ -407,6 +407,18 @@ async def test_dynamic_forward(bilibili: "Bilibili", bing_dy_list: list):
|
|||||||
assert rp.url == "https://t.bilibili.com/915793667264872453"
|
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
|
@pytest.mark.asyncio
|
||||||
@respx.mock
|
@respx.mock
|
||||||
async def test_fetch_new_without_dynamic(bilibili, dummy_user_subinfo, without_dynamic):
|
async def test_fetch_new_without_dynamic(bilibili, dummy_user_subinfo, without_dynamic):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user