From 4f3325d988cef18a2bfa76908ace6aeaf59e76e5 Mon Sep 17 00:00:00 2001 From: felinae98 <731499577@qq.com> Date: Sun, 1 Aug 2021 15:23:40 +0800 Subject: [PATCH] add version check for arknights --- .../nonebot_hk_reporter/platform/__init__.py | 22 ++++++-- .../nonebot_hk_reporter/platform/arknights.py | 53 +++++++++++++++++-- .../nonebot_hk_reporter/platform/platform.py | 7 ++- src/plugins/nonebot_hk_reporter/post.py | 2 +- 4 files changed, 71 insertions(+), 13 deletions(-) diff --git a/src/plugins/nonebot_hk_reporter/platform/__init__.py b/src/plugins/nonebot_hk_reporter/platform/__init__.py index e181c8f..e6283e2 100644 --- a/src/plugins/nonebot_hk_reporter/platform/__init__.py +++ b/src/plugins/nonebot_hk_reporter/platform/__init__.py @@ -1,4 +1,6 @@ -from .platform import Platform +from collections import defaultdict + +from .platform import Platform, NoTargetGroup from pkgutil import iter_modules from pathlib import Path from importlib import import_module @@ -11,7 +13,17 @@ for (_, module_name, _) in iter_modules([_package_dir]): async def check_sub_target(target_type, target): return await platform_manager[target_type].get_target_name(target) -platform_manager: dict[str, Platform] = { - obj.platform_name: obj() for obj in \ - filter(lambda platform: platform.enabled, Platform.registory) - } +_platform_list = defaultdict(list) +for platform in Platform.registory: + if not platform.enabled: + continue + _platform_list[platform.platform_name].append(platform) + +platform_manager: dict[str, Platform] = dict() +for name, platform_list in _platform_list.items(): + if len(platform_list) == 1: + platform_manager[name] = platform_list[0]() + else: + platform_manager[name] = NoTargetGroup(platform_list) + +print(platform_manager) diff --git a/src/plugins/nonebot_hk_reporter/platform/arknights.py b/src/plugins/nonebot_hk_reporter/platform/arknights.py index c7de7e5..1db465d 100644 --- a/src/plugins/nonebot_hk_reporter/platform/arknights.py +++ b/src/plugins/nonebot_hk_reporter/platform/arknights.py @@ -3,9 +3,9 @@ import httpx import json from bs4 import BeautifulSoup as bs -from ..types import RawPost, Target +from ..types import Category, RawPost, Target -from .platform import NewMessage, NoTargetMixin, CategoryNotSupport +from .platform import NewMessage, NoTargetMixin, CategoryNotSupport, StatusChange from ..utils import Render from ..post import Post @@ -13,9 +13,9 @@ from ..post import Post class Arknights(NewMessage, NoTargetMixin): - categories = {} + categories = {1: '游戏公告'} platform_name = 'arknights' - name = '明日方舟游戏内公告' + name = '明日方舟游戏信息' enable_tag = False enabled = True is_common = False @@ -23,7 +23,7 @@ class Arknights(NewMessage, NoTargetMixin): schedule_kw = {'seconds': 30} async def get_target_name(self, _: Target) -> str: - return '明日方舟游戏内公告' + return '明日方舟游戏信息' async def get_sub_list(self, _) -> list[RawPost]: async with httpx.AsyncClient() as client: @@ -36,6 +36,9 @@ class Arknights(NewMessage, NoTargetMixin): def get_date(self, _: RawPost) -> None: return None + def get_category(self, _) -> Category: + return Category(1) + async def parse(self, raw_post: RawPost) -> Post: announce_url = raw_post['webUrl'] async with httpx.AsyncClient() as client: @@ -53,3 +56,43 @@ class Arknights(NewMessage, NoTargetMixin): else: raise CategoryNotSupport() return Post('arknights', text='', url='', target_name="明日方舟游戏内公告", pics=pics, compress=True, override_use_pic=False) + +class AkVersion(NoTargetMixin, StatusChange): + + categories = {2: '更新信息'} + platform_name = 'arknights' + name = '明日方舟游戏信息' + enable_tag = False + enabled = True + is_common = False + schedule_type = 'interval' + schedule_kw = {'seconds': 30} + + async def get_target_name(self, _: Target) -> str: + return '明日方舟游戏信息' + + async def get_status(self, _): + async with httpx.AsyncClient() as client: + res_ver = await client.get('https://ak-conf.hypergryph.com/config/prod/official/IOS/version') + res_preanounce = await client.get('https://ak-conf.hypergryph.com/config/prod/announce_meta/IOS/preannouncement.meta.json') + res = res_ver.json() + res.update(res_preanounce.json()) + return res + + async def compare_status(self, _, old_status, new_status): + res = [] + if old_status.get('preAnnounceType') == 2 and new_status.get('preAnnounceType') == 0: + res.append(Post('arknights', text='开始维护!', target_name='明日方舟更新信息')) + elif old_status.get('preAnnounceType') == 0 and new_status.get('preAnnounceType') == 2: + res.append(Post('arknights', text='维护结束!冲!', target_name='明日方舟更新信息')) + if old_status.get('clientVersion') != new_status.get('clientVersion'): + res.append(Post('arknights', text='游戏本体更新(大更新)', target_name='明日方舟更新信息')) + if old_status.get('resVersion') != new_status.get('resVersion'): + res.append(Post('arknights', text='游戏资源更新(小更新)', target_name='明日方舟更新信息')) + return res + + def get_category(self, _): + return Category(2) + + async def parse(self, raw_post): + return raw_post diff --git a/src/plugins/nonebot_hk_reporter/platform/platform.py b/src/plugins/nonebot_hk_reporter/platform/platform.py index 25f7e96..488db16 100644 --- a/src/plugins/nonebot_hk_reporter/platform/platform.py +++ b/src/plugins/nonebot_hk_reporter/platform/platform.py @@ -280,7 +280,7 @@ class StatusChange( ... @abstractmethod - def compare_status(self, target: Target, old_status, new_status) -> Optional[RawPost]: + def compare_status(self, target: Target, old_status, new_status) -> list[RawPost]: ... @abstractmethod @@ -294,7 +294,7 @@ class StatusChange( if old_status := self.get_stored_data(target): diff = self.compare_status(target, old_status, new_status) if diff: - res = await self.dispatch_user_post(target, [diff], users) + res = await self.dispatch_user_post(target, diff, users) self.set_stored_data(target, new_status) return res except httpx.RequestError as err: @@ -337,6 +337,9 @@ class NoTargetGroup( self.is_common = platform_list[0].is_common super().__init__() + def __str__(self): + return '[' + ' '.join(map(lambda x: x.name, self.platform_list)) + ']' + async def get_target_name(self, _): return await self.platform_list[0].get_target_name(_) diff --git a/src/plugins/nonebot_hk_reporter/post.py b/src/plugins/nonebot_hk_reporter/post.py index eb5d91a..e8ad59c 100644 --- a/src/plugins/nonebot_hk_reporter/post.py +++ b/src/plugins/nonebot_hk_reporter/post.py @@ -15,7 +15,7 @@ class Post: target_type: str text: str - url: Optional[str] + url: Optional[str] = None target_name: Optional[str] = None compress: bool = False override_use_pic: Optional[bool] = None