From 526ffb0ccb741621e6654475f193ac9209335401 Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Sat, 17 Oct 2015 15:41:20 +0200 Subject: [PATCH] Web: Fix code factorization (576a96fb71e318a15e93784362cf5b9f04b51be3). Closes GH-1173. --- plugins/Web/plugin.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/plugins/Web/plugin.py b/plugins/Web/plugin.py index eb4bc0bf7..378275575 100644 --- a/plugins/Web/plugin.py +++ b/plugins/Web/plugin.py @@ -139,7 +139,7 @@ class Web(callbacks.PluginRegexp): def noIgnore(self, irc, msg): return not self.registryValue('checkIgnored', msg.args[0]) - def getTitle(self, url): + def getTitle(self, url, raiseErrors): size = conf.supybot.protocols.http.peekSize() text = utils.web.getUrl(url, size=size) try: @@ -149,12 +149,21 @@ class Web(callbacks.PluginRegexp): pass parser = Title() if minisix.PY3 and isinstance(text, bytes): - irc.error(_('Could not guess the page\'s encoding. (Try ' - 'installing python-charade.)'), Raise=True) + if raiseErrors: + irc.error(_('Could not guess the page\'s encoding. (Try ' + 'installing python-charade.)'), Raise=True) + else: + return None parser.feed(text) title = parser.title if title: title = utils.web.htmlToText(parser.title.strip()) + elif raiseErrors: + if len(text) < size: + irc.reply(_('That URL appears to have no HTML title.')) + else: + irc.reply(format(_('That URL appears to have no HTML title ' + 'within the first %S.'), size)) return title @fetch_sandbox @@ -172,7 +181,7 @@ class Web(callbacks.PluginRegexp): if r and r.search(url): self.log.debug('Not titleSnarfing %q.', url) return - title = self.getTitle(url) + title = self.getTitle(url, False) if title: domain = utils.web.getDomain(fd.geturl() if self.registryValue('snarferShowTargetDomain', channel) @@ -283,17 +292,12 @@ class Web(callbacks.PluginRegexp): if not self._checkURLWhitelist(url): irc.error("This url is not on the whitelist.") return - title = self.getTitle(url) + title = self.getTitle(url, True) if title: if not [y for x,y in optlist if x == 'no-filter']: for i in range(1, 4): title = title.replace(chr(i), '') irc.reply(title) - elif len(text) < size: - irc.reply(_('That URL appears to have no HTML title.')) - else: - irc.reply(format(_('That URL appears to have no HTML title ' - 'within the first %S.'), size)) title = wrap(title, [getopts({'no-filter': ''}), 'httpUrl']) @internationalizeDocstring