summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Honz <exislow+github@gmail.com>2024-11-13 12:36:01 +0100
committerRobert Honz <exislow+github@gmail.com>2024-11-13 12:36:01 +0100
commitf5ed1c6554dbe5e7dda8ce662ac7ee49919cece3 (patch)
treed549a9f69c2349421a7f17155ee8638946fabbe7
parentae2dd4fb2b4738cf73d2a0d1b0d94e7410b5ca65 (diff)
downloadtidal-dl-ng-f5ed1c6554dbe5e7dda8ce662ac7ee49919cece3.tar.gz
✨ Time exponential retries on segment download errors. Fixes #196
-rw-r--r--tidal_dl_ng/download.py12
1 files changed, 10 insertions, 2 deletions
diff --git a/tidal_dl_ng/download.py b/tidal_dl_ng/download.py
index 6c637f9..6f039a8 100644
--- a/tidal_dl_ng/download.py
+++ b/tidal_dl_ng/download.py
@@ -12,6 +12,7 @@ import ffmpeg
import m3u8
import requests
from constants import CHUNK_SIZE, COVER_NAME
+from requests.adapters import HTTPAdapter, Retry
from requests.exceptions import HTTPError
from rich.progress import Progress, TaskID
from tidalapi import Album, Mix, Playlist, Session, Track, UserPlaylist, Video
@@ -164,6 +165,7 @@ class Download:
# If this is NOT the case, but any other URL has resulted in an error,
# mark the whole thing as corrupt.
result_segments = False
+ self.fn_logger.error(f"Something went wrong while downloading {media_name}. File is corrupt!")
tmp_path_file_decrypted: pathlib.Path = path_file
@@ -211,9 +213,15 @@ class Download:
id_segment: int = int(str(path_segment.stem).split("_")[-1])
error: HTTPError | None = None
+ # Retry download on failed segments, with an exponential delay between retries
+ s = requests.Session()
+ retries = Retry(total=5, backoff_factor=1) # , status_forcelist=[ 502, 503, 504 ])
+
+ s.mount("https://", HTTPAdapter(max_retries=retries))
+
try:
# Create the request object with stream=True, so the content won't be loaded into memory at once.
- r = requests.get(url, stream=True, timeout=REQUESTS_TIMEOUT_SEC)
+ r = s.get(url, stream=True, timeout=REQUESTS_TIMEOUT_SEC)
r.raise_for_status()
@@ -225,7 +233,7 @@ class Download:
self.progress.advance(p_task)
result = True
- except HTTPError as e:
+ except Exception as e:
error = e
self.progress.advance(p_task)
self.fn_logger.exception(e)