diff --git a/src/plugins/hk_reporter/platform/arkninghts.py b/src/plugins/hk_reporter/platform/arkninghts.py index cf650e3..325d113 100644 --- a/src/plugins/hk_reporter/platform/arkninghts.py +++ b/src/plugins/hk_reporter/platform/arkninghts.py @@ -9,8 +9,7 @@ from nonebot import logger from ..utils import Singleton, Render from ..post import Post -@Singleton -class Arknights: +class Arknights(metaclass=Singleton): def __init__(self): self.exists_posts = defaultdict(set) @@ -21,7 +20,7 @@ class Arknights: raw_data = client.get('http://ak-fs.hypergryph.com/announce/IOS/announcement.meta.json') return json.loads(raw_data.text) - def filter(self, announce_data, inited=False) -> list[Post]: + async def filter(self, announce_data, inited=False) -> list[Post]: announce_list = announce_data['announceList'] res: list[Post] = [] for announce in announce_list: @@ -31,13 +30,36 @@ class Arknights: if announce['announceId'] in self.exists_posts['default']: continue res.append(await self.parse(announce['webUrl'])) + if None in res: + res.remove(None) return res async def parse(self, announce_url: str) -> Post: async with httpx.AsyncClient() as client: raw_html = client.get(announce_url) soup = bs(raw_html, 'html.parser') + pics = [] if soup.find("div", class_="standerd-container"): # 图文 render = Render() - render. + viewport = {'width': 320, 'height': 6400, 'deviceScaleFactor': 3} + pic_data = await render.render(announce_url, viewport=viewport, target='div.main') + pics.append('base64://{}'.format(pic_data)) + elif (pic := soup.find('img', class_='banner-image')): + pics.append(pic['src']) + else: + return None + + async def fetch_new_post(self, _) -> list[Post]: + try: + data = await self.get_announce_list() + if self.inited['default'] or True: + return await self.filter(data) + else: + await self.filter(data, True) + self.inited['default'] = True + return [] + except httpx.RequestError as err: + logger.warning("network connection error: {}, url: {}".format(type(err), err.request.url)) + return [] + diff --git a/src/plugins/hk_reporter/platform/utils.py b/src/plugins/hk_reporter/platform/utils.py index 215cae4..1562ea0 100644 --- a/src/plugins/hk_reporter/platform/utils.py +++ b/src/plugins/hk_reporter/platform/utils.py @@ -6,6 +6,7 @@ from collections import defaultdict from .weibo import Weibo, get_user_info as weibo_user_info from .bilibili import Bilibili, get_user_info as bilibili_user_info from .rss import Rss, get_rss_info as rss_info +from .arkninghts import Arknights from ..config import Config from ..post import Post from ..send import send_msgs @@ -17,6 +18,8 @@ async def check_sub_target(target_type, target): return await bilibili_user_info(target) elif target_type == 'rss': return await rss_info(target) + elif target_type == 'arknights': + return '明日方舟游戏公告' else: return None @@ -35,7 +38,8 @@ async def fetch_and_send(target_type: str): platform_manager = { 'bilibili': Bilibili(), 'weibo': Weibo(), - 'rss': Rss() + 'rss': Rss(), + 'arkninghts': Arknights() } target = config.get_next_target(target_type) if not target: diff --git a/src/plugins/hk_reporter/scheduler.py b/src/plugins/hk_reporter/scheduler.py index 0e36534..e28e802 100644 --- a/src/plugins/hk_reporter/scheduler.py +++ b/src/plugins/hk_reporter/scheduler.py @@ -18,6 +18,10 @@ async def bilibili_check(): async def rss_check(): await fetch_and_send('rss') +@scheduler.scheduled_job('interval', seconds=30) +async def arknights_check(): + await fetch_and_send('arknights') + @scheduler.scheduled_job('interval', seconds=1) async def _(): await do_send_msgs()