fix urllib.error.HTTPError calls

This commit is contained in:
Maks Snegov 2014-07-20 21:40:14 +04:00
parent fb3870e9dd
commit 41e984e1f0

View File

@ -52,8 +52,11 @@ def charset_header(content_type):
def get_text(url, content='text/html', charset='utf-8'): def get_text(url, content='text/html', charset='utf-8'):
response = urlopen(url) response = urlopen(url)
if response.status != 200: if response.status != 200:
raise urllib.error.HTTPError('Incorrect HTTP status (%d, %s) for %s' % ( raise urllib.error.HTTPError(
response.status, response.reason, url)) url, response.status,
'Incorrect HTTP status (%d, %s) for %s' % (response.status, response.reason, url),
None, None
)
ctype = response.headers.get('content-type') ctype = response.headers.get('content-type')
if ctype is None: if ctype is None:
raise RuntimeError('None content type for %s' % url) raise RuntimeError('None content type for %s' % url)
@ -73,12 +76,15 @@ def get_text(url, content='text/html', charset='utf-8'):
def embedded_image(url): def embedded_image(url):
'''Download content from URL and return bytes if target is image''' '''Download content from URL and return bytes if target is image'''
u = urlopen(url) response = urlopen(url)
if u.status != 200: if response.status != 200:
raise urllib.error.HTTPError('Incorrect HTTP status (%d, %s) for %s' % ( raise urllib.error.HTTPError(
u.status, u.reason, url)) url, response.status,
ctype = u.headers.get('Content-Type') 'Incorrect HTTP status (%d, %s) for %s' % (response.status, response.reason, url),
data = u.read() None, None
)
ctype = response.headers.get('Content-Type')
data = response.read()
b64pict = base64.b64encode(data).decode() b64pict = base64.b64encode(data).decode()
return 'data:%s;base64,%s' % (ctype, b64pict) return 'data:%s;base64,%s' % (ctype, b64pict)