change wolframalpha.py to use the "short answer" api
This commit is contained in:
parent
580b3963ce
commit
cc1b4e6fde
1 changed files with 8 additions and 30 deletions
|
@ -1,10 +1,8 @@
|
||||||
#--require-config wolframalpha-api-key
|
#--require-config wolframalpha-api-key
|
||||||
|
import json
|
||||||
import re
|
|
||||||
import Utils
|
import Utils
|
||||||
|
|
||||||
URL_WA = "http://api.wolframalpha.com/v2/query"
|
URL_WA = "https://api.wolframalpha.com/v1/result"
|
||||||
REGEX_CHARHEX = re.compile("\\\\:(\S{4})")
|
|
||||||
|
|
||||||
class Module(object):
|
class Module(object):
|
||||||
_name = "Wolfram|Alpha"
|
_name = "Wolfram|Alpha"
|
||||||
|
@ -16,34 +14,14 @@ class Module(object):
|
||||||
usage="<query>")
|
usage="<query>")
|
||||||
|
|
||||||
def wa(self, event):
|
def wa(self, event):
|
||||||
soup = Utils.get_url(URL_WA, get_params={"input": event["args"],
|
code, result = Utils.get_url(URL_WA, get_params={"i": event["args"],
|
||||||
"appid": self.bot.config["wolframalpha-api-key"],
|
"appid": self.bot.config["wolframalpha-api-key"],
|
||||||
"format": "plaintext", "reinterpret": "true"}, soup=True)
|
"reinterpret": "true", "units": "metric"}, code=True)
|
||||||
|
|
||||||
if soup:
|
if not result == None:
|
||||||
if int(soup.find("queryresult").get("numpods")) > 0:
|
if code == 200:
|
||||||
input = soup.find(id="Input").find("subpod").find("plaintext"
|
event["stdout"].write("%s: %s" % (event["args"], result))
|
||||||
).text
|
|
||||||
answered = False
|
|
||||||
for pod in soup.find_all("pod"):
|
|
||||||
if pod.get("primary") == "true":
|
|
||||||
answer = pod.find("subpod").find("plaintext")
|
|
||||||
text = "(%s) %s" % (input.replace(" | ", ": "),
|
|
||||||
answer.text.strip().replace(" | ", ": "
|
|
||||||
).replace("\n", " | ").replace("\r", ""))
|
|
||||||
while True:
|
|
||||||
match = re.search(REGEX_CHARHEX, text)
|
|
||||||
if match:
|
|
||||||
text = re.sub(REGEX_CHARHEX, chr(int(
|
|
||||||
match.group(1), 16)), text)
|
|
||||||
else:
|
else:
|
||||||
break
|
event["stdout"].write("No results")
|
||||||
answered = True
|
|
||||||
event["stdout"].write(text)
|
|
||||||
break
|
|
||||||
if not answered:
|
|
||||||
event["stderr"].write("No results found")
|
|
||||||
else:
|
|
||||||
event["stderr"].write("No results found")
|
|
||||||
else:
|
else:
|
||||||
event["stderr"].write("Failed to load results")
|
event["stderr"].write("Failed to load results")
|
||||||
|
|
Loading…
Reference in a new issue