summaryrefslogtreecommitdiff
path: root/searx/engines/libretranslate.py
diff options
context:
space:
mode:
Diffstat (limited to 'searx/engines/libretranslate.py')
-rw-r--r--searx/engines/libretranslate.py25
1 files changed, 16 insertions, 9 deletions
diff --git a/searx/engines/libretranslate.py b/searx/engines/libretranslate.py
index d9b9cf2f9..2e6663cb0 100644
--- a/searx/engines/libretranslate.py
+++ b/searx/engines/libretranslate.py
@@ -2,7 +2,8 @@
"""LibreTranslate (Free and Open Source Machine Translation API)"""
import random
-from json import dumps
+import json
+from searx.result_types import Translations
about = {
"website": 'https://libretranslate.com',
@@ -16,19 +17,27 @@ about = {
engine_type = 'online_dictionary'
categories = ['general', 'translate']
-base_url = "https://translate.terraprint.co"
-api_key = ''
+base_url = "https://libretranslate.com/translate"
+api_key = ""
def request(_query, params):
request_url = random.choice(base_url) if isinstance(base_url, list) else base_url
+
+ if request_url.startswith("https://libretranslate.com") and not api_key:
+ return None
params['url'] = f"{request_url}/translate"
- args = {'source': params['from_lang'][1], 'target': params['to_lang'][1], 'q': params['query'], 'alternatives': 3}
+ args = {
+ 'q': params['query'],
+ 'source': params['from_lang'][1],
+ 'target': params['to_lang'][1],
+ 'alternatives': 3,
+ }
if api_key:
args['api_key'] = api_key
- params['data'] = dumps(args)
+ params['data'] = json.dumps(args)
params['method'] = 'POST'
params['headers'] = {'Content-Type': 'application/json'}
params['req_url'] = request_url
@@ -41,12 +50,10 @@ def response(resp):
json_resp = resp.json()
text = json_resp.get('translatedText')
-
if not text:
return results
- translations = [{'text': text}] + [{'text': alternative} for alternative in json_resp.get('alternatives', [])]
-
- results.append({'answer': text, 'answer_type': 'translations', 'translations': translations})
+ item = Translations.Item(text=text, examples=json_resp.get('alternatives', []))
+ Translations(results=results, translations=[item])
return results