mirror of
https://github.com/searxng/searxng.git
synced 2026-05-30 06:34:11 +02:00
[mod] addition of various type hints / engine processors
Continuation of #5147 .. typification of the engine processors. BTW: - removed obsolete engine property https_support - fixed & improved currency_convert - engine instances can now implement a engine.setup method [#5147] https://github.com/searxng/searxng/pull/5147 Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
This commit is contained in:
committed by
Markus Heiser
parent
23257bddce
commit
8f8343dc0d
@@ -51,7 +51,10 @@ ENGINE_DEFAULT_ARGS: dict[str, int | str | list[t.Any] | dict[str, t.Any] | bool
|
||||
DEFAULT_CATEGORY = 'other'
|
||||
|
||||
categories: "dict[str, list[Engine|types.ModuleType]]" = {'general': []}
|
||||
|
||||
engines: "dict[str, Engine | types.ModuleType]" = {}
|
||||
"""Global registered engine instances."""
|
||||
|
||||
engine_shortcuts = {}
|
||||
"""Simple map of registered *shortcuts* to name of the engine (or ``None``).
|
||||
|
||||
@@ -144,6 +147,9 @@ def load_engine(engine_data: dict[str, t.Any]) -> "Engine | types.ModuleType | N
|
||||
|
||||
set_loggers(engine, engine_name)
|
||||
|
||||
if not call_engine_setup(engine, engine_data):
|
||||
return None
|
||||
|
||||
if not any(cat in settings['categories_as_tabs'] for cat in engine.categories):
|
||||
engine.categories.append(DEFAULT_CATEGORY)
|
||||
|
||||
@@ -223,6 +229,25 @@ def is_engine_active(engine: "Engine | types.ModuleType"):
|
||||
return True
|
||||
|
||||
|
||||
def call_engine_setup(engine: "Engine | types.ModuleType", engine_data: dict[str, t.Any]) -> bool:
|
||||
setup_ok = False
|
||||
setup_func = getattr(engine, "setup", None)
|
||||
|
||||
if setup_func is None:
|
||||
setup_ok = True
|
||||
elif not callable(setup_func):
|
||||
logger.error("engine's setup method isn't a callable (is of type: %s)", type(setup_func))
|
||||
else:
|
||||
try:
|
||||
setup_ok = engine.setup(engine_data)
|
||||
except Exception as e: # pylint: disable=broad-except
|
||||
logger.exception('exception : {0}'.format(e))
|
||||
|
||||
if not setup_ok:
|
||||
logger.error("%s: Engine setup was not successful, engine is set to inactive.", engine.name)
|
||||
return setup_ok
|
||||
|
||||
|
||||
def register_engine(engine: "Engine | types.ModuleType"):
|
||||
if engine.name in engines:
|
||||
logger.error('Engine config error: ambiguous name: {0}'.format(engine.name))
|
||||
|
||||
@@ -1,53 +1,58 @@
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
"""Currency convert (DuckDuckGo)
|
||||
"""
|
||||
"""Currency convert (DuckDuckGo)"""
|
||||
|
||||
import typing as t
|
||||
import json
|
||||
from searx.result_types import EngineResults
|
||||
|
||||
if t.TYPE_CHECKING:
|
||||
from searx.search.processors import OnlineCurrenciesParams
|
||||
from searx.extended_types import SXNG_Response
|
||||
|
||||
# about
|
||||
about = {
|
||||
"website": 'https://duckduckgo.com/',
|
||||
"wikidata_id": 'Q12805',
|
||||
"official_api_documentation": 'https://duckduckgo.com/api',
|
||||
"website": "https://duckduckgo.com/",
|
||||
"wikidata_id": "Q12805",
|
||||
"official_api_documentation": "https://duckduckgo.com/api",
|
||||
"use_official_api": False,
|
||||
"require_api_key": False,
|
||||
"results": 'JSONP',
|
||||
"results": "JSONP",
|
||||
"description": "Service from DuckDuckGo.",
|
||||
}
|
||||
|
||||
engine_type = 'online_currency'
|
||||
categories = []
|
||||
base_url = 'https://duckduckgo.com/js/spice/currency/1/{0}/{1}'
|
||||
engine_type = "online_currency"
|
||||
categories = ["currency", "general"]
|
||||
|
||||
base_url = "https://duckduckgo.com/js/spice/currency/1/%(from_iso4217)s/%(to_iso4217)s"
|
||||
ddg_link_url = "https://duckduckgo.com/?q=%(from_iso4217)s+to+%(to_iso4217)s"
|
||||
|
||||
weight = 100
|
||||
|
||||
https_support = True
|
||||
|
||||
def request(query: str, params: "OnlineCurrenciesParams") -> None: # pylint: disable=unused-argument
|
||||
params["url"] = base_url % params
|
||||
|
||||
|
||||
def request(_query, params):
|
||||
params['url'] = base_url.format(params['from'], params['to'])
|
||||
return params
|
||||
|
||||
|
||||
def response(resp) -> EngineResults:
|
||||
def response(resp: "SXNG_Response") -> EngineResults:
|
||||
res = EngineResults()
|
||||
|
||||
# remove first and last lines to get only json
|
||||
json_resp = resp.text[resp.text.find('\n') + 1 : resp.text.rfind('\n') - 2]
|
||||
json_resp = resp.text[resp.text.find("\n") + 1 : resp.text.rfind("\n") - 2]
|
||||
try:
|
||||
conversion_rate = float(json.loads(json_resp)["to"][0]["mid"])
|
||||
except IndexError:
|
||||
return res
|
||||
answer = '{0} {1} = {2} {3}, 1 {1} ({5}) = {4} {3} ({6})'.format(
|
||||
resp.search_params['amount'],
|
||||
resp.search_params['from'],
|
||||
resp.search_params['amount'] * conversion_rate,
|
||||
resp.search_params['to'],
|
||||
conversion_rate,
|
||||
resp.search_params['from_name'],
|
||||
resp.search_params['to_name'],
|
||||
)
|
||||
|
||||
url = f"https://duckduckgo.com/?q={resp.search_params['from']}+to+{resp.search_params['to']}"
|
||||
params: OnlineCurrenciesParams = resp.search_params # pyright: ignore[reportAssignmentType]
|
||||
answer = "{0} {1} = {2} {3} (1 {5} : {4} {6})".format(
|
||||
params["amount"],
|
||||
params["from_iso4217"],
|
||||
params["amount"] * conversion_rate,
|
||||
params["to_iso4217"],
|
||||
conversion_rate,
|
||||
params["from_name"],
|
||||
params["to_name"],
|
||||
)
|
||||
url = ddg_link_url % params
|
||||
res.add(res.types.Answer(answer=answer, url=url))
|
||||
return res
|
||||
|
||||
@@ -24,7 +24,6 @@ engine_type = 'online_dictionary'
|
||||
categories = ['general', 'translate']
|
||||
base_url = "https://dictzone.com"
|
||||
weight = 100
|
||||
https_support = True
|
||||
|
||||
|
||||
def request(query, params): # pylint: disable=unused-argument
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
"""
|
||||
|
||||
from urllib.parse import urlunparse
|
||||
from json import dumps
|
||||
|
||||
# about
|
||||
about = {
|
||||
@@ -56,7 +55,7 @@ def request(query, params):
|
||||
query_data = query_data_template
|
||||
query_data["query"]["multi_match"]["query"] = query
|
||||
query_data["from"] = (params["pageno"] - 1) * number_of_results
|
||||
params["data"] = dumps(query_data)
|
||||
params["json"] = query_data
|
||||
return params
|
||||
|
||||
|
||||
|
||||
@@ -22,7 +22,6 @@ categories = ['general', 'translate']
|
||||
api_url = "https://api.mymemory.translated.net"
|
||||
web_url = "https://mymemory.translated.net"
|
||||
weight = 100
|
||||
https_support = True
|
||||
|
||||
api_key = ''
|
||||
|
||||
|
||||
Reference in New Issue
Block a user