LogicGoInfotechSpaces commited on
Commit
bdd50c5
·
1 Parent(s): 1edac15

fix: robust conditional_download (handle query names, try mirrors, raise last error)

Browse files
Files changed (1) hide show
  1. DeepFakeAI/utilities.py +20 -9
DeepFakeAI/utilities.py CHANGED
@@ -160,15 +160,26 @@ def is_video(video_path : str) -> bool:
160
 
161
 
162
  def conditional_download(download_directory_path : str, urls : List[str]) -> None:
163
- if not os.path.exists(download_directory_path):
164
- os.makedirs(download_directory_path)
165
- for url in urls:
166
- download_file_path = os.path.join(download_directory_path, os.path.basename(url))
167
- if not os.path.exists(download_file_path):
168
- request = urllib.request.urlopen(url) # type: ignore[attr-defined]
169
- total = int(request.headers.get('Content-Length', 0))
170
- with tqdm(total = total, desc = wording.get('downloading'), unit = 'B', unit_scale = True, unit_divisor = 1024) as progress:
171
- urllib.request.urlretrieve(url, download_file_path, reporthook = lambda count, block_size, total_size: progress.update(block_size)) # type: ignore[attr-defined]
 
 
 
 
 
 
 
 
 
 
 
172
 
173
 
174
  def resolve_relative_path(path : str) -> str:
 
160
 
161
 
162
  def conditional_download(download_directory_path : str, urls : List[str]) -> None:
163
+ if not os.path.exists(download_directory_path):
164
+ os.makedirs(download_directory_path)
165
+ last_error: Optional[Exception] = None
166
+ for url in urls:
167
+ # handle query strings when deriving filename
168
+ base_name = os.path.basename(url.split('?')[0])
169
+ download_file_path = os.path.join(download_directory_path, base_name)
170
+ if os.path.exists(download_file_path):
171
+ return
172
+ try:
173
+ request = urllib.request.urlopen(url) # type: ignore[attr-defined]
174
+ total = int(request.headers.get('Content-Length', 0) or 0)
175
+ with tqdm(total = total, desc = wording.get('downloading'), unit = 'B', unit_scale = True, unit_divisor = 1024) as progress:
176
+ urllib.request.urlretrieve(url, download_file_path, reporthook = lambda count, block_size, total_size: progress.update(block_size)) # type: ignore[attr-defined]
177
+ return
178
+ except Exception as e: # try next mirror
179
+ last_error = e
180
+ continue
181
+ if last_error:
182
+ raise last_error
183
 
184
 
185
  def resolve_relative_path(path : str) -> str: