DDG: rewrite to fix "max results" not working

From: 7a6e2f9f97
This commit is contained in:
James Lu 2017-01-20 18:50:16 -08:00
parent 30615a7284
commit 48b5016f41

View File

@ -81,6 +81,7 @@ class DDG(callbacks.Plugin):
# settings given to the function directly. # settings given to the function directly.
show_snippet = self.registryValue("showSnippet", channel_context) show_snippet = self.registryValue("showSnippet", channel_context)
maxr = max_results or self.registryValue("maxResults", channel_context) maxr = max_results or self.registryValue("maxResults", channel_context)
self.log.debug('DDG: got %s for max results', maxr)
# In a nutshell, the 'lite' site puts all of its usable content # In a nutshell, the 'lite' site puts all of its usable content
# into tables. This means that headings, result snippets and # into tables. This means that headings, result snippets and
@ -88,13 +89,12 @@ class DDG(callbacks.Plugin):
# parsing somewhat tricky. # parsing somewhat tricky.
results = [] results = []
for t in self._ddgurl(text): raw_results = self._ddgurl(text)
# We run a for loop here to extract meaningful content: for t in raw_results:
for n in range(1, maxr):
res = '' res = ''
# Each valid result has a preceding heading in the format # Each valid result has a preceding heading in the format
# '<td valign="top">1.&nbsp;</td>', etc. # '<td valign="top">1.&nbsp;</td>', etc.
if ("%s." % n) in t.text: if t.text[0].isdigit():
res = t.next_sibling.next_sibling res = t.next_sibling.next_sibling
if not res: if not res:
continue continue
@ -131,7 +131,7 @@ class DDG(callbacks.Plugin):
results.append(s) results.append(s)
except AttributeError: except AttributeError:
continue continue
return results return results[:maxr]
@wrap(['text']) @wrap(['text'])
def search(self, irc, msg, args, text): def search(self, irc, msg, args, text):