MibooGram/utils/genshin.py

65 lines
2.3 KiB
Python
Raw Normal View History

2022-12-10 12:37:43 +00:00
from typing import Optional
from genshin import Client
from genshin.client.routes import InternationalRoute # noqa F401
2022-12-23 15:44:40 +00:00
from genshin.utility import recognize_genshin_server
2022-12-11 05:50:03 +00:00
2022-12-23 15:44:40 +00:00
from modules.apihelper.utility.helpers import hex_digest, get_ds
2022-12-10 12:37:43 +00:00
AUTHKEY_API = "https://api-takumi.mihoyo.com/binding/api/genAuthKey"
HK4E_LOGIN_URL = InternationalRoute(
overseas="https://sg-public-api.hoyoverse.com/common/badge/v1/login/account",
chinese="https://api-takumi.mihoyo.com/common/badge/v1/login/account",
)
2022-12-11 05:50:03 +00:00
GACHA_HEADERS = {
"User-Agent": "okhttp/4.8.0",
"x-rpc-sys_version": "12",
"x-rpc-channel": "mihoyo",
"x-rpc-device_id": "",
"x-rpc-device_name": "",
"x-rpc-device_model": "",
"Referer": "https://app.mihoyo.com",
"Host": "api-takumi.mihoyo.com",
}
2022-12-10 12:37:43 +00:00
def recognize_genshin_game_biz(game_uid: int) -> str:
return "hk4e_cn" if game_uid < 600000000 else "hk4e_global"
2022-12-10 12:37:43 +00:00
async def get_authkey_by_stoken(client: Client) -> Optional[str]:
"""通过 stoken 获取 authkey"""
2022-12-11 05:50:03 +00:00
headers = GACHA_HEADERS.copy()
2022-12-10 12:37:43 +00:00
json = {
"auth_appid": "webview_gacha",
"game_biz": recognize_genshin_game_biz(client.uid),
2022-12-10 12:37:43 +00:00
"game_uid": client.uid,
"region": recognize_genshin_server(client.uid),
}
2022-12-11 05:50:03 +00:00
device_id = hex_digest(str(client.uid))
headers["x-rpc-device_id"] = device_id
device = f"Paimon Build {device_id[:5]}"
2022-12-11 05:50:03 +00:00
headers["x-rpc-device_name"] = device
headers["x-rpc-device_model"] = device
2022-12-23 15:44:40 +00:00
app_version, client_type, ds_sign = get_ds()
headers["x-rpc-app_version"] = app_version
headers["x-rpc-client_type"] = client_type
headers["ds"] = ds_sign
2022-12-11 05:50:03 +00:00
data = await client.cookie_manager.request(AUTHKEY_API, method="POST", json=json, headers=headers)
2022-12-10 12:37:43 +00:00
return data.get("authkey")
async def fetch_hk4e_token_by_cookie(client: Client) -> None:
"""通过 cookie_token 获取 hk4e_token 保存到 client"""
url = HK4E_LOGIN_URL.get_url(client.region)
headers = {
"Content-Type": "application/json;charset=UTF-8",
}
json = {
"game_biz": recognize_genshin_game_biz(client.uid),
"lang": "zh-cn",
"uid": str(client.uid),
"region": recognize_genshin_server(client.uid),
}
await client.cookie_manager.request(url, method="POST", json=json, headers=headers)