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

View File

@ -25,18 +25,31 @@ GLOBAL_REQUEST_DELAY = 0.5
def get_js_url() -> str: def get_js_url() -> str:
"""请求百度网盘下载页,获取存在下载链接的js文件""" """请求百度网盘下载页,获取存在下载链接的js文件"""
r = requests.get( max_attempts = 5
"https://pan.baidu.com/download", attempt = 0
headers=GLOBAL_HEADER,
)
e = etree.HTML(r.text, parser=None)
url = ""
# 获取包含链接的js文件 url = ""
for i in e.xpath("//script/@src"): while attempt < max_attempts:
if re.search("https:\\/\\/.*chunk-common.*\\.js", i): try:
url = i r = requests.get(
# print(url) "https://pan.baidu.com/download",
headers=GLOBAL_HEADER,
)
e = etree.HTML(r.text, parser=None)
# 获取包含链接的js文件
for i in e.xpath("//script/@src"):
if re.search("https:\\/\\/.*chunk-common.*\\.js", i):
url = i
# print(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 return url
@ -79,28 +92,41 @@ def parse_url_from_js():
"""从js文件中提取下载的链接并且去重,写入文件""" """从js文件中提取下载的链接并且去重,写入文件"""
url = get_js_url() url = get_js_url()
u_url = [] u_url = []
try:
d_url = ""
r = requests.get(
url,
headers=GLOBAL_HEADER,
)
# 过滤链接
d_url = re.findall(
'(?:link|url(?:_[0-9])?):"https://[a-zA-Z/\\.]*?/netdisk/[a-zA-Z10-9/\\._-]*?(?=")',
r.text,
)
# 清理 max_attempts = 5
for i in range(len(d_url)): attempt = 0
d_url[i] = re.sub('(link|url(?:_[0-9])?):"', "", d_url[i])
# 去重 while attempt < max_attempts:
for i in d_url: try:
if i not in u_url:
u_url.append(i) d_url = ""
except Exception as e: r = requests.get(
print(e) url,
headers=GLOBAL_HEADER,
)
# 过滤链接
d_url = re.findall(
'(?:link|url(?:_[0-9])?):"https://[a-zA-Z/\\.]*?/netdisk/[a-zA-Z10-9/\\._-]*?(?=")',
r.text,
)
# 清理
for i in range(len(d_url)):
d_url[i] = re.sub('(link|url(?:_[0-9])?):"', "", d_url[i])
# 去重
for i in d_url:
if i not in u_url:
u_url.append(i)
break
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,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):
"""寻找最新版本号""" """寻找最新版本号"""