.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
demo_offline.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""Within this module we implement a *demo offline engine*. Do not look to
3close to the implementation, its just a simple example.
4
5Configuration
6=============
7
8To get in use of this *demo* engine add the following entry to your engines list
9in ``settings.yml``:
10
11.. code:: yaml
12
13 - name: my offline engine
14 engine: demo_offline
15 shortcut: demo
16 disabled: false
17
18Implementations
19===============
20
21"""
22
23import typing as t
24import json
25
26from searx.result_types import EngineResults
27from searx.enginelib import EngineCache
28
29if t.TYPE_CHECKING:
30 from searx.search.processors import RequestParams
31
32
33engine_type = "offline"
34categories = ["general"]
35disabled = True
36timeout = 2.0
37
38about = {
39 "wikidata_id": None,
40 "official_api_documentation": None,
41 "use_official_api": False,
42 "require_api_key": False,
43 "results": "JSON",
44}
45
46# if there is a need for globals, use a leading underline
47_my_offline_engine: str = ""
48
49CACHE: EngineCache
50"""Persistent (SQLite) key/value cache that deletes its values after ``expire``
51seconds."""
52
53
54def setup(engine_settings: dict[str, t.Any]) -> bool:
55 """Dynamic setup of the engine settings.
56
57 The origin of this demo engine is a simple json string which is loaded in
58 this example while the engine is initialized.
59
60 For more details see :py:obj:`searx.enginelib.Engine.setup`.
61 """
62 global _my_offline_engine, CACHE # pylint: disable=global-statement
63
64 CACHE = EngineCache(engine_settings["name"])
65
66 _my_offline_engine = (
67 '[ {"value": "%s"}'
68 ', {"value":"first item"}'
69 ', {"value":"second item"}'
70 ', {"value":"third item"}'
71 ']' % engine_settings.get('name')
72 )
73
74 return True
75
76
77def init(engine_settings: dict[str, t.Any]) -> bool: # pylint: disable=unused-argument
78 """Initialization of the engine.
79
80 For more details see :py:obj:`searx.enginelib.Engine.init`.
81 """
82 return True
83
84
85def search(query: str, params: "RequestParams") -> EngineResults:
86 """Query (offline) engine and return results. Assemble the list of results
87 from your local engine.
88
89 In this demo engine we ignore the 'query' term, usual you would pass the
90 'query' term to your local engine to filter out the results.
91 """
92 res = EngineResults()
93
94 count: int = CACHE.get("count", 0)
95 data_rows: list[dict[str, str]] = json.loads(_my_offline_engine)
96
97 for row in data_rows:
98 count += 1
99 kvmap = {
100 'query': query,
101 'language': params['searxng_locale'],
102 'value': row.get("value"),
103 }
104 res.add(
105 res.types.KeyValue(
106 caption=f"Demo Offline Engine Result #{count}",
107 key_title="Name",
108 value_title="Value",
109 kvmap=kvmap,
110 )
111 )
112 res.add(res.types.LegacyResult(number_of_results=count))
113
114 # cache counter value for 20sec
115 CACHE.set("count", count, expire=20)
116 return res
bool setup(dict[str, t.Any] engine_settings)
bool init(dict[str, t.Any] engine_settings)