优化下载文件的逻辑

This commit is contained in:
涵曦 2024-06-27 05:07:34 +00:00
parent 7a154fd847
commit 805b3c41c8
2 changed files with 17 additions and 30 deletions

View File

@ -151,10 +151,11 @@ def downloadjson():
data = request.get_json()
log.info(data)
url = data["url"]
retmsg, content = downloadfile(url)
ret = "OK"
if retmsg != "OK":
log.warning(f"downloadjson failed. url:{url} ret:{ret}")
try:
ret = "OK"
content = downloadfile(url)
except Exception as e:
log.warning(f"downloadjson failed. url:{url} e:{e}")
ret = "Download JSON file failed."
return {
"ret": ret,

View File

@ -152,36 +152,22 @@ def walk_to_depth(root, depth=None, *args, **kwargs):
def downloadfile(url):
# 清理和验证URL
try:
# 解析URL
parsed_url = urlparse(url)
# 解析URL
parsed_url = urlparse(url)
# 基础验证仅允许HTTP和HTTPS协议
if parsed_url.scheme not in ("http", "https"):
return (
f"Invalid URL scheme: {parsed_url.scheme}. Only HTTP and HTTPS are allowed.",
"",
)
# 基础验证仅允许HTTP和HTTPS协议
if parsed_url.scheme not in ("http", "https"):
raise Warning(
f"Invalid URL scheme: {parsed_url.scheme}. Only HTTP and HTTPS are allowed."
)
# 构建目标URL
cleaned_url = parsed_url.geturl()
except Exception as e:
return (f"Invalid URL: {e}", "")
# 构建目标URL
cleaned_url = parsed_url.geturl()
# 发起请求
try:
response = requests.get(cleaned_url, timeout=5) # 增加超时以避免长时间挂起
response.raise_for_status() # 如果响应不是200引发HTTPError异常
return ("OK", response.text)
except requests.exceptions.HTTPError as errh:
return (f"HTTP Error: {errh}", "")
except requests.exceptions.ConnectionError as errc:
return (f"Error Connecting: {errc}", "")
except requests.exceptions.Timeout as errt:
return (f"Timeout Error: {errt}", "")
except requests.exceptions.RequestException as err:
return (f"Oops: Something Else, {err}", "")
return ("Unknown Error", "")
response = requests.get(cleaned_url, timeout=5) # 增加超时以避免长时间挂起
response.raise_for_status() # 如果响应不是200引发HTTPError异常
return response.text
async def _get_web_music_duration(session, url, start=0, end=500):