diff options
| author | Thomas Renard <Thomas.Renard@g3la.de> | 2022-08-08 21:31:24 +0200 |
|---|---|---|
| committer | Markus Heiser <markus.heiser@darmarit.de> | 2022-08-10 09:14:36 +0200 |
| commit | d4acbcfe63180780c6f0a0ad668710a3dd28913f (patch) | |
| tree | 34d682b15210eb5fa7ae79c6f03e19c5e27acf65 /searx | |
| parent | 07342899e5412d5ba03195f1ebbb7cb5ea525556 (diff) | |
[mod] add deepl translation engine
This implements the Deepl Translation engine. It works nearly like lingva but
directly to the deepl API. This api only needs a to-lang, from-lang is a fake
by now.
There is a free option to use [1].
[1] https://www.deepl.com/pro-api?cta=header-pro-api for registering a free account.
Diffstat (limited to 'searx')
| -rw-r--r-- | searx/engines/deepl.py | 62 | ||||
| -rw-r--r-- | searx/settings.yml | 9 |
2 files changed, 71 insertions, 0 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/settings.yml b/searx/settings.yml index 12d1f93f7..11da0b1fb 100644 --- a/searx/settings.yml +++ b/searx/settings.yml @@ -1626,6 +1626,15 @@ engines: engine: seznam disabled: true + # - name: deepl + # engine: deepl + # shortcut: dpl + # # You can use the engine using the official stable API, but you need an API key + # # See: https://www.deepl.com/pro-api?cta=header-pro-api + # api_key: '' # required! + # timeout: 5.0 + # disabled: true + - name: mojeek shortcut: mjk engine: xpath |