Bulk asynchronous downloader for multiple file hosts
Python asynciobunkrcyberdropdownloaderdropboxfilestergofilegoogle-drivemediafiremegamega-nzonedrivepatreonpawchivepinterestpixeldraintransfer-ittwitchtwittervk
Deduplicate calculation of threads number
Arkadiy Illarionovd1e62a9bd9ce
diff --git a/cyberdrop_dl/downloader/downloader_utils.py b/cyberdrop_dl/downloader/downloader_utils.pyindex 543285748..13508cc52 100644--- a/cyberdrop_dl/downloader/downloader_utils.py+++ b/cyberdrop_dl/downloader/downloader_utils.py@@ -1,3 +1,4 @@+import multiprocessingimport shutilfrom base64 import b64encodefrom enum import IntEnum@@ -41,6 +42,13 @@ async def check_free_space(required_space_gb: int, download_directory: Path) ->return free_space_gb >= required_space_gb+async def get_threads_number(args: dict, domain: str) -> int:+ threads = args["Runtime"]["simultaneous_downloads_per_domain"] or multiprocessing.cpu_count()+ if any(s in domain for s in ('anonfiles', 'bunkr', 'pixeldrain')):+ return min(threads, 2)+ return threads++async def is_4xx_client_error(status_code: int) -> bool:"""Checks whether the HTTP status code is 4xx client error"""return HTTPStatus.BAD_REQUEST <= status_code < HTTPStatus.INTERNAL_SERVER_ERRORdiff --git a/cyberdrop_dl/downloader/downloaders.py b/cyberdrop_dl/downloader/downloaders.pyindex 7138b3957..ddb14b894 100644--- a/cyberdrop_dl/downloader/downloaders.py+++ b/cyberdrop_dl/downloader/downloaders.py@@ -3,7 +3,6 @@ from __future__ import annotationsimport asyncioimport itertoolsimport logging-import multiprocessingfrom http import HTTPStatusfrom random import gauss@@ -29,6 +28,7 @@ from .downloader_utils import (allowed_filetype,basic_auth,check_free_space,+ get_threads_number,is_4xx_client_error,retry,)@@ -337,8 +337,6 @@ class Downloader:async def download_cascade(args: dict, Cascade: CascadeItem, SQL_Helper: SQLHelper, client: Client,scraper: ScrapeMapper) -> None:"""Handler for cascades and the progress bars for it"""- user_threads = args["Runtime"]["simultaneous_downloads_per_domain"]-progress_table = await get_cascade_table(args["Progress_Options"])total_files = await Cascade.get_total()files = Files(overall_file_progress.add_task("[green]Completed", total=total_files),@@ -351,9 +349,7 @@ async def download_cascade(args: dict, Cascade: CascadeItem, SQL_Helper: SQLHelptasks = []for domain, domain_obj in Cascade.domains.items():- threads = user_threads if user_threads != 0 else multiprocessing.cpu_count()- if 'bunkr' in domain or 'pixeldrain' in domain or 'anonfiles' in domain:- threads = 2 if (threads > 2) else threads+ threads = await get_threads_number(args, domain)downloader = Downloader(args, client, SQL_Helper, scraper, threads, domain, domain_obj, files)tasks.append(downloader.start_domain(cascade_task))await asyncio.gather(*tasks)@@ -370,8 +366,6 @@ async def download_cascade(args: dict, Cascade: CascadeItem, SQL_Helper: SQLHelpasync def download_forums(args: dict, Forums: ForumItem, SQL_Helper: SQLHelper, client: Client,scraper: ScrapeMapper) -> None:"""Handler for forum threads and the progress bars for it"""- user_threads = args["Runtime"]["simultaneous_downloads_per_domain"]-progress_table = await get_forum_table(args["Progress_Options"])total_files = await Forums.get_total()files = Files(overall_file_progress.add_task("[green]Completed", total=total_files),@@ -385,9 +379,7 @@ async def download_forums(args: dict, Forums: ForumItem, SQL_Helper: SQLHelper,tasks = []for domain, domain_obj in Cascade.domains.items():- threads = user_threads if user_threads != 0 else multiprocessing.cpu_count()- if 'bunkr' in domain or 'pixeldrain' in domain or 'anonfiles' in domain:- threads = 2 if (threads > 2) else threads+ threads = await get_threads_number(args, domain)downloader = Downloader(args, client, SQL_Helper, scraper, threads, domain, domain_obj, files)tasks.append(downloader.start_domain(cascade_task))await asyncio.gather(*tasks)diff --git a/cyberdrop_dl/downloader/old_downloaders.py b/cyberdrop_dl/downloader/old_downloaders.pyindex 11ff37d7d..98cf35e0e 100644--- a/cyberdrop_dl/downloader/old_downloaders.py+++ b/cyberdrop_dl/downloader/old_downloaders.py@@ -3,7 +3,6 @@ from __future__ import annotationsimport asyncioimport itertoolsimport logging-import multiprocessingfrom http import HTTPStatusfrom random import gauss@@ -27,6 +26,7 @@ from .downloader_utils import (allowed_filetype,basic_auth,check_free_space,+ get_threads_number,is_4xx_client_error,retry,)@@ -284,8 +284,6 @@ class Old_Downloader:async def old_download_cascade(args: dict, Cascade: CascadeItem, SQL_Helper: SQLHelper, client: Client,scraper: ScrapeMapper) -> None:"""Handler for cascades and the progress bars for it"""- user_threads = args["Runtime"]["simultaneous_downloads_per_domain"]-files = Files()tasks = []@@ -293,9 +291,7 @@ async def old_download_cascade(args: dict, Cascade: CascadeItem, SQL_Helper: SQLwith tqdm(total=total_files, unit_scale=True, unit='Files', leave=True, initial=0,desc="Files Downloaded") as progress:for domain, domain_obj in Cascade.domains.items():- threads = user_threads if user_threads != 0 else multiprocessing.cpu_count()- if 'bunkr' in domain or 'pixeldrain' in domain or 'anonfiles' in domain:- threads = 2 if (threads > 2) else threads+ threads = await get_threads_number(args, domain)downloader = Old_Downloader(args, client, SQL_Helper, scraper, threads, domain, domain_obj,files, progress)tasks.append(downloader.start_domain())@@ -309,8 +305,6 @@ async def old_download_cascade(args: dict, Cascade: CascadeItem, SQL_Helper: SQLasync def old_download_forums(args: dict, Forums: ForumItem, SQL_Helper: SQLHelper, client: Client,scraper: ScrapeMapper) -> None:"""Handler for forum threads and the progress bars for it"""- user_threads = args["Runtime"]["simultaneous_downloads_per_domain"]-files = Files()total_files = await Forums.get_total()with tqdm(total=total_files, unit_scale=True, unit='Files', leave=True, initial=0,@@ -319,9 +313,7 @@ async def old_download_forums(args: dict, Forums: ForumItem, SQL_Helper: SQLHelptasks = []for domain, domain_obj in Cascade.domains.items():- threads = user_threads if user_threads != 0 else multiprocessing.cpu_count()- if 'bunkr' in domain or 'pixeldrain' in domain or 'anonfiles' in domain:- threads = 2 if (threads > 2) else threads+ threads = await get_threads_number(args, domain)downloader = Old_Downloader(args, client, SQL_Helper, scraper, threads, domain, domain_obj,files, progress)tasks.append(downloader.start_domain())