From 14756a267444614dbed2c8a456ef088f2755198c Mon Sep 17 00:00:00 2001 From: ta Date: Wed, 22 Jun 2022 14:08:19 +0700 Subject: [PATCH] [mod] Adds Lingva translate engine Add the lingva engine (which grabs data from google translate). Results from Lingva are added to the infobox results. --- searx/engines/lingva.py | 68 +++++++++++++++++++++++++++++++++++++++++ searx/settings.yml | 6 ++++ 2 files changed, 74 insertions(+) create mode 100644 searx/engines/lingva.py diff --git a/searx/engines/lingva.py b/searx/engines/lingva.py new file mode 100644 index 000000000..bf51b705e --- /dev/null +++ b/searx/engines/lingva.py @@ -0,0 +1,68 @@ +# SPDX-License-Identifier: AGPL-3.0-or-later +# lint: pylint +"""Lingva (alternative Google Translate frontend)""" + +from json import loads + +about = { + "website": 'https://lingva.ml', + "wikidata_id": None, + "official_api_documentation": 'https://github.com/thedaviddelta/lingva-translate#public-apis', + "use_official_api": True, + "require_api_key": False, + "results": 'JSON', +} + +engine_type = 'online_dictionary' +categories = ['general'] + +url = "https://lingva.ml" +search_url = "{url}/api/v1/{from_lang}/{to_lang}/{query}" + + +def request(_query, params): + params['url'] = search_url.format( + url=url, from_lang=params['from_lang'][1], to_lang=params['to_lang'][1], query=params['query'] + ) + return params + + +def response(resp): + results = [] + + result = loads(resp.text) + info = result["info"] + from_to_prefix = "%s-%s " % (resp.search_params['from_lang'][1], resp.search_params['to_lang'][1]) + + if "typo" in info: + results.append({"suggestion": from_to_prefix + info["typo"]}) + + if 'definitions' in info: # pylint: disable=too-many-nested-blocks + for definition in info['definitions']: + if 'list' in definition: + for item in definition['list']: + if 'synonyms' in item: + for synonym in item['synonyms']: + results.append({"suggestion": from_to_prefix + synonym}) + + infobox = "" + + for translation in info["extraTranslations"]: + infobox += f"{translation['type']}" + + for word in translation["list"]: + infobox += f"
{word['word']}
" + + for meaning in word["meanings"]: + infobox += f"
{meaning}
" + + infobox += "
" + + results.append( + { + 'infobox': result["translation"], + 'content': infobox, + } + ) + + return results diff --git a/searx/settings.yml b/searx/settings.yml index 3ecad0df5..a8ec798e7 100644 --- a/searx/settings.yml +++ b/searx/settings.yml @@ -886,6 +886,12 @@ engines: shortcut: loc categories: images + - name: lingva + engine: lingva + shortcut: lv + # set lingva instance in url, by default it will use the official instance + # url: https://lingva.ml + - name: lobste.rs engine: xpath search_url: https://lobste.rs/search?utf8=%E2%9C%93&q={query}&what=stories&order=relevance