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

16
main.py
View File

@ -681,12 +681,15 @@ 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:
try:
media = requests.get(url, stream=True) media = requests.get(url, stream=True)
media_length = int(media.headers.get("content-length")) media_length = int(media.headers.get("content-length"))
if media.status_code == 200: if media.status_code == 200:
if (os.path.isfile(filename) if (os.path.isfile(filename)
and os.path.getsize(filename) >= media_length): and os.path.getsize(filename) >= media_length):
print("Segment already downloaded.. skipping write to disk..") print(
"Segment already downloaded.. skipping write to disk.."
)
else: else:
try: try:
pbar = tqdm(total=media_length, pbar = tqdm(total=media_length,
@ -705,7 +708,8 @@ def download_media(filename, url, lecture_working_dir, epoch=0):
return False #Successfully downloaded the file return False #Successfully downloaded the file
except: except:
print( print(
"Connection error: Reattempting download of segment..") "Connection error: Reattempting download of segment.."
)
download_media(filename, url, lecture_working_dir, download_media(filename, url, lecture_working_dir,
epoch + 1) epoch + 1)
@ -713,7 +717,8 @@ def download_media(filename, url, lecture_working_dir, epoch=0):
pass pass
else: else:
print("Segment is faulty.. Redownloading...") print("Segment is faulty.. Redownloading...")
download_media(filename, url, lecture_working_dir, epoch + 1) download_media(filename, url, lecture_working_dir,
epoch + 1)
elif (media.status_code == 404): elif (media.status_code == 404):
print("Probably end hit!\n", url) print("Probably end hit!\n", url)
return True #Probably hit the last of the file return True #Probably hit the last of the file
@ -722,6 +727,11 @@ def download_media(filename, url, lecture_working_dir, epoch=0):
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...")
download_media(filename, url, lecture_working_dir, epoch + 1) download_media(filename, url, lecture_working_dir, epoch + 1)
except:
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)
def cleanup(path): def cleanup(path):