From 56990f4d691a27dba02675c4f2cfeb859b245e5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhani=20Krekel=C3=A4?= Date: Sun, 28 Oct 2018 08:51:08 +0200 Subject: [PATCH] Tell users if we ran into weird HTTP codes --- botcmd.py | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) 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 <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 ':'