Add a 5-attempt retry system to DASH & HLS downloads

This commit is contained in:
rlaphoenix 2023-03-11 19:26:51 +00:00
parent 111dac9264
commit 055bc927f5
2 changed files with 59 additions and 41 deletions

View File

@ -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

View File

@ -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