diff --git a/devine/core/manifests/dash.py b/devine/core/manifests/dash.py index 35bf69d..fe8d1de 100644 --- a/devine/core/manifests/dash.py +++ b/devine/core/manifests/dash.py @@ -455,25 +455,34 @@ class DASH: segment_uri, segment_range = segment - if segment_range: - # aria2(c) doesn't support byte ranges, let's use python-requests (likely slower) - r = session.get( - url=segment_uri, - headers={ - "Range": f"bytes={segment_range}" - } - ) - r.raise_for_status() - segment_save_path.parent.mkdir(parents=True, exist_ok=True) - segment_save_path.write_bytes(res.content) - else: - asyncio.run(aria2c( - uri=segment_uri, - out=segment_save_path, - headers=session.headers, - proxy=proxy, - segmented=True - )) + attempts = 1 + while True: + try: + if segment_range: + # aria2(c) doesn't support byte ranges, let's use python-requests (likely slower) + r = session.get( + url=segment_uri, + headers={ + "Range": f"bytes={segment_range}" + } + ) + r.raise_for_status() + segment_save_path.parent.mkdir(parents=True, exist_ok=True) + segment_save_path.write_bytes(res.content) + else: + asyncio.run(aria2c( + uri=segment_uri, + out=segment_save_path, + headers=session.headers, + proxy=proxy, + segmented=True + )) + break + except Exception as ee: + if stop_event.is_set() or attempts == 5: + raise ee + time.sleep(2) + attempts += 1 data_size = segment_save_path.stat().st_size diff --git a/devine/core/manifests/hls.py b/devine/core/manifests/hls.py index ba1a572..79948f2 100644 --- a/devine/core/manifests/hls.py +++ b/devine/core/manifests/hls.py @@ -283,30 +283,39 @@ class HLS: if not segment.uri.startswith(segment.base_uri): segment.uri = segment.base_uri + segment.uri - 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]) + attempts = 1 + while True: + try: + 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() + 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: - asyncio.run(aria2c( - uri=segment.uri, - out=segment_save_path, - headers=session.headers, - proxy=proxy, - segmented=True - )) + segment_save_path.parent.mkdir(parents=True, exist_ok=True) + segment_save_path.write_bytes(res.content) + else: + asyncio.run(aria2c( + uri=segment.uri, + out=segment_save_path, + headers=session.headers, + proxy=proxy, + segmented=True + )) + break + except Exception as ee: + if stop_event.is_set() or attempts == 5: + raise ee + time.sleep(2) + attempts += 1 data_size = segment_save_path.stat().st_size