nemubot/modules/ddg/__init__.py

71 lines
1.6 KiB
Python
Raw Normal View History

# coding=utf-8
2014-08-27 23:39:31 +00:00
"""Search around various search engine or knowledges database"""
import imp
2015-02-11 17:12:39 +00:00
from nemubot import context
2015-07-06 20:10:55 +00:00
from nemubot.exception import IRCException
from nemubot.hooks import hook
2014-08-13 13:53:55 +00:00
nemubotversion = 3.4
from more import Response
from . import DDGSearch
from . import UrbanDictionnary
@hook("cmd_hook", "define")
def define(msg):
2015-07-06 20:10:55 +00:00
if not len(msg.args):
raise IRCException("Indicate a term to define")
2015-07-06 20:10:55 +00:00
s = DDGSearch.DDGSearch(' '.join(msg.args))
2015-07-06 20:10:55 +00:00
return Response(s.definition, channel=msg.channel)
@hook("cmd_hook", "search")
def search(msg):
2015-07-06 20:10:55 +00:00
if not len(msg.args):
raise IRCException("Indicate a term to search")
2015-07-06 20:10:55 +00:00
if "!safeoff" in msg.args:
msg.args.remove("!safeoff")
safeoff = True
else:
safeoff = False
s = DDGSearch.DDGSearch(' '.join(msg.args), safeoff)
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)
2015-07-06 20:10:55 +00:00
res.append_message(s.definition)
return res
@hook("cmd_hook", "urbandictionnary")
def udsearch(msg):
2015-07-06 20:10:55 +00:00
if not len(msg.args):
raise IRCException("Indicate a term to search")
2015-07-06 20:10:55 +00:00
s = UrbanDictionnary.UrbanDictionnary(' '.join(msg.args))
res = Response(channel=msg.channel, nomore="No more results",
count=" (%d more definitions)")
for d in s.definitions:
2015-07-06 20:10:55 +00:00
res.append_message(d.replace("\n", " "))
return res