[mod] demo engines: smaller corrections and improvements

Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
This commit is contained in:
Markus Heiser 2025-09-10 17:23:52 +02:00 committed by Markus Heiser
parent 8f8343dc0d
commit 09fddfde24
2 changed files with 62 additions and 22 deletions

View File

@ -1,7 +1,12 @@
# SPDX-License-Identifier: AGPL-3.0-or-later # SPDX-License-Identifier: AGPL-3.0-or-later
"""Within this module we implement a *demo offline engine*. Do not look to """Within this module we implement a *demo offline engine*. Do not look to
close to the implementation, its just a simple example. To get in use of this close to the implementation, its just a simple example.
*demo* engine add the following entry to your engines list in ``settings.yml``:
Configuration
=============
To get in use of this *demo* engine add the following entry to your engines list
in ``settings.yml``:
.. code:: yaml .. code:: yaml
@ -10,6 +15,9 @@ close to the implementation, its just a simple example. To get in use of this
shortcut: demo shortcut: demo
disabled: false disabled: false
Implementations
===============
""" """
import typing as t import typing as t
@ -18,6 +26,10 @@ import json
from searx.result_types import EngineResults from searx.result_types import EngineResults
from searx.enginelib import EngineCache from searx.enginelib import EngineCache
if t.TYPE_CHECKING:
from searx.search.processors import RequestParams
engine_type = "offline" engine_type = "offline"
categories = ["general"] categories = ["general"]
disabled = True disabled = True
@ -28,7 +40,7 @@ about = {
"official_api_documentation": None, "official_api_documentation": None,
"use_official_api": False, "use_official_api": False,
"require_api_key": False, "require_api_key": False,
"results": 'JSON', "results": "JSON",
} }
# if there is a need for globals, use a leading underline # if there is a need for globals, use a leading underline
@ -39,10 +51,14 @@ CACHE: EngineCache
seconds.""" seconds."""
def init(engine_settings: dict[str, t.Any]) -> None: def setup(engine_settings: dict[str, t.Any]) -> bool:
"""Initialization of the (offline) engine. The origin of this demo engine is a """Dynamic setup of the engine settings.
simple json string which is loaded in this example while the engine is
initialized.""" The origin of this demo engine is a simple json string which is loaded in
this example while the engine is initialized.
For more details see :py:obj:`searx.enginelib.Engine.setup`.
"""
global _my_offline_engine, CACHE # pylint: disable=global-statement global _my_offline_engine, CACHE # pylint: disable=global-statement
CACHE = EngineCache(engine_settings["name"]) CACHE = EngineCache(engine_settings["name"])
@ -55,12 +71,23 @@ def init(engine_settings: dict[str, t.Any]) -> None:
']' % engine_settings.get('name') ']' % engine_settings.get('name')
) )
return True
def search(query: str, params: dict[str, t.Any]) -> EngineResults:
def init(engine_settings: dict[str, t.Any]) -> bool: # pylint: disable=unused-argument
"""Initialization of the engine.
For more details see :py:obj:`searx.enginelib.Engine.init`.
"""
return True
def search(query: str, params: "RequestParams") -> EngineResults:
"""Query (offline) engine and return results. Assemble the list of results """Query (offline) engine and return results. Assemble the list of results
from your local engine. In this demo engine we ignore the 'query' term, from your local engine.
usual you would pass the 'query' term to your local engine to filter out the
results. In this demo engine we ignore the 'query' term, usual you would pass the
'query' term to your local engine to filter out the results.
""" """
res = EngineResults() res = EngineResults()

View File

@ -3,6 +3,9 @@
close to the implementation, its just a simple example which queries `The Art close to the implementation, its just a simple example which queries `The Art
Institute of Chicago <https://www.artic.edu>`_ Institute of Chicago <https://www.artic.edu>`_
Configuration
=============
To get in use of this *demo* engine add the following entry to your engines To get in use of this *demo* engine add the following entry to your engines
list in ``settings.yml``: list in ``settings.yml``:
@ -13,16 +16,19 @@ list in ``settings.yml``:
shortcut: demo shortcut: demo
disabled: false disabled: false
Implementations
===============
""" """
import typing as t import typing as t
from json import loads
from urllib.parse import urlencode from urllib.parse import urlencode
from searx.result_types import EngineResults from searx.result_types import EngineResults
if t.TYPE_CHECKING: if t.TYPE_CHECKING:
from searx.extended_types import SXNG_Response from searx.extended_types import SXNG_Response
from searx.search.processors import OnlineParams
engine_type = "online" engine_type = "online"
@ -34,7 +40,7 @@ categories = ["images"]
paging = True paging = True
page_size = 20 page_size = 20
search_api = "https://api.artic.edu/api/v1/artworks/search?" search_api = "https://api.artic.edu/api/v1/artworks/search"
image_api = "https://www.artic.edu/iiif/2/" image_api = "https://www.artic.edu/iiif/2/"
about = { about = {
@ -51,18 +57,25 @@ about = {
_my_online_engine = None _my_online_engine = None
def init(engine_settings: dict[str, t.Any]) -> None: def setup(engine_settings: dict[str, t.Any]) -> bool:
"""Initialization of the (online) engine. If no initialization is needed, drop """Dynamic setup of the engine settings.
this init function."""
For more details see :py:obj:`searx.enginelib.Engine.setup`."""
global _my_online_engine # pylint: disable=global-statement global _my_online_engine # pylint: disable=global-statement
_my_online_engine = engine_settings.get("name") _my_online_engine = engine_settings.get("name")
return True
def request(query: str, params: dict[str, t.Any]) -> None: def init(engine_settings: dict[str, t.Any]) -> bool: # pylint: disable=unused-argument
"""Initialization of the engine.
For more details see :py:obj:`searx.enginelib.Engine.init`."""
return True
def request(query: str, params: "OnlineParams") -> None:
"""Build up the ``params`` for the online request. In this example we build a """Build up the ``params`` for the online request. In this example we build a
URL to fetch images from `artic.edu <https://artic.edu>`__ URL to fetch images from `artic.edu <https://artic.edu>`__."""
"""
args = urlencode( args = urlencode(
{ {
"q": query, "q": query,
@ -71,7 +84,7 @@ def request(query: str, params: dict[str, t.Any]) -> None:
"limit": page_size, "limit": page_size,
} }
) )
params["url"] = search_api + args params["url"] = f"{search_api}?{args}"
def response(resp: "SXNG_Response") -> EngineResults: def response(resp: "SXNG_Response") -> EngineResults:
@ -81,7 +94,7 @@ def response(resp: "SXNG_Response") -> EngineResults:
""" """
res = EngineResults() res = EngineResults()
json_data = loads(resp.text) json_data = resp.json()
res.add( res.add(
res.types.Answer( res.types.Answer(