From b208e31503c4fa7ed423f11e82ea2dc65f2e23e6 Mon Sep 17 00:00:00 2001 From: luoshuijs Date: Sat, 9 Sep 2023 23:56:53 +0800 Subject: [PATCH] :art: Add cookies status update --- plugins/account/cookies.py | 2 ++ plugins/account/players.py | 2 ++ plugins/jobs/refresh_cookies.py | 33 +++++++++++++++++++++++++++++---- plugins/tools/genshin.py | 2 ++ 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/plugins/account/cookies.py b/plugins/account/cookies.py index 03ba5db..6325fb0 100644 --- a/plugins/account/cookies.py +++ b/plugins/account/cookies.py @@ -382,6 +382,7 @@ class AccountCookiesPlugin(Plugin.Conversation): account_id=account_cookies_plugin_data.account_id, data=account_cookies_plugin_data.cookies, region=account_cookies_plugin_data.region, + status=CookiesStatusEnum.STATUS_SUCCESS, is_share=True, # todo 用户可以自行选择是否将Cookies加入公共池 ) await self.cookies_service.add(cookies) @@ -410,6 +411,7 @@ class AccountCookiesPlugin(Plugin.Conversation): account_id=account_cookies_plugin_data.account_id, data=account_cookies_plugin_data.cookies, region=account_cookies_plugin_data.region, + status=CookiesStatusEnum.STATUS_SUCCESS, is_share=True, # todo 用户可以自行选择是否将Cookies加入公共池 ) await self.cookies_service.add(cookies) diff --git a/plugins/account/players.py b/plugins/account/players.py index 1710338..70edee0 100644 --- a/plugins/account/players.py +++ b/plugins/account/players.py @@ -11,6 +11,7 @@ from core.plugin import Plugin, handler from core.services.cookies import CookiesService from core.services.players import PlayersService from core.services.players.services import PlayerInfoService +from gram_core.services.cookies.models import CookiesStatusEnum from modules.apihelper.models.genshin.cookies import CookiesModel from utils.log import logger @@ -226,6 +227,7 @@ class PlayersManagesPlugin(Plugin): ) return cookies_data.data = cookies.to_dict() + cookies_data.status = CookiesStatusEnum.STATUS_SUCCESS await self.cookies_service.update(cookies_data) await callback_query.edit_message_text( f"玩家 {player.player_id} {player_info.nickname} cookies 刷新成功", reply_markup=InlineKeyboardMarkup(buttons) diff --git a/plugins/jobs/refresh_cookies.py b/plugins/jobs/refresh_cookies.py index 8b18c21..6bf5bce 100644 --- a/plugins/jobs/refresh_cookies.py +++ b/plugins/jobs/refresh_cookies.py @@ -3,11 +3,18 @@ from typing import TYPE_CHECKING, Dict from simnet import Region from simnet.client.components.auth import AuthClient -from simnet.errors import TimedOut as SimnetTimedOut, BadRequest as SimnetBadRequest, NetworkError as SimnetNetworkError +from simnet.errors import ( + TimedOut as SimnetTimedOut, + BadRequest as SimnetBadRequest, + NetworkError as SimnetNetworkError, + InvalidCookies, +) +from sqlalchemy.orm.exc import StaleDataError from core.plugin import Plugin, job from gram_core.basemodel import RegionEnum from gram_core.services.cookies import CookiesService +from gram_core.services.cookies.models import CookiesStatusEnum from utils.log import logger if TYPE_CHECKING: @@ -27,22 +34,40 @@ class RefreshCookiesJob(Plugin): }.items(): for cookie_model in await self.cookies.get_all_by_region(db_region): cookies = cookie_model.data - if cookies.get("stoken"): + if cookies.get("stoken") is not None and cookie_model.status != CookiesStatusEnum.INVALID_COOKIES: try: async with AuthClient(cookies=cookies, region=client_region) as client: new_cookies: Dict[str, str] = cookies.copy() new_cookies["cookie_token"] = await client.get_cookie_token_by_stoken() new_cookies["ltoken"] = await client.get_ltoken_by_stoken() cookie_model.data = new_cookies - await self.cookies.update(cookie_model) + cookie_model.status = CookiesStatusEnum.STATUS_SUCCESS except ValueError: - continue + cookie_model.status = CookiesStatusEnum.INVALID_COOKIES + logger.warning("用户 user_id[%s] Cookies 不完整", cookie_model.user_id) + except InvalidCookies: + cookie_model.status = CookiesStatusEnum.INVALID_COOKIES + logger.info("用户 user_id[%s] Cookies 已经过期", cookie_model.user_id) except SimnetBadRequest: logger.warning("用户 user_id[%s] 刷新 Cookies 时出现错误", cookie_model.user_id) + continue except SimnetTimedOut: logger.warning("用户 user_id[%s] 刷新 Cookies 时超时", cookie_model.user_id) + continue except SimnetNetworkError: logger.warning("用户 user_id[%s] 刷新 Cookies 时网络错误", cookie_model.user_id) + continue except Exception as _exc: logger.error("用户 user_id[%s] 刷新 Cookies 失败", cookie_model.user_id, exc_info=_exc) + continue + try: + await self.cookies.update(cookie_model) + except StaleDataError as _exc: + if "UPDATE" in str(_exc): + logger.warning("用户 user_id[%s] 刷新 Cookies 失败,数据不存在", cookie_model.user_id) + else: + logger.error("用户 user_id[%s] 更新 Cookies 时出现错误", cookie_model.user_id, exc_info=_exc) + except Exception as _exc: + logger.error("用户 user_id[%s] 更新 Cookies 状态失败", cookie_model.user_id, exc_info=_exc) + logger.success("执行每日刷新 Cookies 任务完成") diff --git a/plugins/tools/genshin.py b/plugins/tools/genshin.py index 0fc671b..f745588 100644 --- a/plugins/tools/genshin.py +++ b/plugins/tools/genshin.py @@ -24,6 +24,7 @@ from core.services.devices import DevicesService from core.services.players.services import PlayersService from core.services.users.services import UserService from core.sqlmodel.session import AsyncSession +from gram_core.services.cookies.models import CookiesStatusEnum from utils.log import logger if TYPE_CHECKING: @@ -260,6 +261,7 @@ class GenshinHelper(Plugin): new_cookies["ltoken"] = await client.get_ltoken_by_stoken() logger.success("用户 %s 刷新 ltoken 成功", user_id) cookie_model.data = new_cookies + cookie_model.status = CookiesStatusEnum.STATUS_SUCCESS await self.cookies_service.update(cookie_model) except SimnetBadRequest as _exc: logger.warning(