From e4dd75070f09223437ce48f8dfeeafae755613eb Mon Sep 17 00:00:00 2001 From: a01200356 Date: Mon, 28 Dec 2015 01:27:19 -0600 Subject: Pinche Travis. --- searx/engines/wolframalpha_api.py | 2 -- 1 file changed, 2 deletions(-) (limited to 'searx/engines/wolframalpha_api.py') diff --git a/searx/engines/wolframalpha_api.py b/searx/engines/wolframalpha_api.py index 309608628..d61d25747 100644 --- a/searx/engines/wolframalpha_api.py +++ b/searx/engines/wolframalpha_api.py @@ -10,8 +10,6 @@ from urllib import urlencode from lxml import etree -from searx.engines.xpath import extract_text -from searx.utils import html_to_text # search-url base_url = 'http://api.wolframalpha.com/v2/query' -- cgit v1.2.3 From be54e5269a982e272e2fe8a5064ed898373c9063 Mon Sep 17 00:00:00 2001 From: a01200356 Date: Wed, 30 Dec 2015 00:53:15 -0600 Subject: Add tests for the Wolfram Alpha engines (both API and NO API versions) --- searx/engines/wolframalpha_api.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'searx/engines/wolframalpha_api.py') diff --git a/searx/engines/wolframalpha_api.py b/searx/engines/wolframalpha_api.py index d61d25747..4c99eac95 100644 --- a/searx/engines/wolframalpha_api.py +++ b/searx/engines/wolframalpha_api.py @@ -48,13 +48,16 @@ def response(resp): if search_results.xpath('/queryresult[attribute::success="false"]'): return [] - # parse result - result = search_results.xpath('//pod[attribute::primary="true"]/subpod/plaintext')[0].text - result = replace_pua_chars(result) + # parse answer + answer = search_results.xpath('//pod[attribute::primary="true"]/subpod/plaintext') + if not answer: + return results + + answer = replace_pua_chars(answer[0].text) # append result # TODO: shouldn't it bind the source too? - results.append({'answer': result}) + results.append({'answer': answer}) # return results return results -- cgit v1.2.3 From 0871c7ca85cd19a2fa0971c7db28516a74255d5d Mon Sep 17 00:00:00 2001 From: a01200356 Date: Fri, 1 Jan 2016 22:02:10 -0600 Subject: [enh] wolframalpha appends result --- searx/engines/wolframalpha_api.py | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) (limited to 'searx/engines/wolframalpha_api.py') 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) + + results.append({'answer': answer}) - answer = replace_pua_chars(answer[0].text) + # 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 -- cgit v1.2.3 From e9d35c1309f05a0b214fb323049909ee7ec62ab8 Mon Sep 17 00:00:00 2001 From: a01200356 Date: Sat, 2 Jan 2016 00:41:14 -0600 Subject: update tests for wolframalpha --- searx/engines/wolframalpha_api.py | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) (limited to 'searx/engines/wolframalpha_api.py') diff --git a/searx/engines/wolframalpha_api.py b/searx/engines/wolframalpha_api.py index 6927f9707..d4127be4c 100644 --- a/searx/engines/wolframalpha_api.py +++ b/searx/engines/wolframalpha_api.py @@ -14,9 +14,7 @@ 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 = '' +api_key = '' # defined in settings.yml # xpath variables failure_xpath = '/queryresult[attribute::success="false"]' @@ -28,10 +26,6 @@ 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 @@ -65,11 +59,6 @@ def response(resp): results.append({'answer': answer}) - # result url - result_url = site_url.format(query=urlencode({'i': search_query})) - - # append result - results.append({'url': result_url, - 'title': search_query + ' - Wolfram|Alpha'}) + # TODO: append a result with title and link, like in the no api version return results -- cgit v1.2.3 From d05c676ed5b1dc5372b1cb380740161b3613f7cc Mon Sep 17 00:00:00 2001 From: a01200356 Date: Sat, 2 Jan 2016 22:29:20 -0600 Subject: Add test case in wolframalpha_noapi [fix] Display multiple answers in wolframalpha_api --- searx/engines/wolframalpha_api.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'searx/engines/wolframalpha_api.py') diff --git a/searx/engines/wolframalpha_api.py b/searx/engines/wolframalpha_api.py index d4127be4c..540d81351 100644 --- a/searx/engines/wolframalpha_api.py +++ b/searx/engines/wolframalpha_api.py @@ -52,12 +52,13 @@ def response(resp): if search_results.xpath(failure_xpath): return [] - # parse answer - answer = search_results.xpath(answer_xpath) - if answer: - answer = replace_pua_chars(answer[0].text) + # parse answers + answers = search_results.xpath(answer_xpath) + if answers: + for answer in answers: + answer = replace_pua_chars(answer.text) - results.append({'answer': answer}) + results.append({'answer': answer}) # TODO: append a result with title and link, like in the no api version -- cgit v1.2.3 From 8ca574481485847d5e0f47627d20c543c39b7b66 Mon Sep 17 00:00:00 2001 From: a01200356 Date: Tue, 5 Jan 2016 21:47:31 -0600 Subject: append link to result in wolframalpha_api (and the tests to validate that) --- searx/engines/wolframalpha_api.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'searx/engines/wolframalpha_api.py') diff --git a/searx/engines/wolframalpha_api.py b/searx/engines/wolframalpha_api.py index 540d81351..303c6c165 100644 --- a/searx/engines/wolframalpha_api.py +++ b/searx/engines/wolframalpha_api.py @@ -10,15 +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' +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 @@ -60,6 +63,15 @@ def response(resp): results.append({'answer': answer}) - # TODO: append a result with title and link, like in the no api version + # 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 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 -- cgit v1.2.3