diff --git a/botcmd.py b/botcmd.py
index 9192b91..11978c8 100644
--- a/botcmd.py
+++ b/botcmd.py
@@ -127,27 +127,35 @@ def handle_message(*, prefix, message, nick, channel, irc):
for url in urls:
if possible_titles_left == 0: break
+ domain = sanitize(urllib.parse.urlparse(url).netloc)
+
try:
- with urllib.request.urlopen(url, timeout = 5) as response:
- if response.info().get_content_type() == 'text/html':
- # First 4KiB of a page should be enough for any
- # Turns out it's not, so download 64KiB
- page_source_fragment = response.read(64 * 1024)
- title = sanitize(extract_title(page_source_fragment))
+ try:
+ with urllib.request.urlopen(url, timeout = 5) as response:
+ if response.info().get_content_type() == 'text/html':
+ # First 4KiB of a page should be enough for any
+ # Turns out it's not, so download 64KiB
+ page_source_fragment = response.read(64 * 1024)
+ title = sanitize(extract_title(page_source_fragment))
- domain = sanitize(urllib.parse.urlparse(url).netloc)
+ if title is not None:
+ message = '%s: %s' % (domain, title)
+ else:
+ message = '%s: ' % domain
+ irc.bot_response(channel, message)
- if title is not None:
- message = '%s: %s' % (domain, title)
- else:
- message = '%s: ' % domain
- irc.bot_response(channel, message)
+ possible_titles_left -= 1
- possible_titles_left -= 1
+ except urllib.error.HTTPError as e:
+ # Tell ppl if server responded with an error code
+ message = '%s: %i %s' % (domain, e.getcode(), e.msg)
+ irc.bot_response(channel, message)
+ possible_titles_left -= 1
except (IOError, urllib.error.URLError):
continue
+
# handle_nonmessage(*, prefix, command, arguments, irc)
# Called for all other commands than PINGs and PRIVMSGs.
# prefix is the prefix at the start of the message, without the leading ':'