From 4998e9ec856479d0c619e54f7100c295c7c5851c Mon Sep 17 00:00:00 2001 From: Markus Heiser Date: Fri, 29 Nov 2019 18:56:29 +0100 Subject: [PATCH 1/3] [fix] duckduckgo_definitions - where 'AnswerType' is 'calc' Do not try to get text when 'AnswerType' is 'calc'. Signed-off-by: Markus Heiser --- searx/engines/duckduckgo_definitions.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/searx/engines/duckduckgo_definitions.py b/searx/engines/duckduckgo_definitions.py index 957a13ea..2899b50f 100644 --- a/searx/engines/duckduckgo_definitions.py +++ b/searx/engines/duckduckgo_definitions.py @@ -1,11 +1,25 @@ +""" +DuckDuckGo (definitions) + +- `Instant Answer API`_ +- `DuckDuckGo query`_ + +.. _Instant Answer API: https://duckduckgo.com/api +.. _DuckDuckGo query: https://api.duckduckgo.com/?q=DuckDuckGo&format=json&pretty=1 + +""" + import json from lxml import html from re import compile +import logging from searx.engines.xpath import extract_text from searx.engines.duckduckgo import _fetch_supported_languages, supported_languages_url, language_aliases from searx.url_utils import urlencode from searx.utils import html_to_text, match_language +logger = logging.getLogger('searx.engines.'+ __name__) + url = 'https://api.duckduckgo.com/'\ + '?{query}&format=json&pretty=0&no_redirect=1&d=1' @@ -25,7 +39,9 @@ def result_to_text(url, text, htmlResult): def request(query, params): params['url'] = url.format(query=urlencode({'q': query})) language = match_language(params['language'], supported_languages, language_aliases) - params['headers']['Accept-Language'] = language.split('-')[0] + language = language.split('-')[0] + params['headers']['Accept-Language'] = language + logger.debug("query %s: // headers: %s", params['url'], params['headers']) return params @@ -43,8 +59,9 @@ def response(resp): # add answer if there is one answer = search_res.get('Answer', '') - if answer != '': - results.append({'answer': html_to_text(answer)}) + if answer: + if search_res.get('AnswerType', '') not in ['calc']: + results.append({'answer': html_to_text(answer)}) # add infobox if 'Definition' in search_res: From b6d9f5aa71de43ff9a46f0d4d94baad429e2275d Mon Sep 17 00:00:00 2001 From: Markus Heiser Date: Fri, 29 Nov 2019 19:55:16 +0100 Subject: [PATCH 2/3] [fix] duckduckgo_definition issues reported by 'manage.sh test' Fix this error while travis build:: /home/travis/build/asciimoo/searx/searx/engines/duckduckgo_definitions.py:21:44: E225 missing whitespace around operator Signed-off-by: Markus Heiser --- searx/engines/duckduckgo_definitions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/searx/engines/duckduckgo_definitions.py b/searx/engines/duckduckgo_definitions.py index 2899b50f..49def2a2 100644 --- a/searx/engines/duckduckgo_definitions.py +++ b/searx/engines/duckduckgo_definitions.py @@ -18,7 +18,7 @@ from searx.engines.duckduckgo import _fetch_supported_languages, supported_langu from searx.url_utils import urlencode from searx.utils import html_to_text, match_language -logger = logging.getLogger('searx.engines.'+ __name__) +logger = logging.getLogger('searx.engines.' + __name__) url = 'https://api.duckduckgo.com/'\ + '?{query}&format=json&pretty=0&no_redirect=1&d=1' From 30ad0c666df6644c4d05949f7bc5d7b1485f3fd6 Mon Sep 17 00:00:00 2001 From: Markus Heiser Date: Mon, 2 Dec 2019 14:26:11 +0100 Subject: [PATCH 3/3] duckduckgo_definitions: remove the debug message Signed-off-by: Markus Heiser --- searx/engines/duckduckgo_definitions.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/searx/engines/duckduckgo_definitions.py b/searx/engines/duckduckgo_definitions.py index 49def2a2..79d10c30 100644 --- a/searx/engines/duckduckgo_definitions.py +++ b/searx/engines/duckduckgo_definitions.py @@ -12,14 +12,11 @@ DuckDuckGo (definitions) import json from lxml import html from re import compile -import logging from searx.engines.xpath import extract_text from searx.engines.duckduckgo import _fetch_supported_languages, supported_languages_url, language_aliases from searx.url_utils import urlencode from searx.utils import html_to_text, match_language -logger = logging.getLogger('searx.engines.' + __name__) - url = 'https://api.duckduckgo.com/'\ + '?{query}&format=json&pretty=0&no_redirect=1&d=1' @@ -41,7 +38,6 @@ def request(query, params): language = match_language(params['language'], supported_languages, language_aliases) language = language.split('-')[0] params['headers']['Accept-Language'] = language - logger.debug("query %s: // headers: %s", params['url'], params['headers']) return params