Skip to content

Commit

Permalink
DDG: reintroduce support for multiple results
Browse files Browse the repository at this point in the history
  • Loading branch information
James Lu authored and James Lu committed Jan 17, 2015
1 parent f62c6ee commit 3c5cc19
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
10 changes: 7 additions & 3 deletions DDG/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
48 changes: 29 additions & 19 deletions DDG/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,27 +69,37 @@ def search(self, irc, msg, args, text):
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:
res = t.next_sibling.next_sibling
if not res:
continue
try:
# 1) Get a result snippet.
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
except AttributeError:
continue
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]
snippet = snippet.text.strip()
# 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:
irc.error("No results found.")
if not replies:
irc.error("No results found.")
else:
irc.reply(', '.join(replies))
search = wrap(search, ['text'])

Class = DDG
Expand Down

0 comments on commit 3c5cc19

Please sign in to comment.