Tell users if we ran into weird HTTP codes
This commit is contained in:
parent
284ca9659b
commit
56990f4d69
12
botcmd.py
12
botcmd.py
|
@ -127,6 +127,9 @@ 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:
|
||||
try:
|
||||
with urllib.request.urlopen(url, timeout = 5) as response:
|
||||
if response.info().get_content_type() == 'text/html':
|
||||
|
@ -135,8 +138,6 @@ def handle_message(*, prefix, message, nick, channel, irc):
|
|||
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:
|
||||
|
@ -145,9 +146,16 @@ def handle_message(*, prefix, message, nick, channel, irc):
|
|||
|
||||
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 ':'
|
||||
|
|
Loading…
Reference in New Issue