在绑定 Cookie 时尝试自动获取 stoken

This commit is contained in:
omg-xtao 2022-10-11 20:34:54 +08:00 committed by GitHub
parent 131af7f0c1
commit ee4516982c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 5 deletions

View File

@ -2,7 +2,7 @@ import asyncio
import re import re
import time import time
from json import JSONDecodeError from json import JSONDecodeError
from typing import List, Optional from typing import List, Optional, Dict
from genshin import Client, InvalidCookies from genshin import Client, InvalidCookies
from genshin.utility.uid import recognize_genshin_server from genshin.utility.uid import recognize_genshin_server
@ -228,11 +228,12 @@ class SignIn:
"Host": "api-takumi.mihoyo.com", "Host": "api-takumi.mihoyo.com",
} }
def __init__(self, phone: int = 0): def __init__(self, phone: int = 0, uid: int = 0, cookie: Dict = None):
self.phone = phone self.phone = phone
self.client = AsyncClient() self.client = AsyncClient()
self.uid = 0 self.uid = uid
self.cookie = {} self.cookie = cookie if cookie is not None else {}
self.parse_uid()
def parse_uid(self): def parse_uid(self):
""" """
@ -240,7 +241,7 @@ class SignIn:
:param self: :param self:
:return: :return:
""" """
if "login_ticket" not in self.cookie: if not self.cookie:
return return
for item in ["login_uid", "stuid", "ltuid", "account_id"]: for item in ["login_uid", "stuid", "ltuid", "account_id"]:
if item in self.cookie: if item in self.cookie:
@ -272,10 +273,14 @@ class SignIn:
for k, v in data.cookies.items(): for k, v in data.cookies.items():
self.cookie[k] = v self.cookie[k] = v
if "login_ticket" not in self.cookie:
return False
self.parse_uid() self.parse_uid()
return bool(self.uid) return bool(self.uid)
async def get_s_token(self): async def get_s_token(self):
if not self.cookie.get("login_ticket") or not self.uid:
return
data = await self.client.get( data = await self.client.get(
self.S_TOKEN_URL.format(self.cookie["login_ticket"], self.uid), headers={"User-Agent": self.USER_AGENT} self.S_TOKEN_URL.format(self.cookie["login_ticket"], self.uid), headers={"User-Agent": self.USER_AGENT}
) )

View File

@ -1,3 +1,4 @@
import contextlib
from http.cookies import SimpleCookie, CookieError from http.cookies import SimpleCookie, CookieError
from typing import Optional from typing import Optional
@ -262,6 +263,11 @@ class SetUserCookies(Plugin.Conversation, BasePlugin.Conversation):
except (AttributeError, ValueError): except (AttributeError, ValueError):
await message.reply_text("Cookies错误请检查是否正确", reply_markup=ReplyKeyboardRemove()) await message.reply_text("Cookies错误请检查是否正确", reply_markup=ReplyKeyboardRemove())
return ConversationHandler.END return ConversationHandler.END
with contextlib.suppress(Exception):
sign_in_client = SignIn(cookie=cookies)
await sign_in_client.get_s_token()
add_user_command_data.cookies = sign_in_client.cookie
logger.info(f"用户 {user.full_name}[{user.id}] 绑定时获取 stoken 成功")
add_user_command_data.game_uid = user_info.uid add_user_command_data.game_uid = user_info.uid
reply_keyboard = [["确认", "退出"]] reply_keyboard = [["确认", "退出"]]
await message.reply_text("获取角色基础信息成功,请检查是否正确!") await message.reply_text("获取角色基础信息成功,请检查是否正确!")