🎨 Add cookies status update

This commit is contained in:
luoshuijs 2023-09-09 23:56:53 +08:00 committed by GitHub
parent 3ae7157b73
commit b208e31503
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 4 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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 任务完成")

View File

@ -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(