From 9079d0cac0156139952446f0fdc8b37b94c10756 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Tue, 15 Oct 2024 15:39:14 +0200 Subject: [refactor] translation engines: common interface --- searx/engines/translated.py | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) (limited to 'searx/engines/translated.py') diff --git a/searx/engines/translated.py b/searx/engines/translated.py index ea8c081dc..190707a95 100644 --- a/searx/engines/translated.py +++ b/searx/engines/translated.py @@ -35,18 +35,16 @@ def request(query, params): # pylint: disable=unused-argument def response(resp): - results = [] - results.append( - { - 'url': web_url.format( - from_lang=resp.search_params['from_lang'][2], - to_lang=resp.search_params['to_lang'][2], - query=resp.search_params['query'], - ), - 'title': '[{0}-{1}] {2}'.format( - resp.search_params['from_lang'][1], resp.search_params['to_lang'][1], resp.search_params['query'] - ), - 'content': resp.json()['responseData']['translatedText'], - } - ) - return results + json_resp = resp.json() + text = json_resp['responseData']['translatedText'] + + alternatives = [match['translation'] for match in json_resp['matches'] if match['translation'] != text] + translations = [{'text': translation} for translation in [text] + alternatives] + + result = { + 'answer': translations[0]['text'], + 'answer_type': 'translations', + 'translations': translations, + } + + return [result] -- cgit v1.2.3