.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
6To get in use of this *demo* engine add the following entry to your engines
7list 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
18from json import loads
19from urllib.parse import urlencode
20
21engine_type = 'online'
22send_accept_language_header = True
23categories = ['general']
24disabled = True
25timeout = 2.0
26categories = ['images']
27paging = True
28page_size = 20
29
30search_api = 'https://api.artic.edu/api/v1/artworks/search?'
31image_api = 'https://www.artic.edu/iiif/2/'
32
33about = {
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
47def 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
56def 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
73def 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
init(engine_settings)