Skip to content

Bulk asynchronous downloader for multiple file hosts

update venv in gitignore and add download rate limit

johnsmih7af6d61b3f45
diff --git a/.gitignore b/.gitignore
index 1866efcbe..9cde41fc2 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,3 +6,6 @@
# Python cache
__pycache__
+#venv
+.venv
+venv
diff --git a/.vscode/launch.json b/.vscode/launch.json
new file mode 100644
index 000000000..b1fa522a6
--- /dev/null
+++ b/.vscode/launch.json
@@ -0,0 +1,19 @@
+{
+ // Use IntelliSense to learn about possible attributes.
+ // Hover to view descriptions of existing attributes.
+ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
+ "version": "0.2.0",
+ "configurations": [
+ {
+ "name": "cyberdropdl",
+ "type": "python",
+ "request": "launch",
+ "program": "/home/john/src/CyberDropDownloader/.venv/bin/cyberdrop-dl",
+ "args": ["--ignore-history","--download","https://coomer.su/onlyfans/user/bigtittygothegg"],
+ "console": "integratedTerminal",
+ "justMyCode": false,
+ "python":"/home/john/src/CyberDropDownloader/.venv/bin/python3.11",
+
+ },
+ ]
+}
\ No newline at end of file
diff --git a/cyberdrop_dl/clients/download_client.py b/cyberdrop_dl/clients/download_client.py
index 19356d9bd..36fe7bab5 100644
--- a/cyberdrop_dl/clients/download_client.py
+++ b/cyberdrop_dl/clients/download_client.py
@@ -142,7 +142,8 @@ class DownloadClient:
if not media_item.partial_file.is_file():
media_item.partial_file.touch()
async with aiofiles.open(media_item.partial_file, mode='ab') as f:
- async for chunk, _ in content.iter_chunks():
+ async for chunk in content.iter_chunked(1024):
+ await self.client_manager.check_bucket(chunk)
await asyncio.sleep(0)
await f.write(chunk)
await update_progress(len(chunk))
diff --git a/cyberdrop_dl/managers/client_manager.py b/cyberdrop_dl/managers/client_manager.py
index e440c03f4..c1be5e4a7 100644
--- a/cyberdrop_dl/managers/client_manager.py
+++ b/cyberdrop_dl/managers/client_manager.py
@@ -14,6 +14,7 @@ from cyberdrop_dl.clients.download_client import DownloadClient
from cyberdrop_dl.clients.errors import DownloadFailure, DDOSGuardFailure, ScrapeFailure
from cyberdrop_dl.clients.scraper_client import ScraperClient
from cyberdrop_dl.utils.utilities import CustomHTTPStatus
+from cyberdrop_dl.managers.token_bucket_manager import TokenBucket
if TYPE_CHECKING:
from cyberdrop_dl.managers.manager import Manager
@@ -54,6 +55,7 @@ class ClientManager:
self.scraper_session = ScraperClient(self)
self.downloader_session = DownloadClient(manager, self)
+ self._token_bucket=TokenBucket(32*1024,32*1024)
"""~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"""
@@ -116,3 +118,8 @@ class ClientManager:
raise DownloadFailure(status=CustomHTTPStatus.IM_A_TEAPOT, message="No content-type in response header")
raise DownloadFailure(status=status, message=f"HTTP status code {status}: {phrase}")
+
+ async def check_bucket(self,size):
+ if not isinstance(size, int):
+ size=len(size)
+ await self._token_bucket.consume(size)
diff --git a/cyberdrop_dl/managers/token_bucket_manager.py b/cyberdrop_dl/managers/token_bucket_manager.py
new file mode 100644
index 000000000..e23357233
--- /dev/null
+++ b/cyberdrop_dl/managers/token_bucket_manager.py
@@ -0,0 +1,26 @@
+import time
+import asyncio
+class TokenBucket:
+ def __init__(self, capacity, fill_rate):
+ self.capacity = capacity
+ self.fill_rate = fill_rate
+ self.tokens = 0
+ self.last_update = time.time()
+ self._lock=asyncio.Lock()
+
+ async def consume(self, tokens):
+ if self.capacity<=0:
+ return True
+ while True:
+ async with self._lock:
+ now = time.time()
+ delta = now - self.last_update
+ self.last_update = now
+ self.tokens = min(self.capacity, self.tokens + delta * self.fill_rate)
+
+ if self.tokens >= tokens:
+ self.tokens -= tokens
+ return True
+
+ # Not enough tokens, wait for refill
+ await asyncio.sleep(0.01)
\ No newline at end of file