.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
mozhi.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""Mozhi (alternative frontend for popular translation engines)"""
3
4import random
5import re
6from urllib.parse import urlencode
7from flask_babel import gettext
8
9about = {
10 "website": 'https://codeberg.org/aryak/mozhi',
11 "wikidata_id": None,
12 "official_api_documentation": 'https://mozhi.aryak.me/api/swagger/index.html',
13 "use_official_api": True,
14 "require_api_key": False,
15 "results": 'JSON',
16}
17
18engine_type = 'online_dictionary'
19categories = ['general', 'translate']
20
21base_url = "https://mozhi.aryak.me"
22mozhi_engine = "google"
23
24re_transliteration_unsupported = "Direction '.*' is not supported"
25
26
27def request(_query, params):
28 request_url = random.choice(base_url) if isinstance(base_url, list) else base_url
29
30 args = {'from': params['from_lang'][1], 'to': params['to_lang'][1], 'text': params['query'], 'engine': mozhi_engine}
31 params['url'] = f"{request_url}/api/translate?{urlencode(args)}"
32 return params
33
34
35def response(resp):
36 translation = resp.json()
37
38 infobox = ""
39
40 if translation['target_transliteration'] and not re.match(
41 re_transliteration_unsupported, translation['target_transliteration']
42 ):
43 infobox = f"<b>{translation['target_transliteration']}</b>"
44
45 if translation['word_choices']:
46 for word in translation['word_choices']:
47 infobox += f"<dl><dt>{word['word']}: {word['definition']}</dt>"
48
49 if word['examples_target']:
50 for example in word['examples_target']:
51 infobox += f"<dd>{re.sub(r'<|>', '', example)}</dd>"
52 infobox += f"<dd>{re.sub(r'<|>', '', example)}</dd>"
53
54 infobox += "</dl>"
55
56 if translation['source_synonyms']:
57 infobox += f"<dl><dt>{gettext('Synonyms')}: {', '.join(translation['source_synonyms'])}</dt></dl>"
58
59 result = {
60 'infobox': translation['translated-text'],
61 'content': infobox,
62 }
63
64 return [result]
request(_query, params)
Definition mozhi.py:27