Skip to content

Commit

Permalink
optimize downloader
Browse files Browse the repository at this point in the history
  • Loading branch information
Neutree committed May 10, 2024
1 parent fe17e7f commit bcc8a45
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions tools/file_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,15 @@ def __init__(self, url, save_path, max_thead_num = 8, callback=None, force_write
self.callback = callback
self.data_content = None
headers = self.headers.copy()
res = requests.head(self.url, headers=headers)
# 302 redirect
if res.status_code == 302:
self.url = res.headers['Location']
print(f"redirect to {self.url}")
while 1:
res = requests.head(self.url, headers=headers)
# 302 redirect
if res.status_code == 302 or res.status_code == 301:
self.url = res.headers['Location']
print(f"redirect to {self.url}")
res = requests.head(self.url, headers=headers)
else:
break
if res.status_code != 200:
raise Exception("download file error, code: {}, content: {}".format(res.status_code, res.content.decode()))
try:
Expand Down

0 comments on commit bcc8a45

Please sign in to comment.