.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
demo_online.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 online engine*. Do not look to
3close to the implementation, its just a simple example which queries `The Art
4Institute of Chicago <https://www.artic.edu>`_
5
6Configuration
7=============
8
9To get in use of this *demo* engine add the following entry to your engines
10list in ``settings.yml``:
11
12.. code:: yaml
13
14 - name: my online engine
15 engine: demo_online
16 shortcut: demo
17 disabled: false
18
19Implementations
20===============
21
22"""
23
24import typing as t
25
26from urllib.parse import urlencode
27from searx.result_types import EngineResults
28
29if t.TYPE_CHECKING:
30 from searx.extended_types import SXNG_Response
31 from searx.search.processors import OnlineParams
32
33
34engine_type = "online"
35send_accept_language_header = True
36categories = ["general"]
37disabled = True
38timeout = 2.0
39categories = ["images"]
40paging = True
41page_size = 20
42
43search_api = "https://api.artic.edu/api/v1/artworks/search"
44image_api = "https://www.artic.edu/iiif/2/"
45
46about = {
47 "website": "https://www.artic.edu",
48 "wikidata_id": "Q239303",
49 "official_api_documentation": "http://api.artic.edu/docs/",
50 "use_official_api": True,
51 "require_api_key": False,
52 "results": "JSON",
53}
54
55
56# if there is a need for globals, use a leading underline
57_my_online_engine = None
58
59
60def setup(engine_settings: "OnlineParams") -> bool:
61 """Dynamic setup of the engine settings.
62
63 For more details see :py:obj:`searx.enginelib.Engine.setup`."""
64 global _my_online_engine # pylint: disable=global-statement
65 _my_online_engine = engine_settings.get("name")
66 return True
67
68
69def init(engine_settings: dict[str, t.Any]) -> bool: # pylint: disable=unused-argument
70 """Initialization of the engine.
71
72 For more details see :py:obj:`searx.enginelib.Engine.init`."""
73 return True
74
75
76def request(query: str, params: "OnlineParams") -> None:
77 """Build up the ``params`` for the online request. In this example we build a
78 URL to fetch images from `artic.edu <https://artic.edu>`__."""
79 args = urlencode(
80 {
81 "q": query,
82 "page": params["pageno"],
83 "fields": "id,title,artist_display,medium_display,image_id,date_display,dimensions,artist_titles",
84 "limit": page_size,
85 }
86 )
87 params["url"] = f"{search_api}?{args}"
88
89
90def response(resp: "SXNG_Response") -> EngineResults:
91 """Parse out the result items from the response. In this example we parse the
92 response from `api.artic.edu <https://artic.edu>`__ and filter out all
93 images.
94
95 """
96 res = EngineResults()
97 json_data = resp.json()
98
99 res.add(
100 res.types.Answer(
101 answer="this is a dummy answer ..",
102 url="https://example.org",
103 )
104 )
105
106 for result in json_data["data"]:
107
108 if not result["image_id"]:
109 continue
110
111 kwargs: dict[str, t.Any] = {
112 "url": "https://artic.edu/artworks/%(id)s" % result,
113 "title": result["title"] + " (%(date_display)s) // %(artist_display)s" % result,
114 "content": "%(medium_display)s // %(dimensions)s" % result,
115 "author": ", ".join(result["artist_titles"]),
116 "img_src": image_api + "/%(image_id)s/full/843,/0/default.jpg" % result,
117 "template": "images.html",
118 }
119
120 res.add(res.types.LegacyResult(**kwargs))
121
122 return res
None request(str query, "OnlineParams" params)
EngineResults response("SXNG_Response" resp)
bool init(dict[str, t.Any] engine_settings)
bool setup("OnlineParams" engine_settings)