Skip to content

Commit

Permalink
获取cookie token支持
Browse files Browse the repository at this point in the history
  • Loading branch information
Womsxd committed Oct 6, 2023
1 parent 69edad0 commit f405b07
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
21 changes: 19 additions & 2 deletions login.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
from loghelper import log
from error import CookieError

headers = setting.headers.copy()
headers.pop("DS")
headers.pop("Origin")
headers.pop("Referer")


def login():
if config.config["account"]["cookie"] == '':
Expand All @@ -21,7 +26,7 @@ def login():
uid = get_uid()
if uid is not None:
config.config["account"]["stuid"] = uid
data = http.get(url=setting.bbs_cookie_url2.format(login_ticket, uid)).json()
data = http.get(url=setting.bbs_cookie_url2.format(login_ticket, uid), headers=headers).json()
config.config["account"]["stoken"] = data["data"]["list"][0]["token"]
log.info("登录成功!")
log.info("正在保存Config!")
Expand All @@ -42,9 +47,21 @@ def get_uid() -> str:
uid_match = re.search(r"(account_id|ltuid|login_uid)=(\d+)", config.config["account"]["cookie"])
if uid_match is None:
# stuid就是uid,先搜索cookie里面的,搜不到再用api获取
data = http.get(url=setting.bbs_account_info.format(config.config["account"]["login_ticket"])).json()
data = http.get(url=setting.bbs_account_info.format(config.config["account"]["login_ticket"]),
headers=headers).json()
if "成功" in data["data"]["msg"]:
uid = str(data["data"]["cookie_info"]["account_id"])
else:
uid = uid_match.group(2)
return uid


def get_cookie_token_by_stoken():
data = http.get(url=setting.bbs_get_cookie_token_by_stoken,
params={"stoken": config.config["account"]["stoken"], "uid": config.config["account"]["stuid"]},
headers=headers).json()
if data.get("retcode", -1) != 0:
log.error("stoken已失效,请重新抓取cookie")
config.clear_cookies()
raise CookieError('Cookie expires')
return data["data"]["cookie_token"]
1 change: 1 addition & 0 deletions setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
# 米游社的API列表
bbs_account_info = "https://webapi.account.mihoyo.com/Api/cookie_accountinfo_by_loginticket?login_ticket={}"
bbs_cookie_url2 = web_api + "/auth/api/getMultiTokenByLoginTicket?login_ticket={}&token_types=3&uid={}"
bbs_get_cookie_token_by_stoken = web_api + "/auth/api/getCookieAccountInfoBySToken"
bbs_tasks_list = bbs_api + "/apihub/sapi/getUserMissionsState" # 获取任务列表
bbs_sign_url = bbs_api + "/apihub/app/api/signIn" # post
bbs_post_list_url = bbs_api + "/post/api/getForumPostList?forum_id={}&is_good=false&is_hot=false&page_size=20&sort_type=1"
Expand Down

0 comments on commit f405b07

Please sign in to comment.