Skip to content

Bulk asynchronous downloader for multiple file hosts

No longer holding downloads in memory.

Jules-WinnfieldX108f1479be77
diff --git a/utils/downloaders.py b/utils/downloaders.py
index 8f69043c0..36af8ae91 100644
--- a/utils/downloaders.py
+++ b/utils/downloaders.py
@@ -138,11 +138,13 @@ class Downloader:
"""Download the content of given URL"""
temp_file = (self.folder / self.title / filename).with_suffix(".download")
resume_point = 0
- downloaded = bytearray()
user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36'
headers = {'Referer': referal, 'user-agent': user_agent}
+ complete_file = (self.folder / self.title / filename)
+ temp_file = complete_file.with_suffix(".download")
+
if temp_file.exists():
resume_point = temp_file.stat().st_size
headers['Range'] = 'bytes=%d-' % resume_point
@@ -157,32 +159,17 @@ class Downloader:
unit='B', leave=False, initial=resume_point,
desc=filename, disable=(not show_progress)
) as progress:
- async for chunk, _ in resp.content.iter_chunks():
- downloaded.extend(chunk)
- progress.update(len(chunk))
- await self.write_partial(temp_file, downloaded)
+ async with aiofiles.open(temp_file, mode='ab') as f:
+ async for chunk, _ in resp.content.iter_chunks():
+ await f.write(chunk)
+ progress.update(len(chunk))
+ temp_file.rename(complete_file)
+ logger.debug("Finished " + filename)
except (aiohttp.client_exceptions.ClientPayloadError, aiohttp.client_exceptions.ClientOSError,
aiohttp.client_exceptions.ServerDisconnectedError, asyncio.TimeoutError) as e:
- await self.write_partial(temp_file, downloaded)
raise FailureException(e)
- async def write_partial(self, filename: Path, downloaded: bytearray) -> None:
- """Store partial or full data into file"""
- async with aiofiles.open(filename, mode='ab') as f:
- await f.write(downloaded)
-
- async def rename_file(self, filename: str) -> None:
- """Rename complete file."""
- complete_file = (self.folder / self.title / filename)
- temp_file = complete_file.with_suffix(".download")
- if complete_file.exists():
- logger.debug(str(self.folder / self.title / filename) + " Already Exists")
- await aiofiles.os.remove(temp_file)
- else:
- temp_file.rename(complete_file)
- logger.debug("Finished " + filename)
-
async def download_and_store(
self,
url_object: list,
@@ -207,7 +194,6 @@ class Downloader:
logger.debug("Working on " + url)
try:
await self.download_file(url, referal=referal, filename=filename, session=session, headers=headers, show_progress=show_progress)
- await self.rename_file(filename)
except Exception as e:
log(f"\nSkipping {filename}: likely exceeded download attempts\nRe-run program after exit to continue download.", Fore.WHITE)