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>`_
6To get in use of this *demo* engine add the following entry to your engines
7list in ``settings.yml``:
11 - name: my online engine
21from urllib.parse
import urlencode
29send_accept_language_header =
True
30categories = [
"general"]
33categories = [
"images"]
37search_api =
"https://api.artic.edu/api/v1/artworks/search?"
38image_api =
"https://www.artic.edu/iiif/2/"
41 "website":
"https://www.artic.edu",
42 "wikidata_id":
"Q239303",
43 "official_api_documentation":
"http://api.artic.edu/docs/",
44 "use_official_api":
True,
45 "require_api_key":
False,
51_my_online_engine =
None
54def init(engine_settings: dict[str, t.Any]) ->
None:
55 """Initialization of the (online) engine. If no initialization is needed, drop
56 this init function."""
57 global _my_online_engine
58 _my_online_engine = engine_settings.get(
"name")
61def request(query: str, params: dict[str, t.Any]) ->
None:
62 """Build up the ``params`` for the online request. In this example we build a
63 URL to fetch images from `artic.edu <https://artic.edu>`__
69 "page": params[
"pageno"],
70 "fields":
"id,title,artist_display,medium_display,image_id,date_display,dimensions,artist_titles",
74 params[
"url"] = search_api + args
77def response(resp:
"SXNG_Response") -> EngineResults:
78 """Parse out the result items from the response. In this example we parse the
79 response from `api.artic.edu <https://artic.edu>`__ and filter out all
84 json_data = loads(resp.text)
88 answer=
"this is a dummy answer ..",
89 url=
"https://example.org",
93 for result
in json_data[
"data"]:
95 if not result[
"image_id"]:
98 kwargs: dict[str, t.Any] = {
99 "url":
"https://artic.edu/artworks/%(id)s" % result,
100 "title": result[
"title"] +
" (%(date_display)s) // %(artist_display)s" % result,
101 "content":
"%(medium_display)s // %(dimensions)s" % result,
102 "author":
", ".join(result[
"artist_titles"]),
103 "img_src": image_api +
"/%(image_id)s/full/843,/0/default.jpg" % result,
104 "template":
"images.html",
107 res.add(res.types.LegacyResult(**kwargs))
None request(str query, dict[str, t.Any] params)
EngineResults response("SXNG_Response" resp)
None init(dict[str, t.Any] engine_settings)