DDG: reintroduce support for multiple results

From: 3c5cc19ba7
This commit is contained in:
James Lu 2015-01-17 02:36:08 -05:00
parent 94c582a4a0
commit d26e8ff93d
2 changed files with 36 additions and 22 deletions

View File

@ -49,9 +49,13 @@ def configure(advanced):
DDG = conf.registerPlugin('DDG')
# This is where your configuration variables (if any) should go. For example:
# conf.registerGlobalValue(DDG, 'someConfigVariableName',
# registry.Boolean(False, _("""Help for someConfigVariableName.""")))
conf.registerChannelValue(DDG, 'maxResults',
registry.PositiveInteger(4, _("""Determines the maximum number of
results the bot will respond with.""")))
conf.registerChannelValue(DDG, 'showSnippet',
registry.Boolean(True, _("""Determines whether the bot will show a
snippet of each resulting link. If False, it will show the title
of the link instead.""")))
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:

View File

@ -69,27 +69,37 @@ class DDG(callbacks.Plugin):
self.log.info(url)
irc.error(str(e), Raise=True)
soup = BeautifulSoup(data)
res = ''
replies = []
channel = msg.args[0]
for t in soup.find_all('td'):
if "1." in t.text:
maxr = self.registryValue("maxResults", channel)
for n in range(1, maxr):
res = ''
if ("%s." % n) in t.text:
res = t.next_sibling.next_sibling
if not res:
continue
try:
snippet = ''
# 1) Get a result snippet.
if self.registryValue("showsnippet", channel):
snippet = res.parent.next_sibling.next_sibling.\
find_all("td")[-1]
# 2) Fetch the result link.
link = res.a.get('href')
snippet = snippet.text.strip()
s = format("%s - %u", snippet, link)
irc.reply(s)
return
# 2) Fetch the link title.
title = res.a.text.strip()
# 3) Fetch the result link.
link = res.a.get('href')
s = format("%s - %s %u", ircutils.bold(title), snippet,
link)
replies.append(s)
except AttributeError:
continue
else:
if not replies:
irc.error("No results found.")
else:
irc.reply(', '.join(replies))
search = wrap(search, ['text'])
Class = DDG