demo_online.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """Within this module we implement a *demo online engine*. Do not look to
  3. close to the implementation, its just a simple example which queries `The Art
  4. Institute of Chicago <https://www.artic.edu>`_
  5. To get in use of this *demo* engine add the following entry to your engines
  6. list in ``settings.yml``:
  7. .. code:: yaml
  8. - name: my online engine
  9. engine: demo_online
  10. shortcut: demo
  11. disabled: false
  12. """
  13. from json import loads
  14. from urllib.parse import urlencode
  15. from searx.result_types import EngineResults
  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) -> EngineResults:
  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. res = EngineResults()
  62. json_data = loads(resp.text)
  63. res.add(
  64. res.types.Answer(
  65. answer="this is a dummy answer ..",
  66. url="https://example.org",
  67. )
  68. )
  69. for result in json_data['data']:
  70. if not result['image_id']:
  71. continue
  72. res.append(
  73. {
  74. 'url': 'https://artic.edu/artworks/%(id)s' % result,
  75. 'title': result['title'] + " (%(date_display)s) // %(artist_display)s" % result,
  76. 'content': "%(medium_display)s // %(dimensions)s" % result,
  77. 'author': ', '.join(result['artist_titles']),
  78. 'img_src': image_api + '/%(image_id)s/full/843,/0/default.jpg' % result,
  79. 'template': 'images.html',
  80. }
  81. )
  82. return res