@@ -0,0 +1,60 @@
1+# Wolfram Alpha (Maths)
2+#
3+# @website http://www.wolframalpha.com
4+# @provide-api yes (http://api.wolframalpha.com/v2/)
5+#
6+# @using-api yes
7+# @results XML
8+# @stable yes
9+# @parse result
10+
11+from urllib import urlencode
12+from lxml import etree
13+
14+# search-url
15+base_url = 'http://api.wolframalpha.com/v2/query'
16+search_url = base_url + '?appid={api_key}&{query}&format=plaintext'
17+api_key = ''
18+
19+
20+# do search-request
21+def request(query, params):
22+ params['url'] = search_url.format(query=urlencode({'input': query}),
23+ api_key=api_key)
24+
25+ return params
26+
27+
28+# replace private user area characters to make text legible
29+def replace_pua_chars(text):
30+ pua_chars = {u'\uf74c': 'd',
31+ u'\uf74d': u'\u212f',
32+ u'\uf74e': 'i',
33+ u'\uf7d9': '='}
34+
35+ for k, v in pua_chars.iteritems():
36+ text = text.replace(k, v)
37+
38+ return text
39+
40+
41+# get response from search-request
42+def response(resp):
43+ results = []
44+
45+ search_results = etree.XML(resp.content)
46+
47+ # return empty array if there are no results
48+ if search_results.xpath('/queryresult[attribute::success="false"]'):
49+ return []
50+
51+ # parse result
52+ result = search_results.xpath('//pod[attribute::primary="true"]/subpod/plaintext')[0].text
53+ result = replace_pua_chars(result)
54+
55+ # append result
56+ # TODO: shouldn't it bind the source too?
57+ results.append({'answer': result})
58+
59+ # return results
60+ return results