From 635b8444d57354bbbbc9e0edf8f6048e71ec4bad Mon Sep 17 00:00:00 2001 From: luoshuijs Date: Tue, 17 Oct 2023 22:56:03 +0800 Subject: [PATCH] :fire: Remove support for Chinese public cookies Co-authored-by: xtaodada --- core/services/cookies/services.py | 21 +++++++++++---------- plugins/account/account.py | 8 +++++--- plugins/genshin/abyss.py | 21 ++++++--------------- plugins/genshin/stats.py | 7 ++++--- plugins/tools/genshin.py | 12 +++++++----- 5 files changed, 33 insertions(+), 36 deletions(-) diff --git a/core/services/cookies/services.py b/core/services/cookies/services.py index cddbcddc..c7cd7118 100644 --- a/core/services/cookies/services.py +++ b/core/services/cookies/services.py @@ -17,7 +17,7 @@ __all__ = ("CookiesService", "PublicCookiesService") class PublicCookiesService(BaseService, BasePublicCookiesService): - async def check_public_cookie(self, region: RegionEnum, cookies: Cookies, public_id: int): + async def check_public_cookie(self, region: RegionEnum, cookies: Cookies, public_id: int): # skipcq: PY-R1000 # if region == RegionEnum.HYPERION: client = GenshinClient(cookies=cookies.data, region=Region.CHINESE) elif region == RegionEnum.HOYOLAB: @@ -28,16 +28,17 @@ class PublicCookiesService(BaseService, BasePublicCookiesService): if client.account_id is None: raise RuntimeError("account_id not found") record_cards = await client.get_record_cards() - for record_card in record_cards: - if record_card.game == Game.GENSHIN: - await client.get_partial_genshin_user(record_card.uid) - break - else: - accounts = await client.get_game_accounts() - for account in accounts: - if account.game == Game.GENSHIN: - await client.get_partial_genshin_user(account.uid) + if client.region == Region.OVERSEAS: + for record_card in record_cards: + if record_card.game == Game.GENSHIN: + await client.get_partial_genshin_user(record_card.uid) break + else: + accounts = await client.get_game_accounts() + for account in accounts: + if account.game == Game.GENSHIN: + await client.get_partial_genshin_user(account.uid) + break except InvalidCookies as exc: if exc.ret_code in (10001, -100): logger.warning("用户 [%s] Cookies无效", public_id) diff --git a/plugins/account/account.py b/plugins/account/account.py index 8cc68db6..be35c8c9 100644 --- a/plugins/account/account.py +++ b/plugins/account/account.py @@ -86,13 +86,14 @@ class BindAccountPlugin(Plugin.Conversation): await message.reply_text("退出任务", reply_markup=ReplyKeyboardRemove()) return ConversationHandler.END if message.text == "米游社": + reply_keyboard = [["通过账号ID"], ["退出"]] bind_account_plugin_data.region = RegionEnum.HYPERION elif message.text == "HoYoLab": + reply_keyboard = [["通过玩家ID", "通过账号ID"], ["退出"]] bind_account_plugin_data.region = RegionEnum.HOYOLAB else: await message.reply_text("选择错误,请重新选择") return CHECK_SERVER - reply_keyboard = [["通过玩家ID", "通过账号ID"], ["退出"]] await message.reply_markdown_v2( "请选择你要绑定的方式", reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True) ) @@ -100,12 +101,13 @@ class BindAccountPlugin(Plugin.Conversation): @conversation.state(state=CHECK_METHOD) @handler.message(filters=filters.TEXT & ~filters.COMMAND, block=False) - async def check_method(self, update: "Update", _: "ContextTypes.DEFAULT_TYPE") -> int: + async def check_method(self, update: "Update", context: "ContextTypes.DEFAULT_TYPE") -> int: message = update.effective_message + bind_account_plugin_data: BindAccountPluginData = context.chat_data.get("bind_account_plugin_data") if message.text == "退出": await message.reply_text("退出任务", reply_markup=ReplyKeyboardRemove()) return ConversationHandler.END - if message.text == "通过玩家ID": + if message.text == "通过玩家ID" and bind_account_plugin_data.region != RegionEnum.HYPERION: await message.reply_text("请输入你的玩家ID(非通行证ID)", reply_markup=ReplyKeyboardRemove()) return CHECK_PLAYER_ID if message.text == "通过账号ID": diff --git a/plugins/genshin/abyss.py b/plugins/genshin/abyss.py index 133a6a89..bd953ef8 100644 --- a/plugins/genshin/abyss.py +++ b/plugins/genshin/abyss.py @@ -18,6 +18,7 @@ from core.plugin import Plugin, handler from core.services.cookies.error import TooManyRequestPublicCookies from core.services.template.models import RenderGroupResult, RenderResult from core.services.template.services import TemplateService +from gram_core.basemodel import RegionEnum from plugins.tools.genshin import CookiesNotFoundError, GenshinHelper from utils.log import logger from utils.uid import mask_number @@ -76,7 +77,7 @@ class AbyssPlugin(Plugin): @handler.command("abyss", block=False) @handler.message(filters.Regex(msg_pattern), block=False) - async def command_start(self, update: Update, _: CallbackContext) -> None: + async def command_start(self, update: Update, _: CallbackContext) -> None: # skipcq: PY-R1000 # user = update.effective_user message = update.effective_message uid: Optional[int] = None @@ -129,13 +130,13 @@ class AbyssPlugin(Plugin): async with self.helper.genshin(user.id) as client: await client.get_record_cards() images = await self.get_rendered_pic(client, client.player_id, floor, total, previous) - except CookiesNotFoundError: + except CookiesNotFoundError as exc: + if exc.region == RegionEnum.HYPERION: + raise exc # Cookie 不存在使用公开接口 async with self.helper.public_genshin(user.id) as client: images = await self.get_rendered_pic(client, uid, floor, total, previous) except SimnetBadRequest as exc: - if exc.ret_code == 1034 and uid and uid != client.player_id: - raise CookiesNotFoundError(uid) from exc raise exc except AbyssUnlocked: # 若深渊未解锁 await message.reply_text("还未解锁深渊哦~") @@ -171,17 +172,7 @@ class AbyssPlugin(Plugin): async def get_rendered_pic( self, client: GenshinClient, uid: int, floor: int, total: bool, previous: bool - ) -> Union[ - Tuple[ - Union[BaseException, Any], - Union[BaseException, Any], - Union[BaseException, Any], - Union[BaseException, Any], - Union[BaseException, Any], - ], - List[RenderResult], - None, - ]: + ) -> Union[Tuple[Any], List[RenderResult], None]: """ 获取渲染后的图片 diff --git a/plugins/genshin/stats.py b/plugins/genshin/stats.py index 9bc6f357..8797e357 100644 --- a/plugins/genshin/stats.py +++ b/plugins/genshin/stats.py @@ -9,6 +9,7 @@ from core.plugin import Plugin, handler from core.services.cookies.error import TooManyRequestPublicCookies from core.services.template.models import RenderResult from core.services.template.services import TemplateService +from gram_core.basemodel import RegionEnum from plugins.tools.genshin import CookiesNotFoundError, GenshinHelper from utils.log import logger from utils.uid import mask_number @@ -49,12 +50,12 @@ class PlayerStatsPlugins(Plugin): async with self.helper.genshin(user.id) as client: await client.get_record_cards() render_result = await self.render(client, uid) - except CookiesNotFoundError: + except CookiesNotFoundError as exc: + if exc.region == RegionEnum.HYPERION: + raise exc async with self.helper.public_genshin(user.id) as client: render_result = await self.render(client, uid) except SimnetBadRequest as exc: - if exc.ret_code == 1034 and uid and uid != client.player_id: - raise CookiesNotFoundError(uid) from exc raise exc except TooManyRequestPublicCookies: await message.reply_text("用户查询次数过多 请稍后重试") diff --git a/plugins/tools/genshin.py b/plugins/tools/genshin.py index 1729e9e6..43701d10 100644 --- a/plugins/tools/genshin.py +++ b/plugins/tools/genshin.py @@ -193,7 +193,9 @@ class PlayerNotFoundError(Exception): class CookiesNotFoundError(Exception): - def __init__(self, user_id): + def __init__(self, user_id: int, region: Optional[RegionEnum] = None): + self.user_id = user_id + self.region = region super().__init__(f"{user_id} cookies not found") @@ -221,10 +223,10 @@ class GenshinHelper(Plugin): raise PlayerNotFoundError(user_id) if player.account_id is None: - raise CookiesNotFoundError(user_id) + raise CookiesNotFoundError(user_id, player.region) cookie_model = await self.cookies_service.get(player.user_id, player.account_id, player.region) if cookie_model is None: - raise CookiesNotFoundError(user_id) + raise CookiesNotFoundError(user_id, player.region) cookies = cookie_model.data if player.region == RegionEnum.HYPERION: # 国服 @@ -298,10 +300,10 @@ class GenshinHelper(Plugin): raise PlayerNotFoundError(user_id) if player.account_id is None: - raise CookiesNotFoundError(user_id) + raise CookiesNotFoundError(user_id, player.region) cookie_model = await self.cookies_service.get(player.user_id, player.account_id, player.region) if cookie_model is None: - raise CookiesNotFoundError(user_id) + raise CookiesNotFoundError(user_id, player.region) cookies = cookie_model.data if player.region == RegionEnum.HYPERION: