mirror of
https://github.com/Mikaela/Limnoria.git
synced 2025-01-14 06:02:36 +01:00
DDG: Initial addition of 'zeroclick' command (Closes #24)
From: 285948a47d
This commit is contained in:
parent
d26e8ff93d
commit
4a46c1b88b
60
plugin.py
60
plugin.py
@ -58,23 +58,34 @@ class DDG(callbacks.Plugin):
|
|||||||
"""Searches for results on DuckDuckGo."""
|
"""Searches for results on DuckDuckGo."""
|
||||||
threaded = True
|
threaded = True
|
||||||
|
|
||||||
def search(self, irc, msg, args, text):
|
def _ddgurl(self, text):
|
||||||
"""<query>
|
# DuckDuckGo has a 'lite' site free of unparseable JavaScript
|
||||||
|
# elements, so we'll use that to our advantage!
|
||||||
Searches for <query> on DuckDuckGo (web search)."""
|
|
||||||
url = "https://duckduckgo.com/lite?" + urlencode({"q": text})
|
url = "https://duckduckgo.com/lite?" + urlencode({"q": text})
|
||||||
try:
|
try:
|
||||||
data = utils.web.getUrl(url).decode("utf-8")
|
data = utils.web.getUrl(url).decode("utf-8")
|
||||||
except utils.web.Error as e:
|
except utils.web.Error as e:
|
||||||
self.log.info(url)
|
|
||||||
irc.error(str(e), Raise=True)
|
irc.error(str(e), Raise=True)
|
||||||
soup = BeautifulSoup(data)
|
soup = BeautifulSoup(data)
|
||||||
|
return soup.find_all('td')
|
||||||
|
|
||||||
|
def search(self, irc, msg, args, text):
|
||||||
|
"""<text>
|
||||||
|
|
||||||
|
Searches for <text> on DuckDuckGo's web search."""
|
||||||
replies = []
|
replies = []
|
||||||
channel = msg.args[0]
|
channel = msg.args[0]
|
||||||
for t in soup.find_all('td'):
|
# In a nutshell, the 'lite' site puts all of its usable content
|
||||||
|
# into tables. This means that headings, result snippets and
|
||||||
|
# everything else are all using the same tag (<td>), which makes
|
||||||
|
# parsing somewhat difficult.
|
||||||
|
for t in self._ddgurl(text):
|
||||||
maxr = self.registryValue("maxResults", channel)
|
maxr = self.registryValue("maxResults", channel)
|
||||||
|
# Hence we run a for loop to extract meaningful content:
|
||||||
for n in range(1, maxr):
|
for n in range(1, maxr):
|
||||||
res = ''
|
res = ''
|
||||||
|
# Each valid result has a preceding heading in the format
|
||||||
|
# '<td valign="top">1. </td>', etc.
|
||||||
if ("%s." % n) in t.text:
|
if ("%s." % n) in t.text:
|
||||||
res = t.next_sibling.next_sibling
|
res = t.next_sibling.next_sibling
|
||||||
if not res:
|
if not res:
|
||||||
@ -102,6 +113,43 @@ class DDG(callbacks.Plugin):
|
|||||||
irc.reply(', '.join(replies))
|
irc.reply(', '.join(replies))
|
||||||
search = wrap(search, ['text'])
|
search = wrap(search, ['text'])
|
||||||
|
|
||||||
|
@wrap(['text'])
|
||||||
|
def zeroclick(self, irc, msg, args, text):
|
||||||
|
"""<text>
|
||||||
|
|
||||||
|
Looks up <text> on DuckDuckGo's zero-click engine."""
|
||||||
|
# Zero-click can give multiple replies for things if the
|
||||||
|
# query is ambiguous, sort of like an encyclopedia.
|
||||||
|
|
||||||
|
# For example, looking up "2^3" will give both:
|
||||||
|
# Zero-click info: 8 (number)
|
||||||
|
# Zero-click info: 8
|
||||||
|
replies = {}
|
||||||
|
for td in self._ddgurl(text):
|
||||||
|
if td.text.startswith("Zero-click info:"):
|
||||||
|
# Make a dictionary of things
|
||||||
|
item = td.text.split("Zero-click info:", 1)[1].strip()
|
||||||
|
td = td.parent.next_sibling.next_sibling.\
|
||||||
|
find("td")
|
||||||
|
# Condense newlines (<br> tags)
|
||||||
|
for br in td.find_all('br'):
|
||||||
|
br.replace_with(' - ')
|
||||||
|
res = td.text.strip().split("\n")[0]
|
||||||
|
try:
|
||||||
|
# Some zero-click results have an attached link to them.
|
||||||
|
link = td.a.get('href')
|
||||||
|
# Others have a piece of meaningless JavaScript...
|
||||||
|
if link != "javascript:;":
|
||||||
|
res += format(" %u", link)
|
||||||
|
except AttributeError:
|
||||||
|
pass
|
||||||
|
replies[item] = res
|
||||||
|
else:
|
||||||
|
if not replies:
|
||||||
|
irc.error("No zero-click info could be found for '%s'." %
|
||||||
|
text, Raise=True)
|
||||||
|
s = ["%s - %s" % (ircutils.bold(k), v) for k, v in replies.items()]
|
||||||
|
irc.reply("; ".join(s))
|
||||||
Class = DDG
|
Class = DDG
|
||||||
|
|
||||||
|
|
||||||
|
4
test.py
4
test.py
@ -37,6 +37,8 @@ class DDGTestCase(PluginTestCase):
|
|||||||
def testSearch(self):
|
def testSearch(self):
|
||||||
self.assertRegexp(
|
self.assertRegexp(
|
||||||
'ddg search wikipedia', 'Wikipedia.*? - .*?https?\:\/\/')
|
'ddg search wikipedia', 'Wikipedia.*? - .*?https?\:\/\/')
|
||||||
|
def testZeroclick(self):
|
||||||
|
self.assertRegexp(
|
||||||
|
'zeroclick 2^3', '8')
|
||||||
|
|
||||||
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
|
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
|
||||||
|
Loading…
Reference in New Issue
Block a user