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
 # search-url
14
 # search-url
15
 base_url = 'http://api.wolframalpha.com/v2/query'
15
 base_url = 'http://api.wolframalpha.com/v2/query'
16
 search_url = base_url + '?appid={api_key}&{query}&format=plaintext'
16
 search_url = base_url + '?appid={api_key}&{query}&format=plaintext'
17
+site_url = 'http://www.wolframalpha.com/input/?{query}'
18
+search_query = ''
17
 api_key = ''
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
 # do search-request
26
 # do search-request
21
 def request(query, params):
27
 def request(query, params):
22
     params['url'] = search_url.format(query=urlencode({'input': query}),
28
     params['url'] = search_url.format(query=urlencode({'input': query}),
23
                                       api_key=api_key)
29
                                       api_key=api_key)
24
 
30
 
31
+    # used in response
32
+    global search_query
33
+    search_query = query
34
+
25
     return params
35
     return params
26
 
36
 
27
 
37
 
45
     search_results = etree.XML(resp.content)
55
     search_results = etree.XML(resp.content)
46
 
56
 
47
     # return empty array if there are no results
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
         return []
59
         return []
50
 
60
 
51
     # parse answer
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
     # append result
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
     return results
75
     return results

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

1
 # WolframAlpha (Maths)
1
 # WolframAlpha (Maths)
2
 #
2
 #
3
 # @website     http://www.wolframalpha.com/
3
 # @website     http://www.wolframalpha.com/
4
+# @provide-api yes (http://api.wolframalpha.com/v2/)
4
 #
5
 #
5
 # @using-api   no
6
 # @using-api   no
6
 # @results     HTML
7
 # @results     HTML
14
 # search-url
15
 # search-url
15
 url = 'http://www.wolframalpha.com/'
16
 url = 'http://www.wolframalpha.com/'
16
 search_url = url+'input/?{query}'
17
 search_url = url+'input/?{query}'
18
+search_query = ''
17
 
19
 
18
 
20
 
19
 # do search-request
21
 # do search-request
20
 def request(query, params):
22
 def request(query, params):
21
     params['url'] = search_url.format(query=urlencode({'i': query}))
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
     return params
29
     return params
24
 
30
 
25
 
31
 
42
         except AttributeError:
48
         except AttributeError:
43
             continue
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
     return results
67
     return results

+ 4
- 3
searx/settings.yml View File

300
     engine : vimeo
300
     engine : vimeo
301
     shortcut : vm
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
303
   - name : wolframalpha
306
     shortcut : wa
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
     engine : wolframalpha_noapi
309
     engine : wolframalpha_noapi
308
-#    api_key: 'apikey' # required!
309
     timeout: 6.0
310
     timeout: 6.0
310
 
311
 
311
 #The blekko technology and team have joined IBM Watson! -> https://blekko.com/
312
 #The blekko technology and team have joined IBM Watson! -> https://blekko.com/