This commit is contained in:
YuCat-OVO 2024-03-01 18:58:30 +08:00
parent 74a225d2dc
commit db0ee815ba

View File

@ -25,12 +25,17 @@ GLOBAL_REQUEST_DELAY = 0.5
def get_js_url() -> str: def get_js_url() -> str:
"""请求百度网盘下载页,获取存在下载链接的js文件""" """请求百度网盘下载页,获取存在下载链接的js文件"""
max_attempts = 5
attempt = 0
url = ""
while attempt < max_attempts:
try:
r = requests.get( r = requests.get(
"https://pan.baidu.com/download", "https://pan.baidu.com/download",
headers=GLOBAL_HEADER, headers=GLOBAL_HEADER,
) )
e = etree.HTML(r.text, parser=None) e = etree.HTML(r.text, parser=None)
url = ""
# 获取包含链接的js文件 # 获取包含链接的js文件
for i in e.xpath("//script/@src"): for i in e.xpath("//script/@src"):
@ -38,6 +43,14 @@ def get_js_url() -> str:
url = i url = i
# print(url) # print(url)
return url return url
except Exception as e:
print(f"An error occurred: {e}")
attempt += 1
time.sleep(1) # 等待1秒再重试
if attempt == max_attempts:
print("Failed after several attempts")
return url
def append_json(json_file_name: str, add): def append_json(json_file_name: str, add):
@ -79,7 +92,13 @@ def parse_url_from_js():
"""从js文件中提取下载的链接并且去重,写入文件""" """从js文件中提取下载的链接并且去重,写入文件"""
url = get_js_url() url = get_js_url()
u_url = [] u_url = []
max_attempts = 5
attempt = 0
while attempt < max_attempts:
try: try:
d_url = "" d_url = ""
r = requests.get( r = requests.get(
url, url,
@ -99,8 +118,15 @@ def parse_url_from_js():
for i in d_url: for i in d_url:
if i not in u_url: if i not in u_url:
u_url.append(i) u_url.append(i)
break
except Exception as e: except Exception as e:
print(e) print(f"An error occurred: {e}")
attempt += 1
time.sleep(1) # 等待1秒再重试
if attempt == max_attempts:
print("Failed after several attempts,set url to default")
u_url = GLOBAL_VERSION u_url = GLOBAL_VERSION
with open("url.json", "w") as f: with open("url.json", "w") as f:
@ -176,6 +202,8 @@ def check_version(version):
if attempt == max_attempts: if attempt == max_attempts:
print("Failed after several attempts") print("Failed after several attempts")
return
def find_latest_version(min_version, max_version): def find_latest_version(min_version, max_version):
"""寻找最新版本号""" """寻找最新版本号"""