.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
translated.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""MyMemory Translated
3
4"""
5
6import urllib.parse
7
8from searx.result_types import EngineResults
9
10# about
11about = {
12 "website": 'https://mymemory.translated.net/',
13 "wikidata_id": None,
14 "official_api_documentation": 'https://mymemory.translated.net/doc/spec.php',
15 "use_official_api": True,
16 "require_api_key": False,
17 "results": 'JSON',
18}
19
20engine_type = 'online_dictionary'
21categories = ['general', 'translate']
22api_url = "https://api.mymemory.translated.net"
23web_url = "https://mymemory.translated.net"
24weight = 100
25https_support = True
26
27api_key = ''
28
29
30def request(query, params): # pylint: disable=unused-argument
31
32 args = {"q": params["query"], "langpair": f"{params['from_lang'][1]}|{params['to_lang'][1]}"}
33 if api_key:
34 args["key"] = api_key
35
36 params['url'] = f"{api_url}/get?{urllib.parse.urlencode(args)}"
37 return params
38
39
40def response(resp) -> EngineResults:
41 results = EngineResults()
42 data = resp.json()
43
44 args = {
45 "q": resp.search_params["query"],
46 "lang": resp.search_params.get("searxng_locale", "en"), # ui language
47 "sl": resp.search_params['from_lang'][1],
48 "tl": resp.search_params['to_lang'][1],
49 }
50
51 link = f"{web_url}/search.php?{urllib.parse.urlencode(args)}"
52 text = data['responseData']['translatedText']
53
54 examples = [f"{m['segment']} : {m['translation']}" for m in data['matches'] if m['translation'] != text]
55
56 item = results.types.Translations.Item(text=text, examples=examples)
57 results.add(results.types.Translations(translations=[item], url=link))
58
59 return results
request(query, params)
Definition translated.py:30
EngineResults response(resp)
Definition translated.py:40