Merge pull request #137 from kytvi2p/wikipediafixes

Various fixes for the Wikipedia plugin
master
Valentin Lorentz 2013-08-25 03:40:23 -07:00
commit 6c71749318
1 changed files with 19 additions and 3 deletions

View File

@ -87,11 +87,17 @@ class Wikipedia(callbacks.Plugin):
'[@title="Special:Search"]')
if didyoumean:
redirect = didyoumean[0].text_content().strip()
if sys.version_info[0] < 3:
if isinstance(redirect, unicode):
redirect = redirect.encode('utf-8','replace')
if isinstance(search, unicode):
search = search.encode('utf-8','replace')
reply += _('I didn\'t find anything for "%s".'
'Did you mean "%s"? ') % (search, redirect)
addr = self.registryValue('url', msg.args[0]) + \
didyoumean[0].get('href')
article = utils.web.getUrl(addr)
if not article.startswith('http'):
article = utils.web.getUrl('https://' + addr)
if sys.version_info[0] >= 3:
article = article.decode()
tree = lxml.html.document_fromstring(article)
@ -108,6 +114,7 @@ class Wikipedia(callbacks.Plugin):
article = utils.web.getUrl(addr)
if sys.version_info[0] >= 3:
article = article.decode()
tree = lxml.html.document_fromstring(article)
search = redirect
# otherwise, simply return the title and whether it redirected
@ -119,6 +126,11 @@ class Wikipedia(callbacks.Plugin):
redirect = redirect.text_content().strip()
title = tree.xpath('//*[@class="firstHeading"]')
title = title[0].text_content().strip()
if sys.version_info[0] < 3:
if isinstance(title, unicode):
title = title.encode('utf-8','replace')
if isinstance(redirect, unicode):
redirect = redirect.encode('utf-8','replace')
reply += '"%s" (Redirect from "%s"): ' % (title, redirect)
# extract the address we got it from
addr = re.search(_('Retrieved from') + ' "<a href="([^"]*)">', article)
@ -142,15 +154,19 @@ class Wikipedia(callbacks.Plugin):
else:
##### etree!
p = tree.xpath("//div[@id='mw-content-text']/p[1]")
if len(p) == 0:
if len(p) == 0 or addr.endswith('Special:Search'):
reply += _('Not found, or page bad formed.')
else:
p = p[0]
p = p.text_content()
p = p.strip()
if sys.version_info[0] < 3:
p = p.encode('utf-8')
if isinstance(p, unicode):
p = p.encode('utf-8', 'replace')
if isinstance(reply, unicode):
reply = reply.encode('utf-8','replace')
reply += '%s %s' % (p, ircutils.bold(addr))
reply = reply.replace('&amp;','&')
irc.reply(reply)
wiki = wrap(wiki, ['text'])