Bulk asynchronous downloader for multiple file hosts
Python asynciobunkrcyberdropdownloaderdropboxfilestergofilegoogle-drivemediafiremegamega-nzonedrivepatreonpawchivepinterestpixeldraintransfer-ittwitchtwittervk
Make things runnable.
Jules-WinnfieldX11418398bddd
diff --git a/cyberdrop_dl/clients/download_client.py b/cyberdrop_dl/clients/download_client.pyindex 2dcb642c9..dc9e01861 100644--- a/cyberdrop_dl/clients/download_client.py+++ b/cyberdrop_dl/clients/download_client.py@@ -1,20 +1,26 @@from __future__ import annotations+import asyncioimport copy+import functoolsfrom enum import IntEnumfrom http import HTTPStatusfrom typing import TYPE_CHECKING+import aiofilesimport aiohttpfrom aiolimiter import AsyncLimiterfrom yarl import URLfrom cyberdrop_dl.clients.errors import DownloadFailure+from cyberdrop_dl.utils.utilities import FILE_FORMATSif TYPE_CHECKING:- from typing import Dict+ from pathlib import Path+ from typing import Dict, Callable, Coroutine, Anyfrom cyberdrop_dl.managers.client_manager import ClientManager+ from cyberdrop_dl.managers.manager import Managerfrom cyberdrop_dl.utils.dataclasses.url_objects import MediaItem@@ -37,7 +43,7 @@ class DownloadClient:connect=client_manager.connection_timeout)self.client_session = aiohttp.ClientSession(headers=self.headers, raise_for_status=True,cookie_jar=client_manager.cookies, timeout=self.timeouts)- self.throttle_times: Dict[str, float] = {}+ self.throttle_times: Dict[str, float] = {"user_agent": client_manager.user_agent}self.bunkr_maintenance = [URL("https://bnkr.b-cdn.net/maintenance-vid.mp4"),URL("https://bnkr.b-cdn.net/maintenance.mp4")]@@ -55,3 +61,50 @@ class DownloadClient:message="DDoS-Guard detected, unable to download")raise DownloadFailure(status=resp.status, message=f"Unexpected status code {resp.status} from {media_item.url}")return int(resp.headers.get('Content-Length', '0'))++ """~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"""++ async def _download(self, media_item: MediaItem, headers_inc: Dict,+ save_content: Callable[[aiohttp.StreamReader], Coroutine[Any, Any, None]], file: Path) -> None:+ headers = copy.deepcopy(self.headers)+ headers['Referer'] = media_item.referer+ headers.update(headers_inc)++ async with self.client_session.get(media_item.url, headers=headers, ssl=self.client_manager.ssl_context,+ raise_for_status=True, proxy=self.client_manager.proxy) as resp:+ content_type = resp.headers.get('Content-Type')+ if not content_type:+ raise DownloadFailure(status=CustomHTTPStatus.IM_A_TEAPOT, message="No content-type in response header")+ if resp.url in self.bunkr_maintenance:+ raise DownloadFailure(status=HTTPStatus.SERVICE_UNAVAILABLE, message="Bunkr under maintenance")+ if "imgur.com/removed" in str(resp.url):+ raise DownloadFailure(status=HTTPStatus.NOT_FOUND, message="Imgur image has been removed")++ ext = Path(media_item.filename).suffix.lower()+ if any(s in content_type.lower() for s in ('html', 'text')) and ext not in FILE_FORMATS['Text']:+ raise DownloadFailure(status=CustomHTTPStatus.IM_A_TEAPOT, message="Unexpectedly got text as response")++ if resp.status != HTTPStatus.PARTIAL_CONTENT:+ if file.is_file():+ file.unlink()++ await save_content(resp.content)++ async def _append_content(self, file: Path, content: aiohttp.StreamReader, update_progress: functools.partial) -> None:+ file.parent.mkdir(parents=True, exist_ok=True)+ async with aiofiles.open(file, mode='ab') as f:+ async for chunk, _ in content.iter_chunks():+ await asyncio.sleep(0)+ await f.write(chunk)+ if isinstance(update_progress, functools.partial):+ await update_progress(len(chunk))+ else:+ update_progress(len(chunk))++ async def download_file(self, manager: Manager, media_item: MediaItem, partial_file: Path, headers: Dict) -> None:++ async def save_content(content: aiohttp.StreamReader) -> None:+ await self._append_content(partial_file, content, functools.partial())++ await self._download(media_item, headers, save_content, partial_file)+diff --git a/cyberdrop_dl/downloader/downloader.py b/cyberdrop_dl/downloader/downloader.pyindex 57e7a7f2d..f120f1786 100644--- a/cyberdrop_dl/downloader/downloader.py+++ b/cyberdrop_dl/downloader/downloader.py@@ -236,7 +236,7 @@ class Downloader:resume_point = partial_file.stat().st_size if partial_file.exists() else 0headers = {'Range': f'bytes={resume_point}-'}- # await self.client.download_file(self.manager, media_item, complete_file, partial_file, headers)+ await self.client.download_file(self.manager, media_item, complete_file, partial_file, headers)partial_file.rename(complete_file)await self.mark_completed(media_item)diff --git a/cyberdrop_dl/main.py b/cyberdrop_dl/main.pyindex 44527ce59..59e715fe4 100644--- a/cyberdrop_dl/main.py+++ b/cyberdrop_dl/main.py@@ -29,7 +29,7 @@ def startup():level=logging.DEBUG,format="%(asctime)s:%(levelname)s:%(module)s:%(filename)s:%(lineno)d:%(message)s",filemode="w",- handlers=[RichHandler(rich_tracebacks=True)]+ # handlers=[RichHandler(rich_tracebacks=True)])return managerdiff --git a/cyberdrop_dl/managers/client_manager.py b/cyberdrop_dl/managers/client_manager.pyindex 617f81321..2825983da 100644--- a/cyberdrop_dl/managers/client_manager.py+++ b/cyberdrop_dl/managers/client_manager.py@@ -25,6 +25,7 @@ class ClientManager:self.verify_ssl = not manager.config_manager.global_settings_data['General']['allow_insecure_connections']self.ssl_context = ssl.create_default_context(cafile=certifi.where()) if self.verify_ssl else Falseself.cookies = aiohttp.CookieJar(quote_cookie=False)+ self.proxy = manager.config_manager.global_settings_data['General']['proxy']self.scraper_sessions = {}self.downloader_sessions = {}diff --git a/cyberdrop_dl/managers/progress_manager.py b/cyberdrop_dl/managers/progress_manager.pynew file mode 100644index 000000000..e69de29bbdiff --git a/cyberdrop_dl/ui/progress/progress_definitions.py b/cyberdrop_dl/ui/progress/progress_definitions.pyindex e69de29bb..985afc9c8 100644--- a/cyberdrop_dl/ui/progress/progress_definitions.py+++ b/cyberdrop_dl/ui/progress/progress_definitions.py@@ -0,0 +1,270 @@+from __future__ import annotations++import contextlib+from typing import Dict, List, Optional, Tuple++from rich.console import Group+from rich.panel import Panel+from rich.progress import (+ BarColumn,+ DownloadColumn,+ Progress,+ SpinnerColumn,+ TaskID,+ TimeRemainingColumn,+ TransferSpeedColumn,+)+from rich.table import Table+++def adjust_title(s: str, length: int = 40, placeholder: str = "...") -> str:+ """Collapse and truncate or pad the given string to fit in the given length"""+ return f"{s[:length - len(placeholder)]}{placeholder}" if len(s) >= length else s.ljust(length)+++class Progressions:+ def __init__(self):+ self.overall_file_progress = Progress("[progress.description]{task.description}",+ BarColumn(bar_width=None),+ "[progress.percentage]{task.percentage:>3.2f}%",+ "{task.completed} of {task.total} Files Completed")++ self.forum_progress = Progress("[progress.description]{task.description}",+ BarColumn(bar_width=None),+ "[progress.percentage]{task.percentage:>3.2f}%",+ "{task.completed} of {task.total} Threads Completed")+ self.forum_progress_overflow = Progress("[progress.description]{task.description}")++ self.cascade_progress = Progress("[progress.description]{task.description}",+ BarColumn(bar_width=None),+ "[progress.percentage]{task.percentage:>3.2f}%",+ "{task.completed} of {task.total} Domains Completed")+ self.cascade_progress_overflow = Progress("[progress.description]{task.description}")++ self.domain_progress = Progress("[progress.description]{task.description}",+ BarColumn(bar_width=None),+ "[progress.percentage]{task.percentage:>3.2f}%",+ "{task.completed} of {task.total} Albums Completed")+ self.domain_progress_overflow = Progress("[progress.description]{task.description}")++ self.album_progress = Progress("[progress.description]{task.description}",+ BarColumn(bar_width=None),+ "[progress.percentage]{task.percentage:>3.2f}%",+ "{task.completed} of {task.total} Files Completed")+ self.album_progress_overflow = Progress("[progress.description]{task.description}")++ self.file_progress = Progress(SpinnerColumn(),+ "[progress.description]{task.description}",+ BarColumn(bar_width=None),+ "[progress.percentage]{task.percentage:>3.2f}%",+ "━",+ DownloadColumn(),+ "━",+ TransferSpeedColumn(),+ "━",+ TimeRemainingColumn())+ self.file_progress_overflow = Progress("[progress.description]{task.description}")++ async def get_overall_progress(self) -> Panel:+ return Panel(Group(self.overall_file_progress), title="Overall Progress", border_style="green", padding=(1, 1))++ async def get_forum_progress(self) -> Panel:+ return Panel(Group(self.forum_progress), title="Total Threads", border_style="green",padding=(1, 1))++ async def get_cascade_progress(self) -> Panel:+ return Panel(Group(self.cascade_progress, self.cascade_progress_overflow), title="Current Threads", border_style="green", padding=(1, 1))++ async def get_domain_progress(self) -> Panel:+ return Panel(Group(self.domain_progress, self.domain_progress_overflow), title="Current Domains", border_style="green", padding=(1, 1))++ async def get_album_progress(self) -> Panel:+ return Panel(Group(self.album_progress, self.album_progress_overflow), title="Current Albums", border_style="green", padding=(1, 1))++ async def get_file_progress(self) -> Panel:+ return Panel(Group(self.file_progress, self.file_progress_overflow), title="Current Downloads", border_style="green", padding=(1, 1))+++class OverallFileProgress:+ """Class that keeps track of completed, skipped and failed files"""++ def __init__(self, total_files: int, overall_file_progress: Progress):+ self.overall_file_progress = overall_file_progress++ self.completed_files_task_id = overall_file_progress.add_task("[green]Completed", total=total_files)+ self.completed_files = 0+ self.skipped_files_task_id = overall_file_progress.add_task("[yellow]Skipped", total=total_files)+ self.skipped_files = 0+ self.failed_files_task_id = overall_file_progress.add_task("[red]Failed", total=total_files)+ self.failed_files = 0++ async def update_total(self, total_files: int) -> None:+ self.overall_file_progress.update(self.completed_files_task_id, total=total_files)+ self.overall_file_progress.update(self.skipped_files_task_id, total=total_files)+ self.overall_file_progress.update(self.failed_files_task_id, total=total_files)++ async def add_completed(self) -> None:+ self.overall_file_progress.advance(self.completed_files_task_id, 1)+ self.completed_files += 1++ async def add_skipped(self) -> None:+ self.overall_file_progress.advance(self.skipped_files_task_id, 1)+ self.skipped_files += 1++ async def add_failed(self) -> None:+ self.overall_file_progress.advance(self.failed_files_task_id, 1)+ self.failed_files += 1++ async def return_totals(self) -> Tuple[int, int, int]:+ return self.completed_files, self.skipped_files, self.failed_files++ async def hide(self) -> None:+ self.overall_file_progress.update(self.completed_files_task_id, visible=False)+ self.overall_file_progress.update(self.skipped_files_task_id, visible=False)+ self.overall_file_progress.update(self.failed_files_task_id, visible=False)+++class _Progress:+ def __init__(self, progress: Progress, overflow: Progress, color: str, type_str: str, visible_tasks_limit: int):+ self.color = color+ self.type_str = type_str++ self.progress = progress+ self.progress_str = "[{color}]{description}"+ self.overflow = overflow+ self.overflow_str = "[{color}]... And {number} Other {type_str}"+ self.overflow_task_id = self.overflow.add_task(self.overflow_str.format(color=self.color, number=0, type_str=self.type_str), visible=False)++ self.visible_tasks: List[TaskID] = []+ self.invisible_tasks: List[TaskID] = []+ self.completed_tasks: List[TaskID] = []+ self.uninitiated_tasks: List[TaskID] = []+ self.tasks_visibility_limit = visible_tasks_limit+++class DomainProgress:+ def __init__(self, progressions: Progressions, visible_tasks_limit: int):+ self.domain_progress = progressions.domain_progress+ self.color = "light_pink3"+ self.type_str = "Domains"+ self.progress = _Progress(self.domain_progress, progressions.domain_progress_overflow, self.color, "Domains", visible_tasks_limit)++ self.domains: Dict[str, TaskID] = {}+ self.domain_totals: Dict[str, int] = {}++ async def add_domain(self, domain: str, total_albums: int) -> TaskID:+ if domain in self.domains:+ self.domain_totals[domain] += total_albums+ await self.progress.update_total(self.domains[domain], self.domain_totals[domain])+ else:+ self.domains[domain] = await self.progress.add_task(domain.upper(), total_albums, True)+ self.domain_totals[domain] = total_albums++ await self.progress.redraw()+ return self.domains[domain]++ async def advance_domain(self, task_id: TaskID) -> None:+ await self.progress.advance_task(task_id, 1)++ async def mark_domain_completed(self, domain: str, task_id: TaskID) -> None:+ task = [x for x in self.domain_progress.tasks if x.id == task_id][0]+ if task.finished:+ with contextlib.suppress(KeyError):+ self.domains.pop(domain)+ self.domain_totals.pop(domain)+ await self.progress.mark_task_completed(task_id)+++class AlbumProgress:+ def __init__(self, progressions: Progressions, visible_tasks_limit: int):+ self.album_progress = progressions.album_progress+ self.color = "pink3"+ self.type_str = "Albums"+ self.progress = _Progress(self.album_progress, progressions.album_progress_overflow, self.color, "Albums", visible_tasks_limit)++ self.albums: Dict[str, TaskID] = {}+ self.albums_totals: Dict[str, int] = {}++ async def add_album(self, album: str, total_files: int) -> TaskID:+ task_description = album.split('/')[-1]+ task_description = task_description.encode("ascii", "ignore").decode().strip()+ task_description = adjust_title(task_description).upper().strip()++ if task_description in self.albums:+ self.albums_totals[task_description] += total_files+ await self.progress.update_total(self.albums[task_description], self.albums_totals[task_description])+ else:+ self.albums[task_description] = await self.progress.add_task(task_description, total_files, True)+ self.albums_totals[task_description] = total_files++ await self.progress.redraw()+ return self.albums[task_description]++ async def advance_album(self, task_id: TaskID) -> None:+ await self.progress.advance_task(task_id, 1)++ async def mark_album_completed(self, album: str, task_id: TaskID) -> None:+ task_description = album.split('/')[-1]+ task_description = task_description.encode("ascii", "ignore").decode().strip()+ task_description = adjust_title(task_description).upper()++ task = [x for x in self.album_progress.tasks if x.id == task_id][0]+ if task.finished:+ with contextlib.suppress(KeyError):+ self.albums.pop(task_description)+ self.albums_totals.pop(task_description)+ await self.progress.mark_task_completed(task_id)+++class FileProgress:+ def __init__(self, progressions: Progressions, visible_tasks_limit: int):+ self.file_progress = progressions.file_progress+ self.color = "plum3"+ self.type_str = "Files"+ self.progress = _Progress(self.file_progress, progressions.file_progress_overflow, self.color, "Files", visible_tasks_limit)++ async def add_file(self, file: str, expected_size: Optional[int]) -> TaskID:+ task_description = file.split('/')[-1]+ task_description = task_description.encode("ascii", "ignore").decode().strip()+ task_description = adjust_title(task_description)++ task_id = await self.progress.add_task(task_description.upper(), expected_size if expected_size else 0, False)+ await self.progress.redraw()+ return task_id++ async def update_file_length(self, task_id: TaskID, length: int) -> None:+ await self.progress.update_total(task_id, length)++ async def advance_file(self, task_id: TaskID, increment: int) -> None:+ await self.progress.advance_task(task_id, increment)++ async def remove_file(self, task_id: TaskID) -> None:+ await self.progress.remove_task(task_id)+ await self.progress.redraw()++ async def mark_file_completed(self, task_id: TaskID) -> None:+ await self.progress.mark_task_completed(task_id)+ await self.progress.redraw()+++class ProgressMaster:+ def __init__(self, progress_options: Dict):+ self.hide_scrape_progress = progress_options['hide_scrape_progress']+ self.hide_error_progress = progress_options['hide_error_progress']+ self.hide_file_progress = progress_options['hide_file_progress']+ self.refresh_rate = progress_options['refresh_rate']++ self.Progressions = Progressions()+ self.ScrapeProgress = ScrapeProgress(self.Progressions, progress_options['visible_rows_scrape'])+ self.ErrorProgress = ErrorProgress(self.Progressions, progress_options['visible_rows_error'])+ self.FileProgress = FileProgress(self.Progressions, progress_options['visible_rows_files'])++ async def get_table(self) -> Table:+ """Table creator for forum threads"""+ progress_table = Table.grid(expand=True)+ if not self.hide_scrape_progress:+ progress_table.add_row(await self.Progressions.get_scrape_progress())+ if not self.hide_error_progress:+ progress_table.add_row(await self.Progressions.get_error_progress())+ if not self.hide_file_progress:+ progress_table.add_row(await self.Progressions.get_file_progress())+ return progress_tablediff --git a/cyberdrop_dl/utils/args/args.py b/cyberdrop_dl/utils/args/args.pyindex 2326f1e5a..2ca06b185 100644--- a/cyberdrop_dl/utils/args/args.py+++ b/cyberdrop_dl/utils/args/args.py@@ -1,8 +1,6 @@import argparse-from pathlib import Pathfrom cyberdrop_dl import __version__ as VERSION-from cyberdrop_dl.managers.directory_manager import APP_STORAGE, DOWNLOAD_STORAGEfrom cyberdrop_dl.utils.dataclasses.supported_domains import SupportedDomainsdiff --git a/cyberdrop_dl/utils/db/tables/history_table.py b/cyberdrop_dl/utils/db/tables/history_table.pyindex 3cbf4f9a5..820413197 100644--- a/cyberdrop_dl/utils/db/tables/history_table.py+++ b/cyberdrop_dl/utils/db/tables/history_table.py@@ -1,3 +1,5 @@+from __future__ import annotations+import aiosqlitefrom typing import TYPE_CHECKINGfrom yarl import URLdiff --git a/cyberdrop_dl/utils/utilities.py b/cyberdrop_dl/utils/utilities.pyindex aaea1618b..b7c83ec3b 100644--- a/cyberdrop_dl/utils/utilities.py+++ b/cyberdrop_dl/utils/utilities.py@@ -1,3 +1,5 @@+from __future__ import annotations+import loggingimport refrom typing import TYPE_CHECKINGdiff --git a/pyproject.toml b/pyproject.tomlindex 51d48f6d8..715fe700d 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -6,7 +6,7 @@ authors = ["Jules Winnfield <[email protected]>"]readme = "README.md"repository = "https://github.com/Jules-WinnfieldX/CyberDropDownloader"documentation = ""-packages = [{include = "cyberdrop-dl"}]+packages = [{include = "cyberdrop_dl"}][tool.poetry.dependencies]python = "^3.8"@@ -24,7 +24,7 @@ aiorun = "^2023.7.2"beautifulsoup4 = "^4.12.2"[tool.poetry.scripts]-cyberdrop-dl = "cyberdrop-dl.main:main"+cyberdrop-dl = "cyberdrop_dl.main:main"[build-system]requires = ["poetry-core>=1.0.0"]