add forward for bilibili

This commit is contained in:
felinae98
2021-09-12 19:03:35 +08:00
parent 471ecc5e4f
commit f135bbb5f6
3 changed files with 89 additions and 21 deletions
@@ -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)