on master help sign in
[enh] wolframalpha appends result
commit0871c7ca85cd19a2fa0971c7db28516a74255d5d authora01200356 <a01200356@itesm.mx> dateFri 1 Jan 2016, 22:02 (11 years ago) parentbe54e52
3 files changed+43−18
searx/engines/wolframalpha_api.py+20−8 searx/engines/wolframalpha_noapi.py+19−7 searx/settings.yml+4−3
searx/engines/wolframalpha_api.py+20−8
@@ -14,14 +14,24 @@ from lxml import etree
1414# search-url
1515base_url = 'http://api.wolframalpha.com/v2/query'
1616search_url = base_url + '?appid={api_key}&{query}&format=plaintext'
17+site_url = 'http://www.wolframalpha.com/input/?{query}'
18+search_query = ''
1719api_key = ''
1820
21+# xpath variables
22+failure_xpath = '/queryresult[attribute::success="false"]'
23+answer_xpath = '//pod[attribute::primary="true"]/subpod/plaintext'
24+
1925
2026# do search-request
2127def request(query, params):
2228 params['url'] = search_url.format(query=urlencode({'input': query}),
2329 api_key=api_key)
2430
31+ # used in response
32+ global search_query
33+ search_query = query
34+
2535 return params
2636
2737
@@ -45,19 +55,21 @@ def response(resp):
4555 search_results = etree.XML(resp.content)
4656
4757 # return empty array if there are no results
48− if search_results.xpath('/queryresult[attribute::success="false"]'):
58+ if search_results.xpath(failure_xpath):
4959 return []
5060
5161 # 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})
5567
56− answer = replace_pua_chars(answer[0].text)
68+ # result url
69+ result_url = site_url.format(query=urlencode({'i': search_query}))
5770
5871 # 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'})
6174
62− # return results
6375 return results
searx/engines/wolframalpha_noapi.py+19−7
@@ -1,6 +1,7 @@
11# WolframAlpha (Maths)
22#
33# @website http://www.wolframalpha.com/
4+# @provide-api yes (http://api.wolframalpha.com/v2/)
45#
56# @using-api no
67# @results HTML
@@ -14,12 +15,17 @@ from urllib import urlencode
1415# search-url
1516url = 'http://www.wolframalpha.com/'
1617search_url = url+'input/?{query}'
18+search_query = ''
1719
1820
1921# do search-request
2022def request(query, params):
2123 params['url'] = search_url.format(query=urlencode({'i': query}))
2224
25+ # used in response
26+ global search_query
27+ search_query = query
28+
2329 return params
2430
2531
@@ -42,14 +48,20 @@ def response(resp):
4248 except AttributeError:
4349 continue
4450
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})
4758
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
5262
53− results.append({'answer': answer})
63+ # append result
64+ results.append({'url': request(search_query, {})['url'],
65+ 'title': search_query + ' - Wolfram|Alpha'})
5466
5567 return results
searx/settings.yml+4−3
@@ -300,12 +300,13 @@ 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/
305303 - name : wolframalpha
306304 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!
307309 engine : wolframalpha_noapi
308−# api_key: 'apikey' # required!
309310 timeout: 6.0
310311
311312#The blekko technology and team have joined IBM Watson! -> https://blekko.com/