demo_online.py 2.7 KB

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