优化下载文件的逻辑

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() data = request.get_json()
log.info(data) log.info(data)
url = data["url"] url = data["url"]
retmsg, content = downloadfile(url) try:
ret = "OK" ret = "OK"
if retmsg != "OK": content = downloadfile(url)
log.warning(f"downloadjson failed. url:{url} ret:{ret}") except Exception as e:
log.warning(f"downloadjson failed. url:{url} e:{e}")
ret = "Download JSON file failed." ret = "Download JSON file failed."
return { return {
"ret": ret, "ret": ret,

View File

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