.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. To get in use of this
4*demo* engine add the following entry to your engines list in ``settings.yml``:
5
6.. code:: yaml
7
8 - name: my offline engine
9 engine: demo_offline
10 shortcut: demo
11 disabled: false
12
13"""
14
15import typing as t
16import json
17
18from searx.result_types import EngineResults
19from searx.enginelib import EngineCache
20
21engine_type = "offline"
22categories = ["general"]
23disabled = True
24timeout = 2.0
25
26about = {
27 "wikidata_id": None,
28 "official_api_documentation": None,
29 "use_official_api": False,
30 "require_api_key": False,
31 "results": 'JSON',
32}
33
34# if there is a need for globals, use a leading underline
35_my_offline_engine: str = ""
36
37CACHE: EngineCache
38"""Persistent (SQLite) key/value cache that deletes its values after ``expire``
39seconds."""
40
41
42def init(engine_settings: dict[str, t.Any]) -> None:
43 """Initialization of the (offline) engine. The origin of this demo engine is a
44 simple json string which is loaded in this example while the engine is
45 initialized."""
46 global _my_offline_engine, CACHE # pylint: disable=global-statement
47
48 CACHE = EngineCache(engine_settings["name"])
49
50 _my_offline_engine = (
51 '[ {"value": "%s"}'
52 ', {"value":"first item"}'
53 ', {"value":"second item"}'
54 ', {"value":"third item"}'
55 ']' % engine_settings.get('name')
56 )
57
58
59def search(query: str, params: dict[str, t.Any]) -> EngineResults:
60 """Query (offline) engine and return results. Assemble the list of results
61 from your local engine. In this demo engine we ignore the 'query' term,
62 usual you would pass the 'query' term to your local engine to filter out the
63 results.
64 """
65 res = EngineResults()
66
67 count: int = CACHE.get("count", 0)
68 data_rows: list[dict[str, str]] = json.loads(_my_offline_engine)
69
70 for row in data_rows:
71 count += 1
72 kvmap = {
73 'query': query,
74 'language': params['searxng_locale'],
75 'value': row.get("value"),
76 }
77 res.add(
78 res.types.KeyValue(
79 caption=f"Demo Offline Engine Result #{count}",
80 key_title="Name",
81 value_title="Value",
82 kvmap=kvmap,
83 )
84 )
85 res.add(res.types.LegacyResult(number_of_results=count))
86
87 # cache counter value for 20sec
88 CACHE.set("count", count, expire=20)
89 return res
None init(dict[str, t.Any] engine_settings)