nemubot/modules/ddg/__init__.py
nemunaire 41da1c0780 Response class is now part of 'more' module.
This commit prepare the new message flow based on protocol independent messages.

	This commit changes the module API: you need to import the Response class manually at the begining of our module.
2014-09-27 12:39:44 +02:00

98 lines
2.3 KiB
Python

# coding=utf-8
"""Search around various search engine or knowledges database"""
import imp
from hooks import hook
nemubotversion = 3.4
from more import Response
from . import DDGSearch
from . import UrbanDictionnary
from . import WFASearch
def load(context):
global CONF
WFASearch.CONF = CONF
def reload():
imp.reload(DDGSearch)
imp.reload(UrbanDictionnary)
imp.reload(WFASearch)
imp.reload(Wikipedia)
@hook("cmd_hook", "define")
def define(msg):
if len(msg.cmds) <= 1:
return Response("Indicate a term to define",
msg.channel, nick=msg.nick)
s = DDGSearch.DDGSearch(' '.join(msg.cmds[1:]))
res = Response(channel=msg.channel)
res.append_message(s.definition)
return res
@hook("cmd_hook", "search")
def search(msg):
if len(msg.cmds) <= 1:
return Response("Indicate a term to search",
msg.channel, nick=msg.nick)
s = DDGSearch.DDGSearch(' '.join(msg.cmds[1:]))
res = Response(channel=msg.channel, nomore="No more results",
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
@hook("cmd_hook", "urbandictionnary")
def udsearch(msg):
if len(msg.cmds) <= 1:
return Response("Indicate a term to search",
msg.channel, nick=msg.nick)
s = UrbanDictionnary.UrbanDictionnary(' '.join(msg.cmds[1:]))
res = Response(channel=msg.channel, nomore="No more results",
count=" (%d more definitions)")
for d in s.definitions:
res.append_message(d)
return res
@hook("cmd_hook", "calculate")
def calculate(msg):
if len(msg.cmds) <= 1:
return Response("Indicate a calcul to compute",
msg.channel, nick=msg.nick)
s = WFASearch.WFASearch(' '.join(msg.cmds[1:]))
if s.success:
res = Response(channel=msg.channel, nomore="No more results")
for result in s.nextRes:
res.append_message(result)
if (len(res.messages) > 0):
res.messages.pop(0)
return res
else:
return Response(s.error, msg.channel)