Skip to content

Commit

Permalink
优化下载文件的逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
hanxi committed Jun 27, 2024
1 parent 7a154fd commit 805b3c4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 30 deletions.
9 changes: 5 additions & 4 deletions xiaomusic/httpserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
38 changes: 12 additions & 26 deletions xiaomusic/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,36 +152,22 @@ def fallback_func(root, depth, *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):
Expand Down

0 comments on commit 805b3c4

Please sign in to comment.