diff --git a/searx/engines/wolframalpha_api.py b/searx/engines/wolframalpha_api.py index 4c99eac95..6927f9707 100644 --- a/searx/engines/wolframalpha_api.py +++ b/searx/engines/wolframalpha_api.py @@ -14,14 +14,24 @@ from lxml import etree # search-url base_url = 'http://api.wolframalpha.com/v2/query' search_url = base_url + '?appid={api_key}&{query}&format=plaintext' +site_url = 'http://www.wolframalpha.com/input/?{query}' +search_query = '' api_key = '' +# xpath variables +failure_xpath = '/queryresult[attribute::success="false"]' +answer_xpath = '//pod[attribute::primary="true"]/subpod/plaintext' + # do search-request def request(query, params): params['url'] = search_url.format(query=urlencode({'input': query}), api_key=api_key) + # used in response + global search_query + search_query = query + return params @@ -45,19 +55,21 @@ def response(resp): search_results = etree.XML(resp.content) # return empty array if there are no results - if search_results.xpath('/queryresult[attribute::success="false"]'): + if search_results.xpath(failure_xpath): return [] # parse answer - answer = search_results.xpath('//pod[attribute::primary="true"]/subpod/plaintext') - if not answer: - return results + answer = search_results.xpath(answer_xpath) + if answer: + answer = replace_pua_chars(answer[0].text) - answer = replace_pua_chars(answer[0].text) + results.append({'answer': answer}) + + # result url + result_url = site_url.format(query=urlencode({'i': search_query})) # append result - # TODO: shouldn't it bind the source too? - results.append({'answer': answer}) + results.append({'url': result_url, + 'title': search_query + ' - Wolfram|Alpha'}) - # return results return results diff --git a/searx/engines/wolframalpha_noapi.py b/searx/engines/wolframalpha_noapi.py index 9d3afe658..89a3c45b5 100644 --- a/searx/engines/wolframalpha_noapi.py +++ b/searx/engines/wolframalpha_noapi.py @@ -1,6 +1,7 @@ # WolframAlpha (Maths) # # @website http://www.wolframalpha.com/ +# @provide-api yes (http://api.wolframalpha.com/v2/) # # @using-api no # @results HTML @@ -14,12 +15,17 @@ from urllib import urlencode # search-url url = 'http://www.wolframalpha.com/' search_url = url+'input/?{query}' +search_query = '' # do search-request def request(query, params): params['url'] = search_url.format(query=urlencode({'i': query})) + # used in response + global search_query + search_query = query + return params @@ -42,14 +48,20 @@ def response(resp): except AttributeError: continue - if not line: + if line: + # extract answer from json + answer = line[line.find('{'):line.rfind('}')+1] + answer = loads(answer.encode('unicode-escape')) + answer = answer['stringified'].decode('unicode-escape') + + results.append({'answer': answer}) + + # failed result + elif search('pfail', webpage): return results - # extract answer from json - answer = line[line.find('{'):line.rfind('}')+1] - answer = loads(answer.encode('unicode-escape')) - answer = answer['stringified'].decode('unicode-escape') - - results.append({'answer': answer}) + # append result + results.append({'url': request(search_query, {})['url'], + 'title': search_query + ' - Wolfram|Alpha'}) return results diff --git a/searx/settings.yml b/searx/settings.yml index d2a724118..510fc028c 100644 --- a/searx/settings.yml +++ b/searx/settings.yml @@ -300,12 +300,13 @@ engines: engine : vimeo shortcut : vm -# You can use the engine using the official stable API, but you need an API key -# See : http://products.wolframalpha.com/api/ - name : wolframalpha shortcut : wa + # You can use the engine using the official stable API, but you need an API key + # See : http://products.wolframalpha.com/api/ + # engine : wolframalpha_api + # api_key: 'api_key' # required! engine : wolframalpha_noapi -# api_key: 'apikey' # required! timeout: 6.0 #The blekko technology and team have joined IBM Watson! -> https://blekko.com/