Wikifetch: cleanup redirect handling; fix tests

I also remove the showRedirects logic as the addition text is usually far too verbose for IRC
This commit is contained in:
James Lu 2022-06-19 15:38:46 -07:00
parent fb877f94bc
commit b2d800ce61
3 changed files with 3 additions and 19 deletions

View File

@ -55,8 +55,5 @@ Wikifetch = conf.registerPlugin('Wikifetch')
conf.registerChannelValue(Wikifetch, 'url', conf.registerChannelValue(Wikifetch, 'url',
registry.String(_('en.wikipedia.org'), _("""Default URL of the registry.String(_('en.wikipedia.org'), _("""Default URL of the
website to pull from."""))) website to pull from.""")))
conf.registerChannelValue(Wikifetch, 'showRedirects',
registry.Boolean(True, _("""Determines whether redirect paths will
be shown in the output.""")))
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: # vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:

View File

@ -143,19 +143,7 @@ class Wikifetch(callbacks.Plugin):
self.log.debug('Wikifetch: following search result:') self.log.debug('Wikifetch: following search result:')
tree, article, addr = self._get_article_tree(None, searchresults[0].get('href')) tree, article, addr = self._get_article_tree(None, searchresults[0].get('href'))
search = redirect search = redirect
# otherwise, simply return the title and whether it redirected
elif self.registryValue('showRedirects', msg.args[0]):
redirect = re.search(r'\(%s <a href=[^>]*>([^<]*)</a>\)' %
_('Redirected from'), article)
if redirect:
try:
redirect = tree.xpath('//span[@class="mw-redirectedfrom"]/a')[0]
redirect = redirect.text_content().strip()
title = tree.xpath('//*[@class="firstHeading"]')
title = title[0].text_content().strip()
reply += '"%s" (Redirected from "%s"): ' % (title, redirect)
except IndexError:
pass
# extract the address we got it from - most sites have the perm link # extract the address we got it from - most sites have the perm link
# inside the page itself # inside the page itself
try: try:

View File

@ -66,9 +66,8 @@ if network:
self.assertNotError('wiki GNU/Linux') self.assertNotError('wiki GNU/Linux')
self.assertNotError('wiki --site en.wikipedia.org /') self.assertNotError('wiki --site en.wikipedia.org /')
def testRedirects(self): def testFollowRedirects(self):
self.assertRegexp('wiki Foo', '"Foobar" \(Redirected from "Foo"\): ' self.assertRegexp('wiki YVR', 'Vancouver International Airport')
'The terms \x02foobar\x02')
def testWikiBold(self): def testWikiBold(self):
self.assertRegexp('wiki Apple', '\x02') self.assertRegexp('wiki Apple', '\x02')