[ddg/wolframalpha] extract wolframalpha module and dusting
This commit is contained in:
parent
c1858fff3a
commit
2e55ba5671
3 changed files with 39 additions and 43 deletions
|
@ -11,9 +11,7 @@
|
||||||
-->
|
-->
|
||||||
|
|
||||||
<!--
|
<!--
|
||||||
<module name="ddg">
|
<module name="wolframalpha" apikey="YOUR-APIKEY" />
|
||||||
<wfaapi key="YOUR-APIKEY" />
|
|
||||||
</module>
|
|
||||||
-->
|
-->
|
||||||
|
|
||||||
<module name="cmd_server" />
|
<module name="cmd_server" />
|
||||||
|
|
|
@ -13,17 +13,6 @@ from more import Response
|
||||||
|
|
||||||
from . import DDGSearch
|
from . import DDGSearch
|
||||||
from . import UrbanDictionnary
|
from . import UrbanDictionnary
|
||||||
from . import WFASearch
|
|
||||||
|
|
||||||
def load(context):
|
|
||||||
WFASearch.CONF = context.config
|
|
||||||
|
|
||||||
def reload():
|
|
||||||
imp.reload(DDGSearch)
|
|
||||||
imp.reload(UrbanDictionnary)
|
|
||||||
imp.reload(WFASearch)
|
|
||||||
imp.reload(Wikipedia)
|
|
||||||
|
|
||||||
|
|
||||||
@hook("cmd_hook", "define")
|
@hook("cmd_hook", "define")
|
||||||
def define(msg):
|
def define(msg):
|
||||||
|
@ -76,22 +65,3 @@ def udsearch(msg):
|
||||||
res.append_message(d)
|
res.append_message(d)
|
||||||
|
|
||||||
return res
|
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)
|
|
||||||
|
|
|
@ -2,22 +2,32 @@
|
||||||
|
|
||||||
from urllib.parse import quote
|
from urllib.parse import quote
|
||||||
|
|
||||||
|
from nemubot import context
|
||||||
|
from nemubot.exception import IRCException
|
||||||
|
from nemubot.hooks import hook
|
||||||
from nemubot.tools import web
|
from nemubot.tools import web
|
||||||
|
|
||||||
|
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("%", "%%")
|
||||||
|
|
||||||
|
|
||||||
class WFASearch:
|
class WFASearch:
|
||||||
def __init__(self, terms):
|
def __init__(self, terms):
|
||||||
self.terms = terms
|
self.terms = terms
|
||||||
try:
|
self.wfares = web.getXML(URL_API % quote(terms))
|
||||||
url = ("http://api.wolframalpha.com/v2/query?input=%s&appid=%s" %
|
|
||||||
(quote(terms), CONF.getNode("wfaapi")["key"]))
|
|
||||||
self.wfares = web.getXML(url)
|
|
||||||
except (TypeError, KeyError):
|
|
||||||
print ("You need a Wolfram|Alpha API key in order to use this "
|
|
||||||
"module. Add it to the module configuration file:\n<wfaapi"
|
|
||||||
" key=\"XXXXXX-XXXXXXXXXX\" />\nRegister at "
|
|
||||||
"http://products.wolframalpha.com/api/")
|
|
||||||
self.wfares = None
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def success(self):
|
def success(self):
|
||||||
|
@ -69,3 +79,21 @@ class WFASearch:
|
||||||
subnode.getFirstNode("plaintext").getContent())
|
subnode.getFirstNode("plaintext").getContent())
|
||||||
except IndexError:
|
except IndexError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
@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)
|
Loading…
Add table
Add a link
Reference in a new issue