Skip to content

Commit

Permalink
more errorhandling
Browse files Browse the repository at this point in the history
  • Loading branch information
T0jan committed Jun 28, 2023
1 parent 2960c56 commit f556c2c
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions kintree/common/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def download(url, filetype='API data', fileoutput='', timeout=3, enable_headers=

import socket
import urllib.request
import requests

# Set default timeout for download socket
socket.setdefaulttimeout(timeout)
Expand All @@ -76,7 +77,6 @@ def download(url, filetype='API data', fileoutput='', timeout=3, enable_headers=
if filetype == 'Image' or filetype == 'PDF':
# Enable use of requests library for downloading files (some URLs do NOT work with urllib)
if requests_lib:
import requests
headers = {'User-agent': 'Mozilla/5.0'}
response = requests.get(url, headers=headers, timeout=timeout, allow_redirects=True)
if filetype.lower() not in response.headers['Content-Type'].lower():
Expand All @@ -95,12 +95,14 @@ def download(url, filetype='API data', fileoutput='', timeout=3, enable_headers=
data = url_data.read()
data_json = json.loads(data.decode('utf-8'))
return data_json
except socket.timeout:
except (socket.timeout, requests.exceptions.ConnectTimeout):
cprint(f'[INFO]\tWarning: {filetype} download socket timed out ({timeout}s)', silent=silent)
except urllib.error.HTTPError:
except (urllib.error.HTTPError, ConnectionError):
cprint(f'[INFO]\tWarning: {filetype} download failed (HTTP Error)', silent=silent)
except (urllib.error.URLError, ValueError):
cprint(f'[INFO]\tWarning: {filetype} download failed (URL Error)', silent=silent)
except requests.exceptions.SSLError:
cprint(f'[INFO]\tWarning: {filetype} download failed (SSL Error)', silent=silent)
except FileNotFoundError:
cprint(f'[INFO]\tWarning: {os.path.dirname(fileoutput)} folder does not exist', silent=silent)
return None
Expand Down

0 comments on commit f556c2c

Please sign in to comment.