Tell users if we ran into weird HTTP codes

This commit is contained in:
Juhani Krekelä 2018-10-28 08:51:08 +02:00
parent 284ca9659b
commit 56990f4d69
1 changed files with 21 additions and 13 deletions

View File

@ -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 <title>
# 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 <title>
# 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: <no title found>' % domain
irc.bot_response(channel, message)
if title is not None:
message = '%s: %s' % (domain, title)
else:
message = '%s: <no title found>' % 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 ':'