.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
4
import
random
5
import
re
6
from
urllib.parse
import
urlencode
7
from
flask_babel
import
gettext
8
9
about = {
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
18
engine_type =
'online_dictionary'
19
categories = [
'general'
,
'translate'
]
20
21
base_url =
"https://mozhi.aryak.me"
22
mozhi_engine =
"google"
23
24
re_transliteration_unsupported =
"Direction '.*' is not supported"
25
26
27
def
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
35
def
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]
searx.engines.mozhi.request
request(_query, params)
Definition
mozhi.py:27
searx.engines.mozhi.response
response(resp)
Definition
mozhi.py:35
searxng
searx
engines
mozhi.py
Generated on Sat Nov 16 2024 00:10:57 for .oO SearXNG Developer Documentation Oo. by
1.12.0