增加登录重试机制并调整超时时间 #30
This commit is contained in:
+38
-26
@@ -1,6 +1,7 @@
|
|||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
|
import time
|
||||||
from pprint import pprint
|
from pprint import pprint
|
||||||
from .zfn_api import Client
|
from .zfn_api import Client
|
||||||
|
|
||||||
@@ -64,7 +65,7 @@ def login(url, username, password):
|
|||||||
raspisanie = []
|
raspisanie = []
|
||||||
ignore_type = []
|
ignore_type = []
|
||||||
detail_category_type = []
|
detail_category_type = []
|
||||||
timeout = 5
|
timeout = 10
|
||||||
|
|
||||||
student_client = Client(
|
student_client = Client(
|
||||||
cookies=cookies,
|
cookies=cookies,
|
||||||
@@ -75,35 +76,46 @@ def login(url, username, password):
|
|||||||
timeout=timeout,
|
timeout=timeout,
|
||||||
)
|
)
|
||||||
|
|
||||||
if cookies == {}:
|
attempts = 5 # 最大重试次数
|
||||||
lgn = student_client.login(username, password)
|
while attempts > 0:
|
||||||
if lgn["code"] == 1001:
|
if cookies == {}:
|
||||||
run_log = "登录需要验证码"
|
lgn = student_client.login(username, password)
|
||||||
|
if lgn["code"] == 1001:
|
||||||
|
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"])
|
||||||
|
|
||||||
sys.exit(f"你因{run_log}原因而登录失败,错误代码为{lgn['code']}。")
|
sys.exit(f"你因{run_log}原因而登录失败,错误代码为{lgn['code']}。")
|
||||||
"""
|
"""
|
||||||
verify_data = lgn["data"]
|
verify_data = lgn["data"]
|
||||||
with open(os.path.abspath("kaptcha.png"), "wb") as pic:
|
with open(os.path.abspath("kaptcha.png"), "wb") as pic:
|
||||||
pic.write(base64.b64decode(verify_data.pop("kaptcha_pic")))
|
pic.write(base64.b64decode(verify_data.pop("kaptcha_pic")))
|
||||||
verify_data["kaptcha"] = input("输入验证码:")
|
verify_data["kaptcha"] = input("输入验证码:")
|
||||||
ret = student_client.login_with_kaptcha(**verify_data)
|
ret = student_client.login_with_kaptcha(**verify_data)
|
||||||
if ret["code"] != 1000:
|
if ret["code"] != 1000:
|
||||||
|
pprint(ret)
|
||||||
|
sys.exit()
|
||||||
pprint(ret)
|
pprint(ret)
|
||||||
sys.exit()
|
"""
|
||||||
pprint(ret)
|
elif lgn["code"] != 1000:
|
||||||
"""
|
pprint(lgn)
|
||||||
elif lgn["code"] != 1000:
|
run_log = lgn["msg"]
|
||||||
pprint(lgn)
|
|
||||||
run_log = lgn["msg"]
|
|
||||||
|
|
||||||
# 如果是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"])
|
||||||
|
|
||||||
sys.exit(0)
|
# 如果未成功登录,等待1秒后重试
|
||||||
|
time.sleep(1)
|
||||||
|
attempts -= 1 # 剩余重试次数减1
|
||||||
|
continue
|
||||||
|
|
||||||
|
# 如果登录成功,则跳出重试循环
|
||||||
|
break
|
||||||
|
|
||||||
|
if attempts == 0:
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
return student_client
|
return student_client
|
||||||
|
|||||||
+1
-1
@@ -38,7 +38,7 @@ class Client:
|
|||||||
self.raspisanie = kwargs.get("raspisanie", RASPIANIE)
|
self.raspisanie = kwargs.get("raspisanie", RASPIANIE)
|
||||||
self.ignore_type = kwargs.get("ignore_type", [])
|
self.ignore_type = kwargs.get("ignore_type", [])
|
||||||
self.detail_category_type = kwargs.get("detail_category_type", [])
|
self.detail_category_type = kwargs.get("detail_category_type", [])
|
||||||
self.timeout = kwargs.get("timeout", 3)
|
self.timeout = kwargs.get("timeout", 10)
|
||||||
Client.raspisanie = self.raspisanie
|
Client.raspisanie = self.raspisanie
|
||||||
Client.ignore_type = self.ignore_type
|
Client.ignore_type = self.ignore_type
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user