duckduckgo_images.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """
  3. DuckDuckGo (Images)
  4. """
  5. from json import loads
  6. from urllib.parse import urlencode
  7. from searx.exceptions import SearxEngineAPIException
  8. from searx.engines.duckduckgo import get_region_code
  9. from searx.engines.duckduckgo import ( # pylint: disable=unused-import
  10. _fetch_supported_languages,
  11. supported_languages_url,
  12. )
  13. from searx.network import get
  14. # about
  15. about = {
  16. "website": 'https://duckduckgo.com/',
  17. "wikidata_id": 'Q12805',
  18. "official_api_documentation": {
  19. 'url': 'https://duckduckgo.com/api',
  20. 'comment': 'but images are not supported',
  21. },
  22. "use_official_api": False,
  23. "require_api_key": False,
  24. "results": 'JSON (site requires js to get images)',
  25. }
  26. # engine dependent config
  27. categories = ['images']
  28. paging = True
  29. safesearch = True
  30. # search-url
  31. images_url = 'https://duckduckgo.com/i.js?{query}&s={offset}&p={safesearch}&o=json&vqd={vqd}'
  32. site_url = 'https://duckduckgo.com/?{query}&iar=images&iax=1&ia=images'
  33. # run query in site to get vqd number needed for requesting images
  34. # TODO: find a way to get this number without an extra request (is it a hash of the query?)
  35. def get_vqd(query, headers):
  36. query_url = site_url.format(query=urlencode({'q': query}))
  37. res = get(query_url, headers=headers)
  38. content = res.text
  39. if content.find('vqd=\'') == -1:
  40. raise SearxEngineAPIException('Request failed')
  41. vqd = content[content.find('vqd=\'') + 5 :]
  42. vqd = vqd[: vqd.find('\'')]
  43. return vqd
  44. # do search-request
  45. def request(query, params):
  46. # to avoid running actual external requests when testing
  47. if 'is_test' not in params:
  48. vqd = get_vqd(query, params['headers'])
  49. else:
  50. vqd = '12345'
  51. offset = (params['pageno'] - 1) * 50
  52. safesearch = params['safesearch'] - 1
  53. region_code = get_region_code(params['language'], lang_list=supported_languages)
  54. if region_code:
  55. params['url'] = images_url.format(
  56. query=urlencode({'q': query, 'l': region_code}), offset=offset, safesearch=safesearch, vqd=vqd
  57. )
  58. else:
  59. params['url'] = images_url.format(query=urlencode({'q': query}), offset=offset, safesearch=safesearch, vqd=vqd)
  60. return params
  61. # get response from search-request
  62. def response(resp):
  63. results = []
  64. content = resp.text
  65. res_json = loads(content)
  66. # parse results
  67. for result in res_json['results']:
  68. title = result['title']
  69. url = result['url']
  70. thumbnail = result['thumbnail']
  71. image = result['image']
  72. # append result
  73. results.append(
  74. {
  75. 'template': 'images.html',
  76. 'title': title,
  77. 'content': '',
  78. 'thumbnail_src': thumbnail,
  79. 'img_src': image,
  80. 'url': url,
  81. }
  82. )
  83. return results