summaryrefslogtreecommitdiff
path: root/searx/engines/demo_online.py
blob: e0c1af751fffc16a57da78b466c9a436d02fa083 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Within this module we implement a *demo online engine*.  Do not look to
close to the implementation, its just a simple example which queries `The Art
Institute of Chicago <https://www.artic.edu>`_

To get in use of this *demo* engine add the following entry to your engines
list in ``settings.yml``:

.. code:: yaml

  - name: my online engine
    engine: demo_online
    shortcut: demo
    disabled: false

"""

import typing as t

from json import loads
from urllib.parse import urlencode
from searx.result_types import EngineResults

if t.TYPE_CHECKING:
    from searx.extended_types import SXNG_Response


engine_type = "online"
send_accept_language_header = True
categories = ["general"]
disabled = True
timeout = 2.0
categories = ["images"]
paging = True
page_size = 20

search_api = "https://api.artic.edu/api/v1/artworks/search?"
image_api = "https://www.artic.edu/iiif/2/"

about = {
    "website": "https://www.artic.edu",
    "wikidata_id": "Q239303",
    "official_api_documentation": "http://api.artic.edu/docs/",
    "use_official_api": True,
    "require_api_key": False,
    "results": "JSON",
}


# if there is a need for globals, use a leading underline
_my_online_engine = None


def init(engine_settings: dict[str, t.Any]) -> None:
    """Initialization of the (online) engine.  If no initialization is needed, drop
    this init function."""
    global _my_online_engine  # pylint: disable=global-statement
    _my_online_engine = engine_settings.get("name")


def request(query: str, params: dict[str, t.Any]) -> None:
    """Build up the ``params`` for the online request.  In this example we build a
    URL to fetch images from `artic.edu <https://artic.edu>`__

    """
    args = urlencode(
        {
            "q": query,
            "page": params["pageno"],
            "fields": "id,title,artist_display,medium_display,image_id,date_display,dimensions,artist_titles",
            "limit": page_size,
        }
    )
    params["url"] = search_api + args


def response(resp: "SXNG_Response") -> EngineResults:
    """Parse out the result items from the response.  In this example we parse the
    response from `api.artic.edu <https://artic.edu>`__ and filter out all
    images.

    """
    res = EngineResults()
    json_data = loads(resp.text)

    res.add(
        res.types.Answer(
            answer="this is a dummy answer ..",
            url="https://example.org",
        )
    )

    for result in json_data["data"]:

        if not result["image_id"]:
            continue

        kwargs: dict[str, t.Any] = {
            "url": "https://artic.edu/artworks/%(id)s" % result,
            "title": result["title"] + " (%(date_display)s) // %(artist_display)s" % result,
            "content": "%(medium_display)s // %(dimensions)s" % result,
            "author": ", ".join(result["artist_titles"]),
            "img_src": image_api + "/%(image_id)s/full/843,/0/default.jpg" % result,
            "template": "images.html",
        }

        res.add(res.types.LegacyResult(**kwargs))

    return res