From 6cdde3efb093c90dd94b2fcfba1bc8c1497eccde Mon Sep 17 00:00:00 2001 From: rlaphoenix Date: Wed, 17 May 2023 01:31:03 +0100 Subject: [PATCH] Override the downloader more efficiently in DASH/HLS when Range is used --- devine/core/manifests/dash.py | 32 +++++++++++++------------------- devine/core/manifests/hls.py | 34 +++++++++++++--------------------- 2 files changed, 26 insertions(+), 40 deletions(-) diff --git a/devine/core/manifests/dash.py b/devine/core/manifests/dash.py index 7e3031a..4f409cd 100644 --- a/devine/core/manifests/dash.py +++ b/devine/core/manifests/dash.py @@ -26,7 +26,7 @@ from requests import Session from rich import filesize from devine.core.constants import AnyTrack -from devine.core.downloaders import downloader +from devine.core.downloaders import downloader, requests as requests_downloader from devine.core.drm import Widevine from devine.core.tracks import Audio, Subtitle, Tracks, Video from devine.core.utilities import is_close_match @@ -416,26 +416,20 @@ class DASH: attempts = 1 while True: try: + downloader_ = downloader + headers_ = session.headers if segment_range: # aria2(c) doesn't support byte ranges, let's use python-requests (likely slower) - res = session.get( - url=segment_uri, - headers={ - "Range": f"bytes={segment_range}" - } - ) - res.raise_for_status() - segment_save_path.parent.mkdir(parents=True, exist_ok=True) - segment_save_path.write_bytes(res.content) - else: - downloader( - uri=segment_uri, - out=segment_save_path, - headers=session.headers, - proxy=proxy, - silent=attempts != 5, - segmented=True - ) + downloader_ = requests_downloader + headers_["Range"] = f"bytes={segment_range}" + downloader_( + uri=segment_uri, + out=segment_save_path, + headers=headers_, + proxy=proxy, + silent=attempts != 5, + segmented=True + ) break except Exception as ee: if stop_event.is_set() or attempts == 5: diff --git a/devine/core/manifests/hls.py b/devine/core/manifests/hls.py index cb420f9..4147b8b 100644 --- a/devine/core/manifests/hls.py +++ b/devine/core/manifests/hls.py @@ -23,7 +23,7 @@ from requests import Session from rich import filesize from devine.core.constants import AnyTrack -from devine.core.downloaders import downloader +from devine.core.downloaders import downloader, requests as requests_downloader from devine.core.drm import DRM_T, ClearKey, Widevine from devine.core.tracks import Audio, Subtitle, Tracks, Video from devine.core.utilities import is_close_match @@ -290,31 +290,23 @@ class HLS: attempts = 1 while True: try: + downloader_ = downloader + headers_ = session.headers if segment.byterange: # aria2(c) doesn't support byte ranges, let's use python-requests (likely slower) previous_range_offset = range_offset.get() byte_range = HLS.calculate_byte_range(segment.byterange, previous_range_offset) range_offset.put(byte_range.split("-")[0]) - - res = session.get( - url=segment.uri, - headers={ - "Range": f"bytes={byte_range}" - } - ) - res.raise_for_status() - - segment_save_path.parent.mkdir(parents=True, exist_ok=True) - segment_save_path.write_bytes(res.content) - else: - downloader( - uri=segment.uri, - out=segment_save_path, - headers=session.headers, - proxy=proxy, - silent=attempts != 5, - segmented=True - ) + downloader_ = requests_downloader + headers_["Range"] = f"bytes={byte_range}" + downloader_( + uri=segment.uri, + out=segment_save_path, + headers=headers_, + proxy=proxy, + silent=attempts != 5, + segmented=True + ) break except Exception as ee: if stop_event.is_set() or attempts == 5: