Exception handling only where needed

main
Bob Mottram 2021-12-17 09:55:19 +00:00
parent b5975917a6
commit 974e2d0830
1 changed files with 28 additions and 21 deletions

View File

@ -459,28 +459,9 @@ def getImageBinaryFromUrl(session, url: str, timeoutSec: int, debug: bool):
"""
mimeType = 'image/png'
contentType = None
result = None
try:
result = session.get(url, timeout=timeoutSec)
if result.status_code != 200:
print('WARN: getImageFromUrl: ' + url +
' failed with error code ' + str(result.status_code))
if result.headers.get('content-type'):
contentType = result.headers['content-type']
elif result.headers.get('Content-type'):
contentType = result.headers['Content-type']
elif result.headers.get('Content-Type'):
contentType = result.headers['Content-Type']
if not contentType:
return None, None
if 'image/x-icon' in contentType:
mimeType = 'image/x-icon'
elif 'image/webp' in contentType:
mimeType = 'image/webp'
elif 'image/jpeg' in contentType:
mimeType = 'image/jpeg'
elif 'image/gif' in contentType:
mimeType = 'image/gif'
return result.content, mimeType
except requests.exceptions.RequestException as e:
if debug:
print('ERROR: getImageFromUrl failed: ' + str(url) + ', ' +
@ -493,4 +474,30 @@ def getImageBinaryFromUrl(session, url: str, timeoutSec: int, debug: bool):
if e.errno == errno.ECONNRESET:
print('WARN: getImageFromUrl failed, ' +
'connection was reset ' + str(e))
return None, None
if not result:
return None, None
if result.status_code != 200:
print('WARN: getImageFromUrl: ' + url +
' failed with error code ' + str(result.status_code))
if result.headers.get('content-type'):
contentType = result.headers['content-type']
elif result.headers.get('Content-type'):
contentType = result.headers['Content-type']
elif result.headers.get('Content-Type'):
contentType = result.headers['Content-Type']
if not contentType:
return None, None
if 'image/x-icon' in contentType:
mimeType = 'image/x-icon'
elif 'image/webp' in contentType:
mimeType = 'image/webp'
elif 'image/jpeg' in contentType:
mimeType = 'image/jpeg'
elif 'image/gif' in contentType:
mimeType = 'image/gif'
return result.content, mimeType