2012-06-30 15:06:34 +00:00
|
|
|
# coding=utf-8
|
|
|
|
|
|
|
|
from urllib.parse import quote
|
|
|
|
|
2015-06-03 19:05:28 +00:00
|
|
|
from nemubot import context
|
|
|
|
from nemubot.exception import IRCException
|
|
|
|
from nemubot.hooks import hook
|
2015-01-03 19:34:44 +00:00
|
|
|
from nemubot.tools import web
|
2014-12-17 06:32:34 +00:00
|
|
|
|
2015-06-03 19:05:28 +00:00
|
|
|
nemubotversion = 4.0
|
|
|
|
|
|
|
|
from more import Response
|
|
|
|
|
|
|
|
URL_API = "http://api.wolframalpha.com/v2/query?input=%%s&appid=%s"
|
|
|
|
|
|
|
|
def load(context):
|
|
|
|
global URL_API
|
|
|
|
if not context.config or not context.config.hasAttribute("apikey"):
|
|
|
|
raise ImportError ("You need a Wolfram|Alpha API key in order to use "
|
|
|
|
"this module. Add it to the module configuration: "
|
|
|
|
"\n<module name=\"wolframalpha\" "
|
|
|
|
"apikey=\"XXXXXX-XXXXXXXXXX\" />\n"
|
|
|
|
"Register at http://products.wolframalpha.com/api/")
|
|
|
|
URL_API = URL_API % quote(context.config["apikey"]).replace("%", "%%")
|
|
|
|
|
2012-06-30 15:06:34 +00:00
|
|
|
|
|
|
|
class WFASearch:
|
2012-08-31 00:58:28 +00:00
|
|
|
def __init__(self, terms):
|
|
|
|
self.terms = terms
|
2015-06-03 19:05:28 +00:00
|
|
|
self.wfares = web.getXML(URL_API % quote(terms))
|
2012-06-30 15:06:34 +00:00
|
|
|
|
2012-08-31 00:58:28 +00:00
|
|
|
@property
|
|
|
|
def success(self):
|
|
|
|
try:
|
|
|
|
return self.wfares["success"] == "true"
|
|
|
|
except:
|
|
|
|
return False
|
2012-06-30 15:06:34 +00:00
|
|
|
|
2012-08-31 00:58:28 +00:00
|
|
|
@property
|
|
|
|
def error(self):
|
2012-11-04 15:26:20 +00:00
|
|
|
if self.wfares is None:
|
|
|
|
return "An error occurs during computation."
|
|
|
|
elif self.wfares["error"] == "true":
|
2014-12-17 06:32:34 +00:00
|
|
|
return ("An error occurs during computation: " +
|
|
|
|
self.wfares.getNode("error").getNode("msg").getContent())
|
2012-08-31 00:58:28 +00:00
|
|
|
elif self.wfares.hasNode("didyoumeans"):
|
|
|
|
start = "Did you mean: "
|
|
|
|
tag = "didyoumean"
|
|
|
|
end = "?"
|
|
|
|
elif self.wfares.hasNode("tips"):
|
|
|
|
start = "Tips: "
|
|
|
|
tag = "tip"
|
|
|
|
end = ""
|
|
|
|
elif self.wfares.hasNode("relatedexamples"):
|
|
|
|
start = "Related examples: "
|
|
|
|
tag = "relatedexample"
|
|
|
|
end = ""
|
|
|
|
elif self.wfares.hasNode("futuretopic"):
|
|
|
|
return self.wfares.getNode("futuretopic")["msg"]
|
|
|
|
else:
|
|
|
|
return "An error occurs during computation"
|
|
|
|
proposal = list()
|
|
|
|
for dym in self.wfares.getNode(tag + "s").getNodes(tag):
|
|
|
|
if tag == "tip":
|
|
|
|
proposal.append(dym["text"])
|
|
|
|
elif tag == "relatedexample":
|
|
|
|
proposal.append(dym["desc"])
|
|
|
|
else:
|
|
|
|
proposal.append(dym.getContent())
|
|
|
|
return start + ', '.join(proposal) + end
|
2012-06-30 15:06:34 +00:00
|
|
|
|
2012-08-31 00:58:28 +00:00
|
|
|
@property
|
|
|
|
def nextRes(self):
|
|
|
|
try:
|
|
|
|
for node in self.wfares.getNodes("pod"):
|
|
|
|
for subnode in node.getNodes("subpod"):
|
|
|
|
if subnode.getFirstNode("plaintext").getContent() != "":
|
2014-12-17 06:32:34 +00:00
|
|
|
yield (node["title"] + " " + subnode["title"] + ": " +
|
|
|
|
subnode.getFirstNode("plaintext").getContent())
|
2012-08-31 00:58:28 +00:00
|
|
|
except IndexError:
|
|
|
|
pass
|
2015-06-03 19:05:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
@hook("cmd_hook", "calculate")
|
|
|
|
def calculate(msg):
|
|
|
|
if not len(msg.args):
|
|
|
|
raise IRCException("Indicate a calcul to compute")
|
|
|
|
|
|
|
|
s = WFASearch(' '.join(msg.args))
|
|
|
|
|
|
|
|
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)
|