Merge branch 'main' into arknights

This commit is contained in:
felinae98 2021-02-18 01:06:06 +08:00
commit 8b768f7022
No known key found for this signature in database
GPG Key ID: 00C8B010587FF610
5 changed files with 41 additions and 19 deletions

View File

@ -1,6 +1,6 @@
[tool.poetry] [tool.poetry]
name = "nonebot-hk-reporter" name = "nonebot-hk-reporter"
version = "0.2.1" version = "0.2.2"
description = "Subscribe message from social medias" description = "Subscribe message from social medias"
authors = ["felinae98 <felinae225@qq.com>"] authors = ["felinae98 <felinae225@qq.com>"]
license = "MIT" license = "MIT"

View File

@ -66,6 +66,7 @@ class Bilibili(Platform):
async def parse(self, raw_post: RawPost) -> Post: async def parse(self, raw_post: RawPost) -> Post:
card_content = json.loads(raw_post['card']) card_content = json.loads(raw_post['card'])
post_type = self.get_category(raw_post) post_type = self.get_category(raw_post)
target_name = raw_post['desc']['user_profile']['info']['uname']
if post_type == 1: if post_type == 1:
# 一般动态 # 一般动态
text = card_content['item']['description'] text = card_content['item']['description']
@ -88,5 +89,5 @@ class Bilibili(Platform):
pic = [] pic = []
else: else:
raise CategoryNotSupport(post_type) raise CategoryNotSupport(post_type)
return Post('bilibili', text, url, pic) return Post('bilibili', text=text, url=url, pics=pic, target_name=target_name)

View File

@ -38,4 +38,4 @@ class Rss(Platform):
soup = bs(raw_post.description, 'html.parser') soup = bs(raw_post.description, 'html.parser')
text = soup.text text = soup.text
pics = list(map(lambda x: x.attrs['src'], soup('img'))) pics = list(map(lambda x: x.attrs['src'], soup('img')))
return Post('rss', text, raw_post.link, pics) return Post('rss', text=text, url=raw_post.link, pics=pics)

View File

@ -1,6 +1,7 @@
from collections import defaultdict from collections import defaultdict
from datetime import datetime from datetime import datetime
import json import json
import re
import time import time
from typing import Any, Optional from typing import Any, Optional
@ -64,11 +65,20 @@ class Weibo(Platform):
return Category(2) return Category(2)
else: else:
return Category(3) return Category(3)
def _get_text(self, raw_text: str) -> str:
text = raw_text.replace('<br />', '\n')
return bs(text).text
async def parse(self, raw_post: RawPost) -> Post: async def parse(self, raw_post: RawPost) -> Post:
info = raw_post['mblog'] info = raw_post['mblog']
parsed_text = bs(info['text'], 'html.parser').text if info['isLongText'] or info['pic_num'] > 9:
async with httpx.AsyncClient() as client:
res = await client.get('https://m.weibo.cn/detail/{}'.format(info['mid']))
full_json_text = re.search(r'"status": ([\s\S]+),\s+"hotScheme"', res.text).group(1)
info = json.loads(full_json_text)
parsed_text = self._get_text(info['text'])
pic_urls = [img['large']['url'] for img in info.get('pics', [])] pic_urls = [img['large']['url'] for img in info.get('pics', [])]
detail_url = 'https://weibo.com/{}/{}'.format(info['user']['id'], info['bid']) detail_url = 'https://weibo.com/{}/{}'.format(info['user']['id'], info['bid'])
# return parsed_text, detail_url, pic_urls # return parsed_text, detail_url, pic_urls
return Post('weibo', parsed_text, detail_url, pic_urls) return Post('weibo', text=parsed_text, url=detail_url, pics=pic_urls, target_name=info['user']['screen_name'])

View File

@ -1,4 +1,5 @@
from dataclasses import dataclass, field from dataclasses import dataclass, field
from typing import Optional
from .plugin_config import plugin_config from .plugin_config import plugin_config
from .utils import parse_text from .utils import parse_text
@ -8,23 +9,33 @@ class Post:
target_type: str target_type: str
text: str text: str
url: str url: str
target_name: Optional[str] = None
show_text: bool = True
pics: list[str] = field(default_factory=list) pics: list[str] = field(default_factory=list)
async def generate_messages(self): async def generate_messages(self):
if plugin_config.hk_reporter_use_pic: msgs = []
text_msg = '来源: {}\n{}'.format(self.target_type, self.text) if self.show_text:
if self.target_type == 'rss': text = '来源: {}'.format(self.target_type)
res = [await parse_text(text_msg)] if self.target_name:
elif self.target_type == 'arknights': text += '\n{}'.format(self.target_name)
res = [] if self.text:
text += '\n{}'.format(self.text)
if plugin_config.hk_reporter_use_pic:
msgs.append(await parse_text(text))
if not self.target_type == 'rss':
msgs.append(self.url)
else: else:
res = [await parse_text(text_msg), self.url] text += '详情: {}'.format(self.url)
else: msgs.append(text)
first_msg = '来源: {}\n{}\n详情:{}'.format(self.target_type, self.text, self.url)
res = [first_msg]
for pic in self.pics: for pic in self.pics:
res.append("[CQ:image,file={url}]".format(url=pic)) msgs.append("[CQ:image,file={url}]".format(url=pic))
return res return msgs
def __str__(self): def __str__(self):
return 'type: {}\ntext: {}\nurl: {}\npic: {}'.format(self.target_type, self.text[:50], self.url, ','.join(map(lambda x: 'b64img' if x.startswith('base64') else x, self.pics))) return 'type: {}\ntext: {}\nurl: {}\npic: {}'.format(
self.target_type,
self.text,
self.url,
', '.join(map(lambda x: 'b64img' if x.startswith('base64') else x, self.pics))
)