mirror of
https://github.com/suyiiyii/nonebot-bison.git
synced 2025-06-04 02:26:11 +08:00
add forward for bilibili
This commit is contained in:
parent
471ecc5e4f
commit
f135bbb5f6
@ -14,6 +14,7 @@ class Bilibili(NewMessage, TargetMixin):
|
||||
2: "专栏文章",
|
||||
3: "视频",
|
||||
4: "纯文字",
|
||||
5: "转发"
|
||||
# 5: "短视频"
|
||||
}
|
||||
platform_name = 'bilibili'
|
||||
@ -48,8 +49,7 @@ class Bilibili(NewMessage, TargetMixin):
|
||||
def get_date(self, post: RawPost) -> int:
|
||||
return post['desc']['timestamp']
|
||||
|
||||
def get_category(self, post: RawPost) -> Category:
|
||||
post_type = post['desc']['type']
|
||||
def _do_get_category(self, post_type: int) -> Category:
|
||||
if post_type == 2:
|
||||
return Category(1)
|
||||
elif post_type == 64:
|
||||
@ -60,35 +60,65 @@ class Bilibili(NewMessage, TargetMixin):
|
||||
return Category(4)
|
||||
elif post_type == 1:
|
||||
# 转发
|
||||
raise CategoryNotSupport()
|
||||
return Category(5)
|
||||
raise CategoryNotSupport()
|
||||
|
||||
def get_category(self, post: RawPost) -> Category:
|
||||
post_type = post['desc']['type']
|
||||
return self._do_get_category(post_type)
|
||||
|
||||
def get_tags(self, raw_post: RawPost) -> list[Tag]:
|
||||
return [*map(lambda tp: tp['topic_name'], raw_post['display']['topic_info']['topic_details'])]
|
||||
|
||||
def _get_info(self, post_type: Category, card) -> tuple[str, list]:
|
||||
if post_type == 1:
|
||||
# 一般动态
|
||||
text = card['item']['description']
|
||||
pic = [img['img_src'] for img in card['item']['pictures']]
|
||||
elif post_type == 2:
|
||||
# 专栏文章
|
||||
text = '{} {}'.format(card['title'], card['summary'])
|
||||
pic = card['image_urls']
|
||||
elif post_type == 3:
|
||||
# 视频
|
||||
text = card['dynamic']
|
||||
pic = [card['pic']]
|
||||
elif post_type == 4:
|
||||
# 纯文字
|
||||
text = card['item']['content']
|
||||
pic = []
|
||||
else:
|
||||
raise CategoryNotSupport()
|
||||
return text, pic
|
||||
|
||||
async def parse(self, raw_post: RawPost) -> Post:
|
||||
card_content = json.loads(raw_post['card'])
|
||||
post_type = self.get_category(raw_post)
|
||||
target_name = raw_post['desc']['user_profile']['info']['uname']
|
||||
if post_type == 1:
|
||||
# 一般动态
|
||||
text = card_content['item']['description']
|
||||
url = 'https://t.bilibili.com/{}'.format(raw_post['desc']['dynamic_id'])
|
||||
pic = [img['img_src'] for img in card_content['item']['pictures']]
|
||||
elif post_type == 2:
|
||||
# 专栏文章
|
||||
text = '{} {}'.format(card_content['title'], card_content['summary'])
|
||||
url = 'https://www.bilibili.com/read/cv{}'.format(raw_post['desc']['rid'])
|
||||
pic = card_content['image_urls']
|
||||
elif post_type == 3:
|
||||
# 视频
|
||||
text = card_content['dynamic']
|
||||
url = 'https://www.bilibili.com/video/{}'.format(raw_post['desc']['bvid'])
|
||||
pic = [card_content['pic']]
|
||||
elif post_type == 4:
|
||||
# 纯文字
|
||||
if post_type >= 1 and post_type < 5:
|
||||
url = ''
|
||||
if post_type == 1:
|
||||
# 一般动态
|
||||
url = 'https://t.bilibili.com/{}'.format(raw_post['desc']['dynamic_id_str'])
|
||||
elif post_type == 2:
|
||||
# 专栏文章
|
||||
url = 'https://www.bilibili.com/read/cv{}'.format(raw_post['desc']['rid'])
|
||||
elif post_type == 3:
|
||||
# 视频
|
||||
url = 'https://www.bilibili.com/video/{}'.format(raw_post['desc']['bvid'])
|
||||
elif post_type == 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:
|
||||
# 转发
|
||||
url = 'https://t.bilibili.com/{}'.format(raw_post['desc']['dynamic_id_str'])
|
||||
text = card_content['item']['content']
|
||||
url = 'https://t.bilibili.com/{}'.format(raw_post['desc']['dynamic_id'])
|
||||
orig_type = card_content['item']['orig_type']
|
||||
orig = json.loads(card_content['origin'])
|
||||
orig_text, _ = self._get_info(self._do_get_category(orig_type), orig)
|
||||
text += '\n--------------\n'
|
||||
text += orig_text
|
||||
pic = []
|
||||
else:
|
||||
raise CategoryNotSupport(post_type)
|
||||
|
1
tests/platforms/bilibili_bing_list.json
Normal file
1
tests/platforms/bilibili_bing_list.json
Normal file
File diff suppressed because one or more lines are too long
37
tests/platforms/test_bilibili.py
Normal file
37
tests/platforms/test_bilibili.py
Normal file
@ -0,0 +1,37 @@
|
||||
import pytest
|
||||
import typing
|
||||
from httpx import Response
|
||||
|
||||
if typing.TYPE_CHECKING:
|
||||
import sys
|
||||
sys.path.append('./src/plugins')
|
||||
import nonebot_hk_reporter
|
||||
|
||||
from .utils import get_json
|
||||
|
||||
@pytest.fixture(scope='module')
|
||||
def bing_dy_list():
|
||||
return get_json('bilibili_bing_list.json')['data']['cards']
|
||||
|
||||
@pytest.fixture
|
||||
def bilibili(plugin_module: 'nonebot_hk_reporter'):
|
||||
return plugin_module.platform.platform_manager['bilibili']
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_video_forward(bilibili, bing_dy_list):
|
||||
post = await bilibili.parse(bing_dy_list[1])
|
||||
assert(post.text == '答案揭晓:宿舍!来看看投票结果\nhttps://t.bilibili.com/568093580488553786\n--------------\n#可露希尔的秘密档案# \n11:来宿舍休息一下吧 \n档案来源:lambda:\\罗德岛内务\\秘密档案 \n发布时间:9/12 1:00 P.M. \n档案类型:可见 \n档案描述:今天请了病假在宿舍休息。很舒适。 \n提供者:赫默')
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_article_forward(bilibili, bing_dy_list):
|
||||
post = await bilibili.parse(bing_dy_list[4])
|
||||
assert(post.text == '#明日方舟##饼学大厦#\n9.11专栏更新完毕,这还塌了实属没跟新运营对上\n后边除了周日发饼和PV没提及的中文语音,稳了\n别忘了来参加#可露希尔的秘密档案#的主题投票\nhttps://t.bilibili.com/568093580488553786?tab=2' +
|
||||
'\n--------------\n' +
|
||||
'【明日方舟】饼学大厦#12~14(风暴瞭望&玛莉娅·临光&红松林&感谢庆典)9.11更新 更新记录09.11更新:覆盖09.10更新;以及排期更新,猜测周一周五开活动09.10更新:以周五开活动为底,PV/公告调整位置,整体结构更新09.08更新:饼学大厦#12更新,新增一件六星商店服饰(周日发饼)09.06更新:饼学大厦整栋整栋翻新,改为9.16开主线(四日无饼!)09.05凌晨更新:10.13后的排期(两日无饼,鹰角背刺,心狠手辣)前言感谢楪筱祈ぺ的动态-哔哩哔哩 (bilibili.com) 对饼学的贡献!后续排期:9.17【风暴瞭望】、10.01【玛莉娅·临光】复刻、10.1')
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_dynamic_forward(bilibili, bing_dy_list):
|
||||
post = await bilibili.parse(bing_dy_list[5])
|
||||
assert(post.text == '饼组主线饼学预测——9.11版\n①今日结果\n9.11 殿堂上的游禽-星极(x,新运营实锤了)\n②后续预测\n9.12 #罗德岛相簿#+#可露希尔的秘密档案#11话\n9.13 六星先锋(执旗手)干员-琴柳\n9.14 宣传策略-空弦+家具\n9.15 轮换池(+中文语音前瞻)\n9.16 停机\n9.17 #罗德岛闲逛部#+新六星EP+EP09·风暴瞭望开启\n9.19 #罗德岛相簿#' +
|
||||
'\n--------------\n' +
|
||||
'#明日方舟#\n【新增服饰】\n//殿堂上的游禽 - 星极\n塞壬唱片偶像企划《闪耀阶梯》特供服饰/殿堂上的游禽。星极自费参加了这项企划,尝试着用大众能接受的方式演绎天空之上的故事。\n\n_____________\n谦逊留给观众,骄傲发自歌喉,此夜,唯我璀璨。 ')
|
Loading…
x
Reference in New Issue
Block a user