From a7788dd5c017c59a7acd05cbeef46aa4550551d9 Mon Sep 17 00:00:00 2001 From: seria Date: Sun, 28 Jul 2024 07:47:13 +0900 Subject: [PATCH 1/2] Add cn_fetch_cookie_token_with_stoken_v2 function --- genshin/client/manager/cookie.py | 22 ++++++++++++++++++++++ genshin/client/routes.py | 3 +++ 2 files changed, 25 insertions(+) diff --git a/genshin/client/manager/cookie.py b/genshin/client/manager/cookie.py index ef3af82e..feef7e81 100644 --- a/genshin/client/manager/cookie.py +++ b/genshin/client/manager/cookie.py @@ -14,6 +14,8 @@ - fetch_cookie_with_stoken_v2 - stoken (v2) + mid -> ltoken_v2 (token_type=2) - stoken (v2) + mid -> cookie_token_v2 (token_type=4) +- cn_fetch_cookie_token_with_stoken_v2 + - stoken (v2) + mid -> cookie_token - fetch_cookie_token_with_game_token - game_token -> cookie_token - fetch_stoken_with_game_token @@ -37,6 +39,7 @@ from genshin.utility import ds as ds_utility __all__ = [ + "cn_fetch_cookie_token_with_stoken_v2", "complete_cookies", "fetch_cookie_token_info", "fetch_cookie_token_with_game_token", @@ -116,6 +119,25 @@ async def fetch_cookie_with_stoken_v2( return cookies +async def cn_fetch_cookie_token_with_stoken_v2(cookies: managers.CookieOrHeader) -> str: + """Fetch cookie_token with an stoken (v2) and mid.""" + cookies = managers.parse_cookie(cookies) + url = routes.CN_GET_COOKIE_TOKEN_BY_STOKEN_URL.get_url() + + headers = { + "ds": ds_utility.generate_dynamic_secret(constants.DS_SALT["app_login"]), + "x-rpc-app_id": "bll8iq97cem8", + } + async with aiohttp.ClientSession() as session: + r = await session.request("GET", url, headers=headers, cookies=cookies) + data = await r.json() + + if data["retcode"] != 0: + errors.raise_for_retcode(data) + + return data["data"]["cookie_token"] + + async def fetch_cookie_token_info( cookies: managers.CookieOrHeader, *, diff --git a/genshin/client/routes.py b/genshin/client/routes.py index 5bea27ff..566ef4aa 100644 --- a/genshin/client/routes.py +++ b/genshin/client/routes.py @@ -247,6 +247,9 @@ def get_url(self, region: types.Region, game: types.Game) -> yarl.URL: COOKIE_V2_REFRESH_URL = Route("https://sg-public-api.hoyoverse.com/account/ma-passport/token/getBySToken") GET_COOKIE_TOKEN_BY_GAME_TOKEN_URL = Route("https://api-takumi.mihoyo.com/auth/api/getCookieAccountInfoByGameToken") GET_STOKEN_BY_GAME_TOKEN_URL = Route("https://passport-api.mihoyo.com/account/ma-cn-session/app/getTokenByGameToken") +CN_GET_COOKIE_TOKEN_BY_STOKEN_URL = Route( + "https://passport-api.mihoyo.com/account/auth/api/getCookieAccountInfoBySToken" +) WEB_LOGIN_URL = Route("https://sg-public-api.hoyolab.com/account/ma-passport/api/webLoginByPassword") APP_LOGIN_URL = Route("https://sg-public-api.hoyoverse.com/account/ma-passport/api/appLoginByPassword") From e9d2b6e815c8b1cfc6523192c99c5d7975df4409 Mon Sep 17 00:00:00 2001 From: seria Date: Sun, 28 Jul 2024 08:33:59 +0900 Subject: [PATCH 2/2] Return a Mapping instead --- genshin/client/manager/cookie.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/genshin/client/manager/cookie.py b/genshin/client/manager/cookie.py index feef7e81..2ee60d5b 100644 --- a/genshin/client/manager/cookie.py +++ b/genshin/client/manager/cookie.py @@ -119,7 +119,9 @@ async def fetch_cookie_with_stoken_v2( return cookies -async def cn_fetch_cookie_token_with_stoken_v2(cookies: managers.CookieOrHeader) -> str: +async def cn_fetch_cookie_token_with_stoken_v2( + cookies: managers.CookieOrHeader, +) -> typing.Mapping[typing.Literal["uid", "cookie_token"], str]: """Fetch cookie_token with an stoken (v2) and mid.""" cookies = managers.parse_cookie(cookies) url = routes.CN_GET_COOKIE_TOKEN_BY_STOKEN_URL.get_url() @@ -135,7 +137,7 @@ async def cn_fetch_cookie_token_with_stoken_v2(cookies: managers.CookieOrHeader) if data["retcode"] != 0: errors.raise_for_retcode(data) - return data["data"]["cookie_token"] + return data["data"] async def fetch_cookie_token_info(