1
0
mirror of http://aero2k.de/t/repos/urlbot-native.git synced 2017-09-06 15:25:38 +02:00

added some more error handling because error handling is cool

This commit is contained in:
chat
2014-07-21 02:58:29 +02:00
parent b8cf1a0d58
commit 8aa8485621

View File

@@ -29,14 +29,19 @@ def logger(severity, message):
def fetch_page(url): def fetch_page(url):
logger('info', 'fetching page ' + url) logger('info', 'fetching page ' + url)
try:
response = urllib.urlopen(url) response = urllib.urlopen(url)
html = response.read(BUFSIZ) html = response.read(BUFSIZ)
response.close() response.close()
return html return html
except IOError as e:
logger('warn', 'failed: ' + e.errno)
def extract_title(url): def extract_title(url):
logger('info', 'extracting title from ' + url) logger('info', 'extracting title from ' + url)
html = fetch_page(url) html = fetch_page(url)
if html:
result = re.match(r'.*?<title.*?>(.*?)</title>.*?', html, re.S|re.M) result = re.match(r'.*?<title.*?>(.*?)</title>.*?', html, re.S|re.M)
if result: if result:
return result.groups()[0] return result.groups()[0]
@@ -55,7 +60,11 @@ def extract_url(data):
for r in result: for r in result:
title = extract_title(r) title = extract_title(r)
if title:
message = 'Title: %s: %s' % (title, e(r)) message = 'Title: %s: %s' % (title, e(r))
else:
message = 'some error occured when fetching %s' % e(r)
logger('info', 'printing ' + message) logger('info', 'printing ' + message)
if debug_enabled(): if debug_enabled():