Tell users if we ran into weird HTTP codes
This commit is contained in:
parent
284ca9659b
commit
56990f4d69
34
botcmd.py
34
botcmd.py
|
@ -127,27 +127,35 @@ def handle_message(*, prefix, message, nick, channel, irc):
|
||||||
for url in urls:
|
for url in urls:
|
||||||
if possible_titles_left == 0: break
|
if possible_titles_left == 0: break
|
||||||
|
|
||||||
|
domain = sanitize(urllib.parse.urlparse(url).netloc)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with urllib.request.urlopen(url, timeout = 5) as response:
|
try:
|
||||||
if response.info().get_content_type() == 'text/html':
|
with urllib.request.urlopen(url, timeout = 5) as response:
|
||||||
# First 4KiB of a page should be enough for any <title>
|
if response.info().get_content_type() == 'text/html':
|
||||||
# Turns out it's not, so download 64KiB
|
# First 4KiB of a page should be enough for any <title>
|
||||||
page_source_fragment = response.read(64 * 1024)
|
# Turns out it's not, so download 64KiB
|
||||||
title = sanitize(extract_title(page_source_fragment))
|
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:
|
possible_titles_left -= 1
|
||||||
message = '%s: %s' % (domain, title)
|
|
||||||
else:
|
|
||||||
message = '%s: <no title found>' % domain
|
|
||||||
irc.bot_response(channel, message)
|
|
||||||
|
|
||||||
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):
|
except (IOError, urllib.error.URLError):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
|
||||||
# handle_nonmessage(*, prefix, command, arguments, irc)
|
# handle_nonmessage(*, prefix, command, arguments, irc)
|
||||||
# Called for all other commands than PINGs and PRIVMSGs.
|
# Called for all other commands than PINGs and PRIVMSGs.
|
||||||
# prefix is the prefix at the start of the message, without the leading ':'
|
# prefix is the prefix at the start of the message, without the leading ':'
|
||||||
|
|
Loading…
Reference in New Issue