Skip to content

Bulk asynchronous downloader for multiple file hosts

Turn throttle into DownloadSession method (#439)

Arkadiy Illarionov00b5715e4b5b
diff --git a/cyberdrop_dl/client/client.py b/cyberdrop_dl/client/client.py
index 5cfe58ec7..b84b033ff 100644
--- a/cyberdrop_dl/client/client.py
+++ b/cyberdrop_dl/client/client.py
@@ -4,6 +4,7 @@ import asyncio
import json
import logging
import ssl
+import time
from http import HTTPStatus
from typing import TYPE_CHECKING, Callable
@@ -18,7 +19,7 @@ from ..base_functions.base_functions import adjust, logger
from ..base_functions.error_classes import DownloadFailure, InvalidContentTypeFailure
from ..downloader.downloader_utils import CustomHTTPStatus
from ..downloader.progress_definitions import file_progress
-from .rate_limiting import AsyncRateLimiter, throttle
+from .rate_limiting import AsyncRateLimiter
if TYPE_CHECKING:
from pathlib import Path
@@ -132,7 +133,7 @@ class DownloadSession:
save_content: Callable[[aiohttp.StreamReader, int], None]) -> None:
headers['Referer'] = str(media.referer)
headers['user-agent'] = self.client.user_agent
- await throttle(self, current_throttle, media.url.host)
+ await self._throttle(current_throttle, media.url.host)
async with self.client_session.get(media.url, headers=headers, ssl=self.client.ssl_context,
raise_for_status=True, proxy=proxy) as resp:
content_type = resp.headers.get('Content-Type')
@@ -147,6 +148,24 @@ class DownloadSession:
size = int(resp.headers.get('Content-Length', '0'))
await save_content(resp.content, size)
+ async def _throttle(self, delay: int, host: str) -> None:
+ """Throttles requests to domains by a parameter amount of time"""
+ if delay is None or delay == 0:
+ return
+
+ key = f'throttle:{host}'
+ while True:
+ now = time.time()
+ last = self.throttle_times.get(key, 0.0)
+ elapsed = now - last
+
+ if elapsed >= delay:
+ self.throttle_times[key] = now
+ return
+
+ remaining = delay - elapsed + 0.1
+ await asyncio.sleep(remaining)
+
async def download_file(self, media: MediaItem, file: Path, current_throttle: int, resume_point: int,
proxy: str, headers: dict, file_task: TaskID):
@@ -170,7 +189,7 @@ class DownloadSession:
async def get_filesize(self, url: URL, referer: str, current_throttle: int):
headers = {'Referer': referer, 'user-agent': self.client.user_agent}
- await throttle(self, current_throttle, url.host)
+ await self._throttle(current_throttle, url.host)
async with self.client_session.get(url, headers=headers, ssl=self.client.ssl_context,
raise_for_status=True) as resp:
return int(resp.headers.get('Content-Length', str(0)))
diff --git a/cyberdrop_dl/client/rate_limiting.py b/cyberdrop_dl/client/rate_limiting.py
index d2b66a5d9..511a9e2d2 100644
--- a/cyberdrop_dl/client/rate_limiting.py
+++ b/cyberdrop_dl/client/rate_limiting.py
@@ -2,7 +2,6 @@ from __future__ import annotations
import asyncio
import collections
-import time
from datetime import datetime
from typing import TYPE_CHECKING, Any, Awaitable, Callable, Optional, Type
@@ -63,22 +62,3 @@ class AsyncRateLimiter:
@property
def _timespan(self) -> float:
return self.calls[-1] - self.calls[0]
-
-
-async def throttle(self, delay: int, host: str) -> None:
- """Throttles requests to domains by a parameter amount of time"""
- if delay is None or delay == 0:
- return
-
- key = f'throttle:{host}'
- while True:
- now = time.time()
- last = self.throttle_times.get(key, 0.0)
- elapsed = now - last
-
- if elapsed >= delay:
- self.throttle_times[key] = now
- return
-
- remaining = delay - elapsed + 0.1
- await asyncio.sleep(remaining)