.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
lingva.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""Lingva (alternative Google Translate frontend)"""
3
4from json import loads
5
6about = {
7 "website": 'https://lingva.ml',
8 "wikidata_id": None,
9 "official_api_documentation": 'https://github.com/thedaviddelta/lingva-translate#public-apis',
10 "use_official_api": True,
11 "require_api_key": False,
12 "results": 'JSON',
13}
14
15engine_type = 'online_dictionary'
16categories = ['general', 'translate']
17
18url = "https://lingva.thedaviddelta.com"
19search_url = "{url}/api/v1/{from_lang}/{to_lang}/{query}"
20
21
22def request(_query, params):
23 params['url'] = search_url.format(
24 url=url, from_lang=params['from_lang'][1], to_lang=params['to_lang'][1], query=params['query']
25 )
26 return params
27
28
29def response(resp):
30 results = []
31
32 result = loads(resp.text)
33 info = result["info"]
34 from_to_prefix = "%s-%s " % (resp.search_params['from_lang'][1], resp.search_params['to_lang'][1])
35
36 if "typo" in info:
37 results.append({"suggestion": from_to_prefix + info["typo"]})
38
39 if 'definitions' in info: # pylint: disable=too-many-nested-blocks
40 for definition in info['definitions']:
41 if 'list' in definition:
42 for item in definition['list']:
43 if 'synonyms' in item:
44 for synonym in item['synonyms']:
45 results.append({"suggestion": from_to_prefix + synonym})
46
47 infobox = ""
48
49 for translation in info["extraTranslations"]:
50 for word in translation["list"]:
51 infobox += f"<dl><dt>{word['word']}</dt>"
52
53 for meaning in word["meanings"]:
54 infobox += f"<dd>{meaning}</dd>"
55
56 infobox += "</dl>"
57
58 results.append(
59 {
60 'infobox': result["translation"],
61 'content': infobox,
62 }
63 )
64
65 return results
request(_query, params)
Definition lingva.py:22