.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
25
26api_key = ''
27
28
29def request(query, params): # pylint: disable=unused-argument
30
31 args = {"q": params["query"], "langpair": f"{params['from_lang'][1]}|{params['to_lang'][1]}"}
32 if api_key:
33 args["key"] = api_key
34
35 params['url'] = f"{api_url}/get?{urllib.parse.urlencode(args)}"
36 return params
37
38
39def response(resp) -> EngineResults:
40 results = EngineResults()
41 data = resp.json()
42
43 args = {
44 "q": resp.search_params["query"],
45 "lang": resp.search_params.get("searxng_locale", "en"), # ui language
46 "sl": resp.search_params['from_lang'][1],
47 "tl": resp.search_params['to_lang'][1],
48 }
49
50 link = f"{web_url}/search.php?{urllib.parse.urlencode(args)}"
51 text = data['responseData']['translatedText']
52
53 examples = [f"{m['segment']} : {m['translation']}" for m in data['matches'] if m['translation'] != text]
54
55 item = results.types.Translations.Item(text=text, examples=examples)
56 results.add(results.types.Translations(translations=[item], url=link))
57
58 return results
request(query, params)
Definition translated.py:29
EngineResults response(resp)
Definition translated.py:39