Browse Source

Merge pull request #478 from a01200356/master

Wolfram Alpha API
Adam Tauber 9 years ago
parent
commit
c350def84b
2 changed files with 68 additions and 0 deletions
  1. 60
    0
      searx/engines/wolframalpha_api.py
  2. 8
    0
      searx/settings.yml

+ 60
- 0
searx/engines/wolframalpha_api.py View File

@@ -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

+ 8
- 0
searx/settings.yml View File

@@ -300,6 +300,14 @@ engines:
300 300
     engine : vimeo
301 301
     shortcut : vm
302 302
 
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
+
303 311
 #The blekko technology and team have joined IBM Watson! -> https://blekko.com/
304 312
 #  - name : blekko images
305 313
 #    engine : blekko_images