2012-06-30 15:06:34 +00:00
|
|
|
# coding=utf-8
|
|
|
|
|
2014-08-27 23:39:31 +00:00
|
|
|
"""Search around various search engine or knowledges database"""
|
|
|
|
|
2012-07-03 02:13:10 +00:00
|
|
|
import imp
|
|
|
|
|
2015-02-11 17:12:39 +00:00
|
|
|
from nemubot import context
|
2015-01-03 19:34:44 +00:00
|
|
|
from nemubot.hooks import hook
|
2014-08-12 18:10:19 +00:00
|
|
|
|
2014-08-13 13:53:55 +00:00
|
|
|
nemubotversion = 3.4
|
2012-06-30 15:06:34 +00:00
|
|
|
|
2014-09-26 16:00:22 +00:00
|
|
|
from more import Response
|
|
|
|
|
2012-07-03 02:13:10 +00:00
|
|
|
from . import DDGSearch
|
2014-04-22 15:19:10 +00:00
|
|
|
from . import UrbanDictionnary
|
2012-06-30 15:06:34 +00:00
|
|
|
from . import WFASearch
|
|
|
|
|
2012-08-31 00:58:28 +00:00
|
|
|
def load(context):
|
2015-02-11 17:12:39 +00:00
|
|
|
WFASearch.CONF = context.config
|
2012-06-30 15:06:34 +00:00
|
|
|
|
2012-07-03 02:13:10 +00:00
|
|
|
def reload():
|
2012-08-31 00:58:28 +00:00
|
|
|
imp.reload(DDGSearch)
|
2014-04-22 15:19:10 +00:00
|
|
|
imp.reload(UrbanDictionnary)
|
2012-08-31 00:58:28 +00:00
|
|
|
imp.reload(WFASearch)
|
|
|
|
imp.reload(Wikipedia)
|
|
|
|
|
|
|
|
|
2014-08-12 18:10:19 +00:00
|
|
|
@hook("cmd_hook", "define")
|
2012-08-31 00:58:28 +00:00
|
|
|
def define(msg):
|
2012-11-04 03:39:54 +00:00
|
|
|
if len(msg.cmds) <= 1:
|
2014-09-18 05:57:06 +00:00
|
|
|
return Response("Indicate a term to define",
|
2012-08-31 00:58:28 +00:00
|
|
|
msg.channel, nick=msg.nick)
|
|
|
|
|
2012-11-04 03:39:54 +00:00
|
|
|
s = DDGSearch.DDGSearch(' '.join(msg.cmds[1:]))
|
2012-08-31 00:58:28 +00:00
|
|
|
|
2014-09-18 05:57:06 +00:00
|
|
|
res = Response(channel=msg.channel)
|
2012-08-31 00:58:28 +00:00
|
|
|
|
|
|
|
res.append_message(s.definition)
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
2014-08-12 18:10:19 +00:00
|
|
|
@hook("cmd_hook", "search")
|
2012-08-31 00:58:28 +00:00
|
|
|
def search(msg):
|
2012-11-04 03:39:54 +00:00
|
|
|
if len(msg.cmds) <= 1:
|
2014-09-18 05:57:06 +00:00
|
|
|
return Response("Indicate a term to search",
|
2012-08-31 00:58:28 +00:00
|
|
|
msg.channel, nick=msg.nick)
|
|
|
|
|
2012-11-04 03:39:54 +00:00
|
|
|
s = DDGSearch.DDGSearch(' '.join(msg.cmds[1:]))
|
2012-08-31 00:58:28 +00:00
|
|
|
|
2014-09-18 05:57:06 +00:00
|
|
|
res = Response(channel=msg.channel, nomore="No more results",
|
2012-08-31 00:58:28 +00:00
|
|
|
count=" (%d more results)")
|
|
|
|
|
|
|
|
res.append_message(s.redirect)
|
|
|
|
res.append_message(s.abstract)
|
|
|
|
res.append_message(s.result)
|
|
|
|
res.append_message(s.answer)
|
|
|
|
|
|
|
|
for rt in s.relatedTopics:
|
|
|
|
res.append_message(rt)
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
2014-08-12 18:10:19 +00:00
|
|
|
@hook("cmd_hook", "urbandictionnary")
|
2014-04-22 15:19:10 +00:00
|
|
|
def udsearch(msg):
|
|
|
|
if len(msg.cmds) <= 1:
|
2014-09-18 05:57:06 +00:00
|
|
|
return Response("Indicate a term to search",
|
2014-04-22 15:19:10 +00:00
|
|
|
msg.channel, nick=msg.nick)
|
|
|
|
|
|
|
|
s = UrbanDictionnary.UrbanDictionnary(' '.join(msg.cmds[1:]))
|
|
|
|
|
2014-09-18 05:57:06 +00:00
|
|
|
res = Response(channel=msg.channel, nomore="No more results",
|
2014-04-22 15:19:10 +00:00
|
|
|
count=" (%d more definitions)")
|
|
|
|
|
|
|
|
for d in s.definitions:
|
|
|
|
res.append_message(d)
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
2014-08-12 18:10:19 +00:00
|
|
|
@hook("cmd_hook", "calculate")
|
2012-08-31 00:58:28 +00:00
|
|
|
def calculate(msg):
|
2012-11-04 03:39:54 +00:00
|
|
|
if len(msg.cmds) <= 1:
|
2014-09-18 05:57:06 +00:00
|
|
|
return Response("Indicate a calcul to compute",
|
2012-08-31 00:58:28 +00:00
|
|
|
msg.channel, nick=msg.nick)
|
|
|
|
|
2012-11-04 03:39:54 +00:00
|
|
|
s = WFASearch.WFASearch(' '.join(msg.cmds[1:]))
|
2012-08-31 00:58:28 +00:00
|
|
|
|
|
|
|
if s.success:
|
2014-09-18 05:57:06 +00:00
|
|
|
res = Response(channel=msg.channel, nomore="No more results")
|
2012-08-31 00:58:28 +00:00
|
|
|
for result in s.nextRes:
|
|
|
|
res.append_message(result)
|
2012-11-04 03:39:54 +00:00
|
|
|
if (len(res.messages) > 0):
|
|
|
|
res.messages.pop(0)
|
2012-08-31 00:58:28 +00:00
|
|
|
return res
|
2012-06-30 15:06:34 +00:00
|
|
|
else:
|
2014-09-18 05:57:06 +00:00
|
|
|
return Response(s.error, msg.channel)
|