2019-05-25 20:40:06 +00:00
|
|
|
#--depends-on commands
|
2016-03-30 18:31:23 +00:00
|
|
|
#--require-config wolframalpha-api-key
|
2019-05-25 20:40:06 +00:00
|
|
|
|
2018-09-02 08:28:55 +00:00
|
|
|
import json
|
2018-10-03 12:22:37 +00:00
|
|
|
from src import ModuleManager, utils
|
2016-03-29 11:56:58 +00:00
|
|
|
|
2019-12-11 11:18:36 +00:00
|
|
|
URL_WA = "https://api.wolframalpha.com/v2/query"
|
2016-03-29 11:56:58 +00:00
|
|
|
|
2018-09-27 10:46:10 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
2016-03-29 11:56:58 +00:00
|
|
|
_name = "Wolfram|Alpha"
|
|
|
|
|
2018-10-10 09:42:41 +00:00
|
|
|
@utils.hook("received.command.wa", alias_of="wolframalpha")
|
|
|
|
@utils.hook("received.command.wolframalpha", min_args=1)
|
2016-03-29 11:56:58 +00:00
|
|
|
def wa(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2019-10-01 22:37:59 +00:00
|
|
|
:help: Evaluate a given string on Wolfram|Alpha
|
2018-09-30 16:29:09 +00:00
|
|
|
:usage: <query>
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2019-12-11 11:18:36 +00:00
|
|
|
query = event["args"].strip()
|
2019-01-20 19:04:32 +00:00
|
|
|
try:
|
2019-12-11 11:18:36 +00:00
|
|
|
page = utils.http.request(URL_WA, timeout=10, get_params={
|
|
|
|
"input": query, "format": "plaintext",
|
|
|
|
"output": "JSON", "reinterpret": "true", "units": "metric",
|
|
|
|
"appid": self.bot.config["wolframalpha-api-key"]}).json()
|
2019-01-20 19:05:50 +00:00
|
|
|
except utils.http.HTTPTimeoutException:
|
2019-01-20 19:04:32 +00:00
|
|
|
page = None
|
2016-03-29 11:56:58 +00:00
|
|
|
|
2018-12-11 22:26:38 +00:00
|
|
|
if page:
|
2019-12-11 11:18:36 +00:00
|
|
|
if page["queryresult"]["numpods"]:
|
|
|
|
input = query
|
|
|
|
primaries = []
|
|
|
|
for pod in page["queryresult"]["pods"]:
|
|
|
|
text = pod["subpods"][0]["plaintext"]
|
|
|
|
if pod["id"] == "Input" and text:
|
|
|
|
input = text
|
|
|
|
elif pod.get("primary", False):
|
|
|
|
primaries.append(text)
|
|
|
|
|
|
|
|
event["stdout"].write("%s: %s" % (input, " | ".join(primaries)))
|
2016-03-29 11:56:58 +00:00
|
|
|
else:
|
2018-09-02 08:28:55 +00:00
|
|
|
event["stdout"].write("No results")
|
2016-03-29 11:56:58 +00:00
|
|
|
else:
|
2019-11-18 12:06:59 +00:00
|
|
|
raise utils.EventResultsError()
|