From cfbe59b6b307b6808613f0ca6c0860c50526f443 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sun, 11 Feb 2024 13:56:30 +0100 Subject: [PATCH] [feat] engine: implementation of mozhi --- searx/engines/mozhi.py | 59 ++++++++++++++++++++++++++++++++++++++++++ searx/settings.yml | 11 ++++++++ 2 files changed, 70 insertions(+) create mode 100644 searx/engines/mozhi.py diff --git a/searx/engines/mozhi.py b/searx/engines/mozhi.py new file mode 100644 index 000000000..3494e1863 --- /dev/null +++ b/searx/engines/mozhi.py @@ -0,0 +1,59 @@ +# SPDX-License-Identifier: AGPL-3.0-or-later +# lint: pylint +"""Mozhi (alternative frontend for popular translation engines)""" + +import random +import re +from urllib.parse import urlencode + +about = { + "website": 'https://codeberg.org/aryak/mozhi', + "wikidata_id": None, + "official_api_documentation": 'https://mozhi.aryak.me/api/swagger/index.html', + "use_official_api": True, + "require_api_key": False, + "results": 'JSON', +} + +engine_type = 'online_dictionary' +categories = ['general'] + +base_url = "https://mozhi.aryak.me" +mozhi_engine = "google" + +re_transliteration_unsupported = "Direction '.*' is not supported" + + +def request(_query, params): + request_url = random.choice(base_url) if isinstance(base_url, list) else base_url + + args = {'from': params['from_lang'][1], 'to': params['to_lang'][1], 'text': params['query'], 'engine': mozhi_engine} + params['url'] = f"{request_url}/api/translate?{urlencode(args)}" + return params + + +def response(resp): + translation = resp.json() + + infobox = "" + + if translation['target_transliteration'] and not re.match( + re_transliteration_unsupported, translation['target_transliteration'] + ): + infobox = f"{translation['target_transliteration']}" + + if translation['word_choices']: + for word in translation['word_choices']: + infobox += f"
{word['word']}
" + + for example in word['examples_target']: + infobox += f"
{re.sub(r'<|>', '', example)}
" + + infobox += "
" + + result = { + 'infobox': translation['translated-text'], + 'content': infobox, + } + + return [result] diff --git a/searx/settings.yml b/searx/settings.yml index f56536ec0..f265f70c3 100644 --- a/searx/settings.yml +++ b/searx/settings.yml @@ -1158,6 +1158,17 @@ engines: # collection: 'reviews' # name of the db collection # key: 'name' # key in the collection to search for + - name: mozhi + engine: mozhi + base_url: + - https://mozhi.aryak.me + - https://translate.bus-hit.me + - https://nyc1.mz.ggtyler.dev + # mozhi_engine: google - see https://mozhi.aryak.me for supported engines + timeout: 4.0 + shortcut: mz + disabled: true + - name: mwmbl engine: mwmbl # api_url: https://api.mwmbl.org