.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
libretranslate.py
Go to the documentation of this file.
1
# SPDX-License-Identifier: AGPL-3.0-or-later
2
"""LibreTranslate (Free and Open Source Machine Translation API)"""
3
4
import
random
5
from
json
import
dumps
6
7
about = {
8
"website"
:
'https://libretranslate.com'
,
9
"wikidata_id"
:
None
,
10
"official_api_documentation"
:
'https://libretranslate.com/docs/'
,
11
"use_official_api"
:
True
,
12
"require_api_key"
:
False
,
13
"results"
:
'JSON'
,
14
}
15
16
engine_type =
'online_dictionary'
17
categories = [
'general'
,
'translate'
]
18
19
base_url =
"https://translate.terraprint.co"
20
api_key =
''
21
22
23
def
request
(_query, params):
24
request_url = random.choice(base_url)
if
isinstance(base_url, list)
else
base_url
25
params[
'url'
] = f
"{request_url}/translate"
26
27
args = {
'source'
: params[
'from_lang'
][1],
'target'
: params[
'to_lang'
][1],
'q'
: params[
'query'
]}
28
if
api_key:
29
args[
'api_key'
] = api_key
30
params[
'data'
] = dumps(args)
31
32
params[
'method'
] =
'POST'
33
params[
'headers'
] = {
'Content-Type'
:
'application/json'
}
34
35
return
params
36
37
38
def
response
(resp):
39
results = []
40
41
json_resp = resp.json()
42
text = json_resp.get(
'translatedText'
)
43
if
text:
44
results.append({
'answer'
: text})
45
46
return
results
searx.engines.libretranslate.response
response(resp)
Definition
libretranslate.py:38
searx.engines.libretranslate.request
request(_query, params)
Definition
libretranslate.py:23
searxng
searx
engines
libretranslate.py
Generated on Wed May 15 2024 21:10:12 for .oO SearXNG Developer Documentation Oo. by
1.10.0