This commit is contained in:
2024-07-18 11:24:37 +08:00
parent 8b9fa46ff1
commit 8291421943
2 changed files with 40 additions and 0 deletions
View File
+40
View File
@@ -0,0 +1,40 @@
import requests
import json
import os
from pathlib import Path
project_path = Path(__file__).parent.parent.absolute()
data_path = os.path.join(project_path, 'data')
def hg_get_new_user_token(username, password):
login_url = 'https://ak.hypergryph.com/user/login'
session = requests.session()
session.get(login_url)
login_data = json.dumps({"phone": username, "password": password})
response = session.post('https://as.hypergryph.com/user/auth/v1/token_by_phone_password', data=login_data,
headers={"Content-Type": "application/json"})
token_data = json.loads(response.text)['data']['token']
return token_data
def load_existing_token(token_path):
with open(token_path, 'r', encoding='utf-8') as f:
token_data = json.load(f)
return token_data
def hg_login(username, password, token_path=None):
if not token_path:
token_path = os.path.join(data_path, 'token')
user_token = load_existing_token(token_path)
get_gacha_url = f'https://ak.hypergryph.com/user/api/inquiry/gacha?page=1&token={user_token}&channelId=1'
gacha_response = requests.get(get_gacha_url).text
if gacha_response == '{"code":3000,"msg":"登录失效"}':
hg_get_new_user_token(username, password)
get_gacha_url = f'https://ak.hypergryph.com/user/api/inquiry/gacha?page=1&token={user_token}&channelId=1'
gacha_response = requests.get(get_gacha_url).text
if gacha_response == '{"code":3000,"msg":"登录失效"}':
print('用户名/密码错误')
raise ValueError