Web: Implement protocols.http.requestLanguage

This commit is contained in:
Tasos Sahanidis 2020-01-12 03:30:12 +02:00 committed by Valentin Lorentz
parent 62f8b47e61
commit ae5ad2ceab

View File

@ -146,12 +146,20 @@ class Web(callbacks.PluginRegexp):
def noIgnore(self, irc, msg):
return not self.registryValue('checkIgnored', msg.channel, irc.network)
def getTitle(self, irc, url, raiseErrors):
def getTitle(self, irc, url, raiseErrors, msg):
size = conf.supybot.protocols.http.peekSize()
timeout = self.registryValue('timeout')
headers = dict(utils.web.defaultHeaders)
language = conf.supybot.protocols.http.requestLanguage.getSpecific(
irc.network, msg.channel)()
lkey = 'Accept-Language'
if language:
headers[lkey] = language
elif lkey in headers:
del headers[lkey]
try:
(target, text) = utils.web.getUrlTargetAndContent(url, size=size,
timeout=timeout)
timeout=timeout, headers=headers)
except Exception as e:
if raiseErrors:
irc.error(_('That URL raised <' + str(e)) + '>',
@ -217,7 +225,7 @@ class Web(callbacks.PluginRegexp):
if r and r.search(url):
self.log.debug('Not titleSnarfing %q.', url)
return
r = self.getTitle(irc, url, False)
r = self.getTitle(irc, url, False, msg)
if not r:
return
(target, title) = r
@ -342,7 +350,7 @@ class Web(callbacks.PluginRegexp):
if not self._checkURLWhitelist(url):
irc.error("This url is not on the whitelist.")
return
r = self.getTitle(irc, url, True)
r = self.getTitle(irc, url, True, msg)
if not r:
return
(target, title) = r