From 91522f3801e11aac117c8f2b3c18a87dea0ebad0 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Thu, 25 Apr 2024 19:47:59 +0200 Subject: [PATCH] [feat] engine: implementation of LibreTranslate Co-authored-by: Markus Heiser --- searx/engines/libretranslate.py | 46 +++++++++++++++++++++++++++++++++ searx/settings.yml | 10 +++++++ 2 files changed, 56 insertions(+) create mode 100644 searx/engines/libretranslate.py diff --git a/searx/engines/libretranslate.py b/searx/engines/libretranslate.py new file mode 100644 index 000000000..20fa481f5 --- /dev/null +++ b/searx/engines/libretranslate.py @@ -0,0 +1,46 @@ +# SPDX-License-Identifier: AGPL-3.0-or-later +"""LibreTranslate (Free and Open Source Machine Translation API)""" + +import random +from json import dumps + +about = { + "website": 'https://libretranslate.com', + "wikidata_id": None, + "official_api_documentation": 'https://libretranslate.com/docs/', + "use_official_api": True, + "require_api_key": False, + "results": 'JSON', +} + +engine_type = 'online_dictionary' +categories = ['general', 'translate'] + +base_url = "https://translate.terraprint.co" +api_key = '' + + +def request(_query, params): + request_url = random.choice(base_url) if isinstance(base_url, list) else base_url + params['url'] = f"{request_url}/translate" + + args = {'source': params['from_lang'][1], 'target': params['to_lang'][1], 'q': params['query']} + if api_key: + args['api_key'] = api_key + params['data'] = dumps(args) + + params['method'] = 'POST' + params['headers'] = {'Content-Type': 'application/json'} + + return params + + +def response(resp): + results = [] + + json_resp = resp.json() + text = json_resp.get('translatedText') + if text: + results.append({'answer': text}) + + return results diff --git a/searx/settings.yml b/searx/settings.yml index ce725d7e7..6bf060144 100644 --- a/searx/settings.yml +++ b/searx/settings.yml @@ -1060,6 +1060,16 @@ engines: shortcut: loc categories: images + - name: libretranslate + engine: libretranslate + # https://github.com/LibreTranslate/LibreTranslate?tab=readme-ov-file#mirrors + base_url: + - https://translate.terraprint.co + - https://trans.zillyhuhn.com + # api_key: abc123 + shortcut: lt + disabled: true + - name: lingva engine: lingva shortcut: lv