mirror of
https://github.com/searxng/searxng.git
synced 2026-05-26 12:50:14 +02:00
[mod] engine: MyMemory Translated - typification and html to text (#6132)
The implementation is normalized, type annotations are applied, and the results are freed from the HTML markup (which is partially present). Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
This commit is contained in:
+35
-18
@@ -1,56 +1,73 @@
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
"""MyMemory Translated"""
|
||||
|
||||
import typing as t
|
||||
|
||||
import urllib.parse
|
||||
|
||||
from searx.utils import html_to_text
|
||||
from searx.result_types import EngineResults
|
||||
|
||||
if t.TYPE_CHECKING:
|
||||
from searx.extended_types import SXNG_Response
|
||||
from searx.search.processors import OnlineDictParams
|
||||
#
|
||||
# about
|
||||
about = {
|
||||
"website": 'https://mymemory.translated.net/',
|
||||
"website": "https://mymemory.translated.net/",
|
||||
"wikidata_id": None,
|
||||
"official_api_documentation": 'https://mymemory.translated.net/doc/spec.php',
|
||||
"official_api_documentation": "https://mymemory.translated.net/doc/spec.php",
|
||||
"use_official_api": True,
|
||||
"require_api_key": False,
|
||||
"results": 'JSON',
|
||||
"results": "JSON",
|
||||
}
|
||||
|
||||
engine_type = 'online_dictionary'
|
||||
categories = ['general', 'translate']
|
||||
engine_type = "online_dictionary"
|
||||
categories = ["general", "translate"]
|
||||
api_url = "https://api.mymemory.translated.net"
|
||||
web_url = "https://mymemory.translated.net"
|
||||
weight = 100
|
||||
|
||||
api_key = ''
|
||||
api_key = ""
|
||||
|
||||
|
||||
def request(query, params): # pylint: disable=unused-argument
|
||||
def request(_: str, params: "OnlineDictParams") -> None:
|
||||
|
||||
args = {"q": params["query"], "langpair": f"{params['from_lang'][1]}|{params['to_lang'][1]}"}
|
||||
args = {
|
||||
"q": params["query"],
|
||||
"langpair": f"{params['from_lang'][1]}|{params['to_lang'][1]}",
|
||||
}
|
||||
if api_key:
|
||||
args["key"] = api_key
|
||||
|
||||
params['url'] = f"{api_url}/get?{urllib.parse.urlencode(args)}"
|
||||
return params
|
||||
|
||||
|
||||
def response(resp) -> EngineResults:
|
||||
def response(resp: "SXNG_Response") -> EngineResults:
|
||||
|
||||
results = EngineResults()
|
||||
data = resp.json()
|
||||
data: dict[str, t.Any] = resp.json()
|
||||
params: "OnlineDictParams" = resp.search_params # pyright: ignore[reportAssignmentType]
|
||||
|
||||
args = {
|
||||
"q": resp.search_params["query"],
|
||||
"lang": resp.search_params.get("searxng_locale", "en"), # ui language
|
||||
"sl": resp.search_params['from_lang'][1],
|
||||
"tl": resp.search_params['to_lang'][1],
|
||||
"q": params["query"],
|
||||
"lang": params.get("searxng_locale", "en"), # ui language
|
||||
"sl": params["from_lang"][1],
|
||||
"tl": params["to_lang"][1],
|
||||
}
|
||||
|
||||
link = f"{web_url}/search.php?{urllib.parse.urlencode(args)}"
|
||||
text = data['responseData']['translatedText']
|
||||
text: str = html_to_text(data["responseData"]["translatedText"])
|
||||
|
||||
examples = [f"{m['segment']} : {m['translation']}" for m in data['matches'] if m['translation'] != text]
|
||||
examples: set[str] = set()
|
||||
match: dict[str, str]
|
||||
for match in data["matches"]:
|
||||
_text = html_to_text(match["translation"])
|
||||
if _text != text:
|
||||
_seg = html_to_text(match["segment"])
|
||||
examples.add(f"{_seg} : {_text}")
|
||||
|
||||
item = results.types.Translations.Item(text=text, examples=examples)
|
||||
item = results.types.Translations.Item(text=text, examples=list(examples))
|
||||
results.add(results.types.Translations(translations=[item], url=link))
|
||||
|
||||
return results
|
||||
|
||||
@@ -30,7 +30,7 @@ import httpx
|
||||
if typing.TYPE_CHECKING:
|
||||
import searx.preferences
|
||||
import searx.results
|
||||
from searx.search.processors import OnlineParamTypes
|
||||
from searx.search.processors import OnlineParamTypes, OnlineDictParams, OnlineCurrenciesParams
|
||||
|
||||
|
||||
class SXNG_Request(flask.Request):
|
||||
@@ -83,4 +83,4 @@ class SXNG_Response(httpx.Response):
|
||||
"""
|
||||
|
||||
ok: bool
|
||||
search_params: "OnlineParamTypes"
|
||||
search_params: "OnlineParamTypes | OnlineDictParams | OnlineCurrenciesParams"
|
||||
|
||||
Reference in New Issue
Block a user