2012-06-30 15:06:34 +00:00
|
|
|
# coding=utf-8
|
|
|
|
|
2012-07-03 02:13:10 +00:00
|
|
|
import imp
|
|
|
|
|
2014-08-12 18:10:19 +00:00
|
|
|
from hooks import hook
|
|
|
|
|
2014-08-13 13:53:55 +00:00
|
|
|
nemubotversion = 3.4
|
2012-06-30 15:06:34 +00:00
|
|
|
|
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-07-24 15:56:20 +00:00
|
|
|
from . import Wikipedia
|
2012-06-30 15:06:34 +00:00
|
|
|
|
2012-08-31 00:58:28 +00:00
|
|
|
def load(context):
|
|
|
|
global CONF
|
|
|
|
WFASearch.CONF = CONF
|
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:
|
2012-08-31 00:58:28 +00:00
|
|
|
return Response(msg.sender,
|
|
|
|
"Indicate a term to define",
|
|
|
|
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
|
|
|
|
|
|
|
res = Response(msg.sender, channel=msg.channel)
|
|
|
|
|
|
|
|
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:
|
2012-08-31 00:58:28 +00:00
|
|
|
return Response(msg.sender,
|
|
|
|
"Indicate a term to search",
|
|
|
|
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
|
|
|
|
|
|
|
res = Response(msg.sender, 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
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
return Response(msg.sender,
|
|
|
|
"Indicate a term to search",
|
|
|
|
msg.channel, nick=msg.nick)
|
|
|
|
|
|
|
|
s = UrbanDictionnary.UrbanDictionnary(' '.join(msg.cmds[1:]))
|
|
|
|
|
|
|
|
res = Response(msg.sender, channel=msg.channel, nomore="No more results",
|
|
|
|
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:
|
2012-08-31 00:58:28 +00:00
|
|
|
return Response(msg.sender,
|
|
|
|
"Indicate a calcul to compute",
|
|
|
|
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:
|
|
|
|
res = Response(msg.sender, channel=msg.channel, nomore="No more results")
|
|
|
|
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:
|
2012-08-31 00:58:28 +00:00
|
|
|
return Response(msg.sender, s.error, msg.channel)
|
|
|
|
|
|
|
|
|
2014-08-12 18:10:19 +00:00
|
|
|
@hook("cmd_hook", "wikipedia")
|
|
|
|
def wikipedia(msg):
|
|
|
|
return wiki("wikipedia.org", 0, msg)
|
|
|
|
|
|
|
|
@hook("cmd_hook", "wiktionary")
|
|
|
|
def wiktionary(msg):
|
|
|
|
return wiki("wiktionary.org", 1, msg)
|
|
|
|
|
|
|
|
@hook("cmd_hook", "etymology")
|
|
|
|
def wiktionary(msg):
|
|
|
|
return wiki("wiktionary.org", 0, msg)
|
|
|
|
|
|
|
|
def wiki(site, section, msg):
|
2012-11-04 03:39:54 +00:00
|
|
|
if len(msg.cmds) <= 1:
|
2012-08-31 00:58:28 +00:00
|
|
|
return Response(msg.sender,
|
|
|
|
"Indicate a term to search",
|
|
|
|
msg.channel, nick=msg.nick)
|
2012-11-30 10:36:14 +00:00
|
|
|
if len(msg.cmds) > 2 and len(msg.cmds[1]) < 4:
|
|
|
|
lang = msg.cmds[1]
|
|
|
|
extract = 2
|
|
|
|
else:
|
2012-08-31 00:58:28 +00:00
|
|
|
lang = "fr"
|
2012-11-30 10:36:14 +00:00
|
|
|
extract = 1
|
2012-06-30 15:06:34 +00:00
|
|
|
|
2012-11-30 10:36:14 +00:00
|
|
|
s = Wikipedia.Wikipedia(' '.join(msg.cmds[extract:]), lang, site, section)
|
2012-06-30 15:06:34 +00:00
|
|
|
|
2012-08-31 00:58:28 +00:00
|
|
|
res = Response(msg.sender, channel=msg.channel, nomore="No more results")
|
2012-11-07 14:37:17 +00:00
|
|
|
if site == "wiktionary.org":
|
|
|
|
tout = [result for result in s.nextRes if result.find("\x03\x16 :\x03\x16 ") != 0]
|
2012-11-30 10:36:14 +00:00
|
|
|
if len(tout) > 0:
|
|
|
|
defI=1
|
|
|
|
for t in tout:
|
|
|
|
if t.find("# ") == 0:
|
|
|
|
t = t.replace("# ", "%d. " % defI)
|
|
|
|
defI += 1
|
|
|
|
elif t.find("#* ") == 0:
|
|
|
|
t = t.replace("#* ", " * ")
|
|
|
|
res.append_message(t)
|
2012-11-07 14:37:17 +00:00
|
|
|
else:
|
|
|
|
for result in s.nextRes:
|
|
|
|
res.append_message(result)
|
2012-06-30 15:06:34 +00:00
|
|
|
|
2012-08-31 00:58:28 +00:00
|
|
|
if len(res.messages) > 0:
|
|
|
|
return res
|
|
|
|
else:
|
|
|
|
return Response(msg.sender,
|
2012-12-07 00:16:05 +00:00
|
|
|
"No information about " + " ".join(msg.cmds[extract:]),
|
2012-08-31 00:58:28 +00:00
|
|
|
msg.channel)
|