refactor(login): 支持 session / cookies 复用并统一登录逻辑,精简重复代码
This commit is contained in:
+49
-8
@@ -68,14 +68,26 @@ def write_github_summary(run_log, lgn_code):
|
|||||||
file.write(summary_log)
|
file.write(summary_log)
|
||||||
|
|
||||||
|
|
||||||
def login(url, username, password):
|
def login(url, username=None, password=None, session=None, cookies=None):
|
||||||
|
"""
|
||||||
|
封装登录逻辑,兼容三种场景:
|
||||||
|
1. 直接复用已登录的 requests.Session
|
||||||
|
2. 使用现成 cookies
|
||||||
|
3. 账号密码登录
|
||||||
|
"""
|
||||||
|
# 统一 cookies 为 dict,方便后续判断
|
||||||
|
if cookies is None:
|
||||||
cookies = {}
|
cookies = {}
|
||||||
|
|
||||||
base_url = url
|
base_url = url
|
||||||
raspisanie = []
|
raspisanie = []
|
||||||
ignore_type = []
|
ignore_type = []
|
||||||
detail_category_type = []
|
detail_category_type = []
|
||||||
timeout = 10
|
timeout = 10
|
||||||
|
|
||||||
|
# 若已提供 session,则直接复用
|
||||||
|
if session is not None:
|
||||||
|
cookies = session.cookies.get_dict()
|
||||||
student_client = Client(
|
student_client = Client(
|
||||||
cookies=cookies,
|
cookies=cookies,
|
||||||
base_url=base_url,
|
base_url=base_url,
|
||||||
@@ -84,15 +96,44 @@ def login(url, username, password):
|
|||||||
detail_category_type=detail_category_type,
|
detail_category_type=detail_category_type,
|
||||||
timeout=timeout,
|
timeout=timeout,
|
||||||
)
|
)
|
||||||
|
# 复用会话,保持连接池、代理等设置
|
||||||
|
student_client.sess = session
|
||||||
|
return student_client
|
||||||
|
|
||||||
|
# 若提供了 cookies,直接使用
|
||||||
|
if cookies:
|
||||||
|
student_client = Client(
|
||||||
|
cookies=cookies,
|
||||||
|
base_url=base_url,
|
||||||
|
raspisanie=raspisanie,
|
||||||
|
ignore_type=ignore_type,
|
||||||
|
detail_category_type=detail_category_type,
|
||||||
|
timeout=timeout,
|
||||||
|
)
|
||||||
|
return student_client
|
||||||
|
|
||||||
|
# 走到这里说明只能账号密码登录,缺失则直接退出
|
||||||
|
if username is None or password is None:
|
||||||
|
sys.exit("未提供有效登录方式(session/cookies/账号密码均为空),无法登录。")
|
||||||
|
|
||||||
|
# 账号密码登录
|
||||||
|
student_client = Client(
|
||||||
|
cookies={},
|
||||||
|
base_url=base_url,
|
||||||
|
raspisanie=raspisanie,
|
||||||
|
ignore_type=ignore_type,
|
||||||
|
detail_category_type=detail_category_type,
|
||||||
|
timeout=timeout,
|
||||||
|
)
|
||||||
|
|
||||||
attempts = 5 # 最大重试次数
|
attempts = 5 # 最大重试次数
|
||||||
while attempts > 0:
|
while attempts > 0:
|
||||||
if cookies == {}:
|
|
||||||
lgn = student_client.login(username, password)
|
lgn = student_client.login(username, password)
|
||||||
|
|
||||||
if lgn["code"] == 1001:
|
if lgn["code"] == 1001:
|
||||||
run_log = "登录需要验证码"
|
run_log = "登录需要验证码"
|
||||||
|
|
||||||
# 如果是Github Actions运行,则将运行日志写入到GitHub Actions的日志文件中
|
# 如果是 GitHub Actions 运行,则将运行日志写入到 GitHub Actions 的日志文件中
|
||||||
if github_actions:
|
if github_actions:
|
||||||
write_github_summary(run_log, lgn["code"])
|
write_github_summary(run_log, lgn["code"])
|
||||||
|
|
||||||
@@ -109,23 +150,23 @@ def login(url, username, password):
|
|||||||
pprint(ret)
|
pprint(ret)
|
||||||
"""
|
"""
|
||||||
elif lgn["code"] != 1000:
|
elif lgn["code"] != 1000:
|
||||||
|
# 登录失败但非验证码
|
||||||
if attempts == 1:
|
if attempts == 1:
|
||||||
pprint(lgn)
|
pprint(lgn)
|
||||||
run_log = lgn["msg"]
|
run_log = lgn["msg"]
|
||||||
|
|
||||||
# 如果是Github Actions运行,则将运行日志写入到GitHub Actions的日志文件中
|
|
||||||
if github_actions:
|
if github_actions:
|
||||||
write_github_summary(run_log, lgn["code"])
|
write_github_summary(run_log, lgn["code"])
|
||||||
|
|
||||||
# 如果未成功登录,等待1秒后重试
|
# 重试
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
attempts -= 1 # 剩余重试次数减1
|
attempts -= 1
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# 如果登录成功,则跳出重试循环
|
# 登录成功,跳出循环
|
||||||
break
|
break
|
||||||
|
|
||||||
if attempts == 0:
|
if attempts == 0:
|
||||||
|
# 尝试次数耗尽依旧失败
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
return student_client
|
return student_client
|
||||||
|
|||||||
Reference in New Issue
Block a user