diff options
| author | Markus Heiser <markus.heiser@darmarit.de> | 2025-09-11 19:10:27 +0200 |
|---|---|---|
| committer | Markus Heiser <markus.heiser@darmarIT.de> | 2025-09-18 19:40:03 +0200 |
| commit | 8f8343dc0d78bb57215afc3e99fd9000fce6e0cf (patch) | |
| tree | 7c0aa8587ed4bc47e403b4148a308191e2d21c55 /searx/engines | |
| parent | 23257bddce864cfc44d64324dee36b32b1cf5248 (diff) | |
[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>
Diffstat (limited to 'searx/engines')
| -rw-r--r-- | searx/engines/__init__.py | 25 | ||||
| -rw-r--r-- | searx/engines/currency_convert.py | 55 | ||||
| -rw-r--r-- | searx/engines/dictzone.py | 1 | ||||
| -rw-r--r-- | searx/engines/metacpan.py | 3 | ||||
| -rw-r--r-- | searx/engines/translated.py | 1 |
5 files changed, 56 insertions, 29 deletions
diff --git a/searx/engines/__init__.py b/searx/engines/__init__.py index b1e24aea2..30ef7fd75 100644 --- a/searx/engines/__init__.py +++ b/searx/engines/__init__.py @@ -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)) diff --git a/searx/engines/currency_convert.py b/searx/engines/currency_convert.py index c4c757e3f..0b9b339a9 100644 --- a/searx/engines/currency_convert.py +++ b/searx/engines/currency_convert.py @@ -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}' -weight = 100 +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" -https_support = True +weight = 100 -def request(_query, params): - params['url'] = base_url.format(params['from'], params['to']) - return params +def request(query: str, params: "OnlineCurrenciesParams") -> None: # pylint: disable=unused-argument + params["url"] = base_url % 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'], + + 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, - resp.search_params['from_name'], - resp.search_params['to_name'], + params["from_name"], + params["to_name"], ) - - url = f"https://duckduckgo.com/?q={resp.search_params['from']}+to+{resp.search_params['to']}" + url = ddg_link_url % params res.add(res.types.Answer(answer=answer, url=url)) return res diff --git a/searx/engines/dictzone.py b/searx/engines/dictzone.py index bda056edd..d393eae92 100644 --- a/searx/engines/dictzone.py +++ b/searx/engines/dictzone.py @@ -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 diff --git a/searx/engines/metacpan.py b/searx/engines/metacpan.py index 50608bc11..32bc55b89 100644 --- a/searx/engines/metacpan.py +++ b/searx/engines/metacpan.py @@ -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 diff --git a/searx/engines/translated.py b/searx/engines/translated.py index cffb6eda3..08808cfd2 100644 --- a/searx/engines/translated.py +++ b/searx/engines/translated.py @@ -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 = '' |