diff options
| author | Adam Tauber <asciimoo@gmail.com> | 2016-01-19 17:02:14 +0100 |
|---|---|---|
| committer | Adam Tauber <asciimoo@gmail.com> | 2016-01-19 17:02:14 +0100 |
| commit | b5a3dfca60f23bac10ade068c40729f030bbad63 (patch) | |
| tree | da3142cd5f1c1d05b922be2d1e6aaaf029fa8d8d /searx/engines/wolframalpha_api.py | |
| parent | 09b7673fbd271349b6878959bd2e1ae846981e13 (diff) | |
| parent | 30bfbf2e07def8911d0b293e8032699812f43599 (diff) | |
Merge pull request #486 from a01200356/master
[enh] WolframAlpha no API engine (and tests for both)
Diffstat (limited to 'searx/engines/wolframalpha_api.py')
| -rw-r--r-- | searx/engines/wolframalpha_api.py | 35 |
1 files changed, 26 insertions, 9 deletions
diff --git a/searx/engines/wolframalpha_api.py b/searx/engines/wolframalpha_api.py index d61d25747..303c6c165 100644 --- a/searx/engines/wolframalpha_api.py +++ b/searx/engines/wolframalpha_api.py @@ -10,11 +10,18 @@ from urllib import urlencode from lxml import etree +from re import search # search-url base_url = 'http://api.wolframalpha.com/v2/query' search_url = base_url + '?appid={api_key}&{query}&format=plaintext' -api_key = '' +site_url = 'http://www.wolframalpha.com/input/?{query}' +api_key = '' # defined in settings.yml + +# xpath variables +failure_xpath = '/queryresult[attribute::success="false"]' +answer_xpath = '//pod[attribute::primary="true"]/subpod/plaintext' +input_xpath = '//pod[starts-with(attribute::title, "Input")]/subpod/plaintext' # do search-request @@ -45,16 +52,26 @@ 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 result - result = search_results.xpath('//pod[attribute::primary="true"]/subpod/plaintext')[0].text - result = replace_pua_chars(result) + # parse answers + answers = search_results.xpath(answer_xpath) + if answers: + for answer in answers: + answer = replace_pua_chars(answer.text) + + results.append({'answer': answer}) + + # if there's no input section in search_results, check if answer has the input embedded (before their "=" sign) + try: + query_input = search_results.xpath(input_xpath)[0].text + except IndexError: + query_input = search(u'([^\uf7d9]+)', answers[0].text).group(1) - # append result - # TODO: shouldn't it bind the source too? - results.append({'answer': result}) + # append link to site + result_url = site_url.format(query=urlencode({'i': query_input.encode('utf-8')})) + results.append({'url': result_url, + 'title': query_input + " - Wolfram|Alpha"}) - # return results return results |