on master help sign in
Merge pull request #478 from a01200356/master

Wolfram Alpha API

commitc350def84bb5603f99859e3a5a9824cc356e613b authorAdam Tauber <asciimoo@gmail.com> dateMon 28 Dec 2015, 10:57 (11 years ago) parent91c850c0bb403b
2 files changed+68
searx/engines/wolframalpha_api.py+60 searx/settings.yml+8
searx/engines/wolframalpha_api.pynew file+60
@@ -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
searx/settings.yml+8
@@ -300,6 +300,14 @@ engines:
300300 engine : vimeo
301301 shortcut : vm
302302
303+# You can use the engine using the official stable API, but you need an API key
304+# See : http://products.wolframalpha.com/api/
305+# - name : wolframalpha
306+# shortcut : wa
307+# engine : wolframalpha_api
308+# api_key: 'apikey' # required!
309+# timeout: 6.0
310+
303311#The blekko technology and team have joined IBM Watson! -> https://blekko.com/
304312# - name : blekko images
305313# engine : blekko_images