Skip to content

Commit 1ebbcf2

Browse files
committed
mvp wolfram.py
1 parent ca20115 commit 1ebbcf2

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

nerdreply.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import sys
55
import codecs
66
import dice
7+
import wolfram
78
sys.stdout = codecs.getwriter('utf8')(sys.stdout)
89
import sys
910
reload(sys)
@@ -29,7 +30,8 @@ def handlers():
2930

3031
# Weather currently disabled due to UTF-8 issues.
3132
, Handler(".*!forecast.*\r\n", lambda m: weather.encoding(m))
32-
33+
, Handler(".*[tT]ellem.*\r\n", lambda m: wolfram.get_result(m))
34+
3335
#Droppin' bangers
3436
, Handler(".*banger count.*\r\n", lambda m: bangers.count())
3537
, Handler(".*banger add https.*\r\n", lambda m: bangers.add_banger(m))

wolfram.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import urllib
2+
import os
3+
from xml.etree import ElementTree as ET
4+
5+
wolfkey = os.environ["WOLFKEY"]
6+
url = "http://api.wolframalpha.com/v2/query?input="
7+
8+
#text = raw_input()
9+
10+
def wolf_answer(text):
11+
12+
tree = ET.parse(urllib.urlopen(url + text + "&appid=" + wolfkey))
13+
root = tree.getroot()
14+
pt_ans = {}
15+
for i in tree.findall('pod'):
16+
for g in [m for m in list(i) if m.tag=='subpod']:
17+
for x in [n for n in list(g) if n.tag=='plaintext']:
18+
if x.tag=='plaintext':
19+
pt_ans[i.get('title')] = x.text
20+
21+
return pt_ans
22+
23+
def get_result(text):
24+
result = wolf_answer(text)
25+
if 'Result' in result:
26+
return str(result['Result'])
27+
else:
28+
return "Please try another question."
29+
30+
#print (get_result(text))
31+
32+
##math is still broken, need to add support for multiple results, and update test_brobot

0 commit comments

Comments
 (0)