demo_online.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 = 'offline'
  17. categories = ['general']
  18. disabled = True
  19. timeout = 2.0
  20. categories = ['images']
  21. paging = True
  22. page_size = 20
  23. search_api = 'https://api.artic.edu/api/v1/artworks/search?'
  24. image_api = 'https://www.artic.edu/iiif/2/'
  25. about = {
  26. "website": 'https://www.artic.edu',
  27. "wikidata_id": 'Q239303',
  28. "official_api_documentation": 'http://api.artic.edu/docs/',
  29. "use_official_api": True,
  30. "require_api_key": False,
  31. "results": 'JSON',
  32. }
  33. # if there is a need for globals, use a leading underline
  34. _my_online_engine = None
  35. def init(engine_settings):
  36. """Initialization of the (online) engine. If no initialization is needed, drop
  37. this init function.
  38. """
  39. global _my_online_engine # pylint: disable=global-statement
  40. _my_online_engine = engine_settings.get('name')
  41. def request(query, params):
  42. """Build up the ``params`` for the online request. In this example we build a
  43. URL to fetch images from `artic.edu <https://artic.edu>`__
  44. """
  45. args = urlencode(
  46. {
  47. 'q': query,
  48. 'page': params['pageno'],
  49. 'fields': 'id,title,artist_display,medium_display,image_id,date_display,dimensions,artist_titles',
  50. 'limit': page_size,
  51. }
  52. )
  53. params['url'] = search_api + args
  54. return params
  55. def response(resp):
  56. """Parse out the result items from the response. In this example we parse the
  57. response from `api.artic.edu <https://artic.edu>`__ and filter out all
  58. images.
  59. """
  60. results = []
  61. json_data = loads(resp.text)
  62. for result in json_data['data']:
  63. if not result['image_id']:
  64. continue
  65. results.append(
  66. {
  67. 'url': 'https://artic.edu/artworks/%(id)s' % result,
  68. 'title': result['title'] + " (%(date_display)s) // %(artist_display)s" % result,
  69. 'content': result['medium_display'],
  70. 'author': ', '.join(result['artist_titles']),
  71. 'img_src': image_api + '/%(image_id)s/full/843,/0/default.jpg' % result,
  72. 'img_format': result['dimensions'],
  73. 'template': 'images.html',
  74. }
  75. )
  76. return results