From f556c2c29979f02eb12ca501267e5a0fbf6bdcd7 Mon Sep 17 00:00:00 2001 From: T0jan Date: Wed, 28 Jun 2023 16:32:28 +0200 Subject: [PATCH] more errorhandling --- kintree/common/tools.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/kintree/common/tools.py b/kintree/common/tools.py index 603831ef..64c13be6 100644 --- a/kintree/common/tools.py +++ b/kintree/common/tools.py @@ -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) @@ -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(): @@ -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