Skip to content

Bulk asynchronous downloader for multiple file hosts

Broken Update as marker

Jules-WinnfieldX7cd410054f9d
diff --git a/main.py b/main.py
index b1bd3d210..dcde19c4e 100644
--- a/main.py
+++ b/main.py
@@ -11,7 +11,7 @@ import nest_asyncio
# Fixes reactor already installed error
try:
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
-except:
+except Exception:
pass
from pathlib import Path
diff --git a/utils/downloaders.py b/utils/downloaders.py
index 1b7dd8175..a1f1e9899 100644
--- a/utils/downloaders.py
+++ b/utils/downloaders.py
@@ -1,5 +1,6 @@
import asyncio
import http
+import random
import time
import traceback
import typing
@@ -83,11 +84,21 @@ def retry(
return inner
+async def jitter(self, url: yarl.URL):
+ host = url.host
+ if host is None:
+ return None
+ return self.delay.get(host) + (random.random() - 0.5) * 2 * 1.1
+
+
async def throttle(self, url: yarl.URL) -> None:
host = url.host
if host is None:
return
- delay = self.delay.get(host)
+ try:
+ delay = await jitter(self, url)
+ except Exception:
+ return
if delay is None:
return
@@ -97,13 +108,11 @@ async def throttle(self, url: yarl.URL) -> None:
key = 'throttle:{}'.format(host)
now = time.time()
- last = self.state.get(key)
- if not isinstance(last, float):
- last = 0.0
+ last = self.throttle_times.get(key, 0.0)
elapsed = now - last
if elapsed >= delay:
- self.state[key] = now
+ self.throttle_times[key] = now
return
remaining = delay - elapsed
@@ -122,7 +131,7 @@ class Downloader:
self.max_workers = max_workers
self._semaphore = asyncio.Semaphore(max_workers)
self.delay = {'media-files.bunkr.is': 2}
- self.state = {}
+ self.throttle_times = {}
"""Changed from aiohttp exceptions caught to FailureException to allow for partial downloads."""
@retry(attempts=settings.download_attempts, timeout=4, exceptions=FailureException)