Add exception handling for timeout and connection errors
This commit is contained in:
parent
7e3f215a8a
commit
0d8f3b6b11
1 changed files with 30 additions and 5 deletions
|
@ -317,14 +317,39 @@ def download_multiple(feed, maxnum):
|
||||||
def download_single(folder, url, filename):
|
def download_single(folder, url, filename):
|
||||||
print(url)
|
print(url)
|
||||||
base = CONFIGURATION['podcast-directory']
|
base = CONFIGURATION['podcast-directory']
|
||||||
|
|
||||||
|
if 'connection_timeout' in CONFIGURATION:
|
||||||
|
connection_timeout = CONFIGURATION['connection_timeout']
|
||||||
|
else:
|
||||||
|
connection_timeout = 10
|
||||||
|
|
||||||
|
if 'retries' in CONFIGURATION:
|
||||||
|
retries = CONFIGURATION['retries']
|
||||||
|
else:
|
||||||
|
retries = 3
|
||||||
|
|
||||||
if filename is None:
|
if filename is None:
|
||||||
filename = get_original_filename(url)
|
filename = get_original_filename(url)
|
||||||
|
|
||||||
print_green("{:s} downloading".format(filename))
|
print_green("{:s} downloading".format(filename))
|
||||||
r = requests.get(url.strip(), stream=True)
|
|
||||||
with open(os.path.join(base, folder, filename), 'wb') as f:
|
for i in range(retries):
|
||||||
for chunk in r.iter_content(chunk_size=1024**2):
|
try:
|
||||||
f.write(chunk)
|
r = requests.get(url.strip(), stream=True, timeout=connection_timeout)
|
||||||
print("done.")
|
with open(os.path.join(base, folder, filename), 'wb') as f:
|
||||||
|
for chunk in r.iter_content(chunk_size=1024**2):
|
||||||
|
f.write(chunk)
|
||||||
|
except ConnectionError:
|
||||||
|
if i == retries-1:
|
||||||
|
print("Connection failed")
|
||||||
|
continue
|
||||||
|
except Timeout:
|
||||||
|
if i == retries-1:
|
||||||
|
print("Connection to server timed out")
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
print("done.")
|
||||||
|
break
|
||||||
|
|
||||||
|
|
||||||
def available_feeds():
|
def available_feeds():
|
||||||
|
|
Loading…
Add table
Reference in a new issue