diff options
Diffstat (limited to 'searx/engines')
| -rw-r--r-- | searx/engines/deepl.py | 62 | ||||
| -rw-r--r-- | searx/engines/xpath.py | 18 |
2 files changed, 79 insertions, 1 deletions
diff --git a/searx/engines/deepl.py b/searx/engines/deepl.py new file mode 100644 index 000000000..85072710f --- /dev/null +++ b/searx/engines/deepl.py @@ -0,0 +1,62 @@ +# SPDX-License-Identifier: AGPL-3.0-or-later +# lint: pylint +"""Deepl translation engine""" + +from json import loads + +about = { + "website": 'https://deepl.com', + "wikidata_id": 'Q43968444', + "official_api_documentation": 'https://www.deepl.com/docs-api', + "use_official_api": True, + "require_api_key": True, + "results": 'JSON', +} + +engine_type = 'online_dictionary' +categories = ['general'] + +url = 'https://api-free.deepl.com/v2/translate' +api_key = None + + +def request(_query, params): + '''pre-request callback + + params<dict>: + + - ``method`` : POST/GET + - ``headers``: {} + - ``data``: {} # if method == POST + - ``url``: '' + - ``category``: 'search category' + - ``pageno``: 1 # number of the requested page + ''' + + params['url'] = url + params['method'] = 'POST' + params['data'] = {'auth_key': api_key, 'text': params['query'], 'target_lang': params['to_lang'][1]} + + return params + + +def response(resp): + results = [] + result = loads(resp.text) + translations = result['translations'] + + infobox = "<dl>" + + for translation in translations: + infobox += f"<dd>{translation['text']}</dd>" + + infobox += "</dl>" + + results.append( + { + 'infobox': 'Deepl', + 'content': infobox, + } + ) + + return results diff --git a/searx/engines/xpath.py b/searx/engines/xpath.py index 705a5211d..f9528e92d 100644 --- a/searx/engines/xpath.py +++ b/searx/engines/xpath.py @@ -22,6 +22,7 @@ from urllib.parse import urlencode from lxml import html from searx.utils import extract_text, extract_url, eval_xpath, eval_xpath_list +from searx.network import raise_for_httperror search_url = None """ @@ -61,6 +62,14 @@ lang_all = 'en' selected. ''' +no_result_for_http_status = [] +'''Return empty result for these HTTP status codes instead of throwing an error. + +.. code:: yaml + + no_result_for_http_status: [] +''' + soft_max_redirects = 0 '''Maximum redirects, soft limit. Record an error but don't stop the engine''' @@ -177,11 +186,18 @@ def request(query, params): params['url'] = search_url.format(**fargs) params['soft_max_redirects'] = soft_max_redirects + params['raise_for_httperror'] = False + return params -def response(resp): +def response(resp): # pylint: disable=too-many-branches '''Scrap *results* from the response (see :ref:`engine results`).''' + if no_result_for_http_status and resp.status_code in no_result_for_http_status: + return [] + + raise_for_httperror(resp) + results = [] dom = html.fromstring(resp.text) is_onion = 'onions' in categories |