.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
7
8about = {
9 "website": 'https://codeberg.org/aryak/mozhi',
10 "wikidata_id": None,
11 "official_api_documentation": 'https://mozhi.aryak.me/api/swagger/index.html',
12 "use_official_api": True,
13 "require_api_key": False,
14 "results": 'JSON',
15}
16
17engine_type = 'online_dictionary'
18categories = ['general', 'translate']
19
20base_url = "https://mozhi.aryak.me"
21mozhi_engine = "google"
22
23re_transliteration_unsupported = "Direction '.*' is not supported"
24
25
26def request(_query, params):
27 request_url = random.choice(base_url) if isinstance(base_url, list) else base_url
28
29 args = {'from': params['from_lang'][1], 'to': params['to_lang'][1], 'text': params['query'], 'engine': mozhi_engine}
30 params['url'] = f"{request_url}/api/translate?{urlencode(args)}"
31 return params
32
33
34def response(resp):
35 translation = resp.json()
36
37 infobox = ""
38
39 if translation['target_transliteration'] and not re.match(
40 re_transliteration_unsupported, translation['target_transliteration']
41 ):
42 infobox = f"<b>{translation['target_transliteration']}</b>"
43
44 if translation['word_choices']:
45 for word in translation['word_choices']:
46 infobox += f"<dl><dt>{word['word']}</dt>"
47
48 for example in word['examples_target']:
49 infobox += f"<dd>{re.sub(r'<|>', '', example)}</dd>"
50
51 infobox += "</dl>"
52
53 result = {
54 'infobox': translation['translated-text'],
55 'content': infobox,
56 }
57
58 return [result]
request(_query, params)
Definition mozhi.py:26