Properly catch connection error and retry (maybe)

This commit is contained in:
Puyodead1 2021-05-26 23:39:09 -04:00
parent b667420dc2
commit b7b27419fd

74
main.py
View File

@ -681,43 +681,53 @@ def download_media(filename, url, lecture_working_dir, epoch=0):
if (os.path.isfile(filename)): if (os.path.isfile(filename)):
print("Segment already downloaded.. skipping..") print("Segment already downloaded.. skipping..")
else: else:
media = requests.get(url, stream=True) try:
media_length = int(media.headers.get("content-length")) media = requests.get(url, stream=True)
if media.status_code == 200: media_length = int(media.headers.get("content-length"))
if (os.path.isfile(filename) if media.status_code == 200:
and os.path.getsize(filename) >= media_length): if (os.path.isfile(filename)
print("Segment already downloaded.. skipping write to disk..") and os.path.getsize(filename) >= media_length):
else:
try:
pbar = tqdm(total=media_length,
initial=0,
unit='B',
unit_scale=True,
desc=filename)
with open(os.path.join(lecture_working_dir, filename),
'wb') as video_file:
for chunk in media.iter_content(chunk_size=1024):
if chunk:
video_file.write(chunk)
pbar.update(1024)
pbar.close()
print("Segment downloaded: " + filename)
return False #Successfully downloaded the file
except:
print( print(
"Connection error: Reattempting download of segment..") "Segment already downloaded.. skipping write to disk.."
)
else:
try:
pbar = tqdm(total=media_length,
initial=0,
unit='B',
unit_scale=True,
desc=filename)
with open(os.path.join(lecture_working_dir, filename),
'wb') as video_file:
for chunk in media.iter_content(chunk_size=1024):
if chunk:
video_file.write(chunk)
pbar.update(1024)
pbar.close()
print("Segment downloaded: " + filename)
return False #Successfully downloaded the file
except:
print(
"Connection error: Reattempting download of segment.."
)
download_media(filename, url, lecture_working_dir,
epoch + 1)
if os.path.getsize(filename) >= media_length:
pass
else:
print("Segment is faulty.. Redownloading...")
download_media(filename, url, lecture_working_dir, download_media(filename, url, lecture_working_dir,
epoch + 1) epoch + 1)
elif (media.status_code == 404):
if os.path.getsize(filename) >= media_length: print("Probably end hit!\n", url)
pass return True #Probably hit the last of the file
else: else:
print("Segment is faulty.. Redownloading...") if (epoch > retry):
exit("Error fetching segment, exceeded retry times.")
print("Error fetching segment file.. Redownloading...")
download_media(filename, url, lecture_working_dir, epoch + 1) download_media(filename, url, lecture_working_dir, epoch + 1)
elif (media.status_code == 404): except:
print("Probably end hit!\n", url)
return True #Probably hit the last of the file
else:
if (epoch > retry): if (epoch > retry):
exit("Error fetching segment, exceeded retry times.") exit("Error fetching segment, exceeded retry times.")
print("Error fetching segment file.. Redownloading...") print("Error fetching segment file.. Redownloading...")