switch to using the full wolfram alpha API
This commit is contained in:
parent
5a2905de01
commit
1b9a26919d
1 changed files with 17 additions and 7 deletions
|
@ -4,7 +4,7 @@
|
||||||
import json
|
import json
|
||||||
from src import ModuleManager, utils
|
from src import ModuleManager, utils
|
||||||
|
|
||||||
URL_WA = "https://api.wolframalpha.com/v1/result"
|
URL_WA = "https://api.wolframalpha.com/v2/query"
|
||||||
|
|
||||||
class Module(ModuleManager.BaseModule):
|
class Module(ModuleManager.BaseModule):
|
||||||
_name = "Wolfram|Alpha"
|
_name = "Wolfram|Alpha"
|
||||||
|
@ -16,17 +16,27 @@ class Module(ModuleManager.BaseModule):
|
||||||
:help: Evaluate a given string on Wolfram|Alpha
|
:help: Evaluate a given string on Wolfram|Alpha
|
||||||
:usage: <query>
|
:usage: <query>
|
||||||
"""
|
"""
|
||||||
|
query = event["args"].strip()
|
||||||
try:
|
try:
|
||||||
page = utils.http.request(URL_WA,
|
page = utils.http.request(URL_WA, timeout=10, get_params={
|
||||||
get_params={"i": event["args"],
|
"input": query, "format": "plaintext",
|
||||||
"appid": self.bot.config["wolframalpha-api-key"],
|
"output": "JSON", "reinterpret": "true", "units": "metric",
|
||||||
"reinterpret": "true", "units": "metric"})
|
"appid": self.bot.config["wolframalpha-api-key"]}).json()
|
||||||
except utils.http.HTTPTimeoutException:
|
except utils.http.HTTPTimeoutException:
|
||||||
page = None
|
page = None
|
||||||
|
|
||||||
if page:
|
if page:
|
||||||
if page.code == 200:
|
if page["queryresult"]["numpods"]:
|
||||||
event["stdout"].write("%s: %s" % (event["args"], page.decode()))
|
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)))
|
||||||
else:
|
else:
|
||||||
event["stdout"].write("No results")
|
event["stdout"].write("No results")
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Reference in a new issue