.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
3
close to the implementation, its just a simple example which queries `The Art
4
Institute of Chicago <https://www.artic.edu>`_
5
6
To get in use of this *demo* engine add the following entry to your engines
7
list in ``settings.yml``:
8
9
.. code:: yaml
10
11
- name: my online engine
12
engine: demo_online
13
shortcut: demo
14
disabled: false
15
16
"""
17
18
from
json
import
loads
19
from
urllib.parse
import
urlencode
20
21
engine_type =
'online'
22
send_accept_language_header =
True
23
categories = [
'general'
]
24
disabled =
True
25
timeout = 2.0
26
categories = [
'images'
]
27
paging =
True
28
page_size = 20
29
30
search_api =
'https://api.artic.edu/api/v1/artworks/search?'
31
image_api =
'https://www.artic.edu/iiif/2/'
32
33
about = {
34
"website"
:
'https://www.artic.edu'
,
35
"wikidata_id"
:
'Q239303'
,
36
"official_api_documentation"
:
'http://api.artic.edu/docs/'
,
37
"use_official_api"
:
True
,
38
"require_api_key"
:
False
,
39
"results"
:
'JSON'
,
40
}
41
42
43
# if there is a need for globals, use a leading underline
44
_my_online_engine =
None
45
46
47
def
init
(engine_settings):
48
"""Initialization of the (online) engine. If no initialization is needed, drop
49
this init function.
50
51
"""
52
global
_my_online_engine
# pylint: disable=global-statement
53
_my_online_engine = engine_settings.get(
'name'
)
54
55
56
def
request
(query, params):
57
"""Build up the ``params`` for the online request. In this example we build a
58
URL to fetch images from `artic.edu <https://artic.edu>`__
59
60
"""
61
args = urlencode(
62
{
63
'q'
: query,
64
'page'
: params[
'pageno'
],
65
'fields'
:
'id,title,artist_display,medium_display,image_id,date_display,dimensions,artist_titles'
,
66
'limit'
: page_size,
67
}
68
)
69
params[
'url'
] = search_api + args
70
return
params
71
72
73
def
response
(resp):
74
"""Parse out the result items from the response. In this example we parse the
75
response from `api.artic.edu <https://artic.edu>`__ and filter out all
76
images.
77
78
"""
79
results = []
80
json_data = loads(resp.text)
81
82
for
result
in
json_data[
'data'
]:
83
84
if
not
result[
'image_id'
]:
85
continue
86
87
results.append(
88
{
89
'url'
:
'https://artic.edu/artworks/%(id)s'
% result,
90
'title'
: result[
'title'
] +
" (%(date_display)s) // %(artist_display)s"
% result,
91
'content'
:
"%(medium_display)s // %(dimensions)s"
% result,
92
'author'
:
', '
.join(result[
'artist_titles'
]),
93
'img_src'
: image_api +
'/%(image_id)s/full/843,/0/default.jpg'
% result,
94
'template'
:
'images.html'
,
95
}
96
)
97
98
return
results
searx.engines.demo_online.request
request(query, params)
Definition
demo_online.py:56
searx.engines.demo_online.init
init(engine_settings)
Definition
demo_online.py:47
searx.engines.demo_online.response
response(resp)
Definition
demo_online.py:73
searxng
searx
engines
demo_online.py
Generated on Sat Nov 16 2024 00:10:57 for .oO SearXNG Developer Documentation Oo. by
1.12.0