Merge pull request #1434 from liimee/eng-4

Adds Lingva/Google Translate engine
This commit is contained in:
Markus Heiser 2022-07-04 19:17:50 +02:00 committed by GitHub
commit b5cceeb2f9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 97 additions and 18 deletions

68
searx/engines/lingva.py Normal file
View file

@ -0,0 +1,68 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
# lint: pylint
"""Lingva (alternative Google Translate frontend)"""
from json import loads
about = {
"website": 'https://lingva.ml',
"wikidata_id": None,
"official_api_documentation": 'https://github.com/thedaviddelta/lingva-translate#public-apis',
"use_official_api": True,
"require_api_key": False,
"results": 'JSON',
}
engine_type = 'online_dictionary'
categories = ['general']
url = "https://lingva.ml"
search_url = "{url}/api/v1/{from_lang}/{to_lang}/{query}"
def request(_query, params):
params['url'] = search_url.format(
url=url, from_lang=params['from_lang'][1], to_lang=params['to_lang'][1], query=params['query']
)
return params
def response(resp):
results = []
result = loads(resp.text)
info = result["info"]
from_to_prefix = "%s-%s " % (resp.search_params['from_lang'][1], resp.search_params['to_lang'][1])
if "typo" in info:
results.append({"suggestion": from_to_prefix + info["typo"]})
if 'definitions' in info: # pylint: disable=too-many-nested-blocks
for definition in info['definitions']:
if 'list' in definition:
for item in definition['list']:
if 'synonyms' in item:
for synonym in item['synonyms']:
results.append({"suggestion": from_to_prefix + synonym})
infobox = ""
for translation in info["extraTranslations"]:
infobox += f"<b>{translation['type']}</b>"
for word in translation["list"]:
infobox += f"<dl><dt>{word['word']}</dt>"
for meaning in word["meanings"]:
infobox += f"<dd>{meaning}</dd>"
infobox += "</dl>"
results.append(
{
'infobox': result["translation"],
'content': infobox,
}
)
return results

View file

@ -886,6 +886,12 @@ engines:
shortcut: loc shortcut: loc
categories: images categories: images
- name: lingva
engine: lingva
shortcut: lv
# set lingva instance in url, by default it will use the official instance
# url: https://lingva.ml
- name: lobste.rs - name: lobste.rs
engine: xpath engine: xpath
search_url: https://lobste.rs/search?utf8=%E2%9C%93&q={query}&what=stories&order=relevance search_url: https://lobste.rs/search?utf8=%E2%9C%93&q={query}&what=stories&order=relevance

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -594,24 +594,29 @@ article[data-vim-selected].category-social {
padding: 0; padding: 0;
} }
dl {
margin: 0.5em 0;
}
dt { dt {
display: inline;
margin-top: 0.5em;
.ltr-margin-right(0.25em);
margin-bottom: 0.5em;
.ltr-margin-left(0);
padding: 0;
font-weight: bold; font-weight: bold;
} }
dd { .attributes {
display: inline; dl {
margin: 0.5em 0; margin: 0.5em 0;
padding: 0; }
dt {
display: inline;
margin-top: 0.5em;
.ltr-margin-right(0.25em);
margin-bottom: 0.5em;
.ltr-margin-left(0);
padding: 0;
}
dd {
display: inline;
margin: 0.5em 0;
padding: 0;
}
} }
input { input {