Browse Source

[enh] wolframalpha appends result

a01200356 9 years ago
parent
commit
0871c7ca85
3 changed files with 43 additions and 18 deletions
  1. 20
    8
      searx/engines/wolframalpha_api.py
  2. 19
    7
      searx/engines/wolframalpha_noapi.py
  3. 4
    3
      searx/settings.yml

+ 20
- 8
searx/engines/wolframalpha_api.py View File

@@ -14,14 +14,24 @@ from lxml import etree
14 14
 # search-url
15 15
 base_url = 'http://api.wolframalpha.com/v2/query'
16 16
 search_url = base_url + '?appid={api_key}&{query}&format=plaintext'
17
+site_url = 'http://www.wolframalpha.com/input/?{query}'
18
+search_query = ''
17 19
 api_key = ''
18 20
 
21
+# xpath variables
22
+failure_xpath = '/queryresult[attribute::success="false"]'
23
+answer_xpath = '//pod[attribute::primary="true"]/subpod/plaintext'
24
+
19 25
 
20 26
 # do search-request
21 27
 def request(query, params):
22 28
     params['url'] = search_url.format(query=urlencode({'input': query}),
23 29
                                       api_key=api_key)
24 30
 
31
+    # used in response
32
+    global search_query
33
+    search_query = query
34
+
25 35
     return params
26 36
 
27 37
 
@@ -45,19 +55,21 @@ def response(resp):
45 55
     search_results = etree.XML(resp.content)
46 56
 
47 57
     # return empty array if there are no results
48
-    if search_results.xpath('/queryresult[attribute::success="false"]'):
58
+    if search_results.xpath(failure_xpath):
49 59
         return []
50 60
 
51 61
     # parse answer
52
-    answer = search_results.xpath('//pod[attribute::primary="true"]/subpod/plaintext')
53
-    if not answer:
54
-        return results
62
+    answer = search_results.xpath(answer_xpath)
63
+    if answer:
64
+        answer = replace_pua_chars(answer[0].text)
65
+
66
+        results.append({'answer': answer})
55 67
 
56
-    answer = replace_pua_chars(answer[0].text)
68
+    # result url
69
+    result_url = site_url.format(query=urlencode({'i': search_query}))
57 70
 
58 71
     # append result
59
-    # TODO: shouldn't it bind the source too?
60
-    results.append({'answer': answer})
72
+    results.append({'url': result_url,
73
+                    'title': search_query + ' - Wolfram|Alpha'})
61 74
 
62
-    # return results
63 75
     return results

+ 19
- 7
searx/engines/wolframalpha_noapi.py View File

@@ -1,6 +1,7 @@
1 1
 # WolframAlpha (Maths)
2 2
 #
3 3
 # @website     http://www.wolframalpha.com/
4
+# @provide-api yes (http://api.wolframalpha.com/v2/)
4 5
 #
5 6
 # @using-api   no
6 7
 # @results     HTML
@@ -14,12 +15,17 @@ from urllib import urlencode
14 15
 # search-url
15 16
 url = 'http://www.wolframalpha.com/'
16 17
 search_url = url+'input/?{query}'
18
+search_query = ''
17 19
 
18 20
 
19 21
 # do search-request
20 22
 def request(query, params):
21 23
     params['url'] = search_url.format(query=urlencode({'i': query}))
22 24
 
25
+    # used in response
26
+    global search_query
27
+    search_query = query
28
+
23 29
     return params
24 30
 
25 31
 
@@ -42,14 +48,20 @@ def response(resp):
42 48
         except AttributeError:
43 49
             continue
44 50
 
45
-    if not line:
46
-        return results
51
+    if line:
52
+        # extract answer from json
53
+        answer = line[line.find('{'):line.rfind('}')+1]
54
+        answer = loads(answer.encode('unicode-escape'))
55
+        answer = answer['stringified'].decode('unicode-escape')
56
+        
57
+        results.append({'answer': answer})
47 58
 
48
-    # extract answer from json
49
-    answer = line[line.find('{'):line.rfind('}')+1]
50
-    answer = loads(answer.encode('unicode-escape'))
51
-    answer = answer['stringified'].decode('unicode-escape')
59
+    # failed result
60
+    elif search('pfail', webpage):
61
+        return results
52 62
 
53
-    results.append({'answer': answer})
63
+    # append result
64
+    results.append({'url': request(search_query, {})['url'],
65
+                    'title': search_query + ' - Wolfram|Alpha'})
54 66
 
55 67
     return results

+ 4
- 3
searx/settings.yml View File

@@ -300,12 +300,13 @@ 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 303
   - name : wolframalpha
306 304
     shortcut : wa
305
+    # You can use the engine using the official stable API, but you need an API key
306
+    # See : http://products.wolframalpha.com/api/
307
+    #    engine : wolframalpha_api
308
+    #    api_key: 'api_key' # required!
307 309
     engine : wolframalpha_noapi
308
-#    api_key: 'apikey' # required!
309 310
     timeout: 6.0
310 311
 
311 312
 #The blekko technology and team have joined IBM Watson! -> https://blekko.com/