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') DDG = conf.registerPlugin('DDG')
# This is where your configuration variables (if any) should go. For example: conf.registerChannelValue(DDG, 'maxResults',
# conf.registerGlobalValue(DDG, 'someConfigVariableName', registry.PositiveInteger(4, _("""Determines the maximum number of
# registry.Boolean(False, _("""Help for someConfigVariableName."""))) 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: # vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:

View File

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