Added code to get the old account_id

This commit is contained in:
omg-xtao 2022-11-26 16:31:05 +08:00 committed by GitHub
parent ceff8d0340
commit 09b211d65d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 2 deletions

View File

@ -238,6 +238,7 @@ class SignIn:
"Accept-Language": "zh-CN,zh-Hans;q=0.9",
}
AUTHKEY_API = "https://api-takumi.mihoyo.com/binding/api/genAuthKey"
USER_INFO_API = "https://bbs-api.mihoyo.com/user/wapi/getUserFullInfo"
GACHA_HEADERS = {
"User-Agent": "okhttp/4.8.0",
"x-rpc-app_version": "2.28.1",
@ -332,7 +333,7 @@ class SignIn:
for k, v in data.cookies.items():
self.cookie[k] = v
return "cookie_token" in self.cookie
return "cookie_token" in self.cookie or "cookie_token_v2" in self.cookie
@staticmethod
async def get_authkey_by_stoken(client: Client) -> Optional[str]:
@ -358,6 +359,26 @@ class SignIn:
pass
return None
@staticmethod
async def get_v2_account_id(client: Client) -> Optional[int]:
"""获取 v2 account_id"""
try:
headers = SignIn.GACHA_HEADERS.copy()
headers["DS"] = generate_dynamic_secret("ulInCDohgEs557j0VsPDYnQaaz6KJcv5")
data = await client.cookie_manager.request(
SignIn.USER_INFO_API,
method="GET",
params={"gids": "2"},
headers=headers,
)
uid = data.get("user_info", {}).get("uid", None)
if uid:
uid = int(uid)
return uid
except JSONDecodeError:
pass
return None
class Verification:
HOST = "api-takumi-record.mihoyo.com"

View File

@ -34,6 +34,10 @@ class AddUserCommandData(TelegramObject):
sign_in_client: Optional[SignIn] = None
class GetAccountIdException(Exception):
pass
CHECK_SERVER, CHECK_PHONE, CHECK_CAPTCHA, INPUT_COOKIES, COMMAND_RESULT = range(10100, 10105)
@ -255,6 +259,13 @@ class SetUserCookies(Plugin.Conversation, BasePlugin.Conversation):
await message.reply_text("数据错误", reply_markup=ReplyKeyboardRemove())
return ConversationHandler.END
try:
if "account_mid_v2" in cookies:
logger.info("检测到用户 %s[%s] 使用 V2 Cookie 正在尝试获取 account_id", user.full_name, user.id)
account_id = await SignIn.get_v2_account_id(client)
if account_id is None:
raise GetAccountIdException
logger.success("获取用户 %s[%s] account_id[%s] 成功", user.full_name, user.id, account_id)
add_user_command_data.cookies["account_id"] = account_id
genshin_accounts = await client.genshin_accounts()
except DataNotPublic:
await message.reply_text("账号疑似被注销,请检查账号状态", reply_markup=ReplyKeyboardRemove())
@ -267,11 +278,14 @@ class SetUserCookies(Plugin.Conversation, BasePlugin.Conversation):
f"获取账号信息发生错误,错误信息为 {str(exc)}请检查Cookie或者账号是否正常", reply_markup=ReplyKeyboardRemove()
)
return ConversationHandler.END
except GetAccountIdException:
await message.reply_text("获取账号ID发生错误请检查Cookie或者账号是否正常", reply_markup=ReplyKeyboardRemove())
return ConversationHandler.END
except (AttributeError, ValueError):
await message.reply_text("Cookies错误请检查是否正确", reply_markup=ReplyKeyboardRemove())
return ConversationHandler.END
with contextlib.suppress(Exception):
sign_in_client = SignIn(cookie=cookies)
sign_in_client = SignIn(cookie=add_user_command_data.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 成功")