bing_images.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # lint: pylint
  3. """Bing (Images)
  4. """
  5. from json import loads
  6. from urllib.parse import urlencode
  7. from lxml import html
  8. from searx.utils import match_language
  9. from searx.engines.bing import language_aliases
  10. from searx.engines.bing import ( # pylint: disable=unused-import
  11. _fetch_supported_languages,
  12. fetch_traits,
  13. supported_languages_url,
  14. )
  15. # about
  16. about = {
  17. "website": 'https://www.bing.com/images',
  18. "wikidata_id": 'Q182496',
  19. "official_api_documentation": 'https://www.microsoft.com/en-us/bing/apis/bing-image-search-api',
  20. "use_official_api": False,
  21. "require_api_key": False,
  22. "results": 'HTML',
  23. }
  24. # engine dependent config
  25. categories = ['images', 'web']
  26. paging = True
  27. safesearch = True
  28. time_range_support = True
  29. send_accept_language_header = True
  30. supported_languages_url = 'https://www.bing.com/account/general'
  31. number_of_results = 28
  32. # search-url
  33. base_url = 'https://www.bing.com/'
  34. search_string = (
  35. # fmt: off
  36. 'images/search'
  37. '?{query}'
  38. '&count={count}'
  39. '&first={first}'
  40. '&tsc=ImageHoverTitle'
  41. # fmt: on
  42. )
  43. time_range_string = '&qft=+filterui:age-lt{interval}'
  44. time_range_dict = {'day': '1440', 'week': '10080', 'month': '43200', 'year': '525600'}
  45. # safesearch definitions
  46. safesearch_types = {2: 'STRICT', 1: 'DEMOTE', 0: 'OFF'}
  47. # do search-request
  48. def request(query, params):
  49. offset = ((params['pageno'] - 1) * number_of_results) + 1
  50. search_path = search_string.format(query=urlencode({'q': query}), count=number_of_results, first=offset)
  51. language = match_language(params['language'], supported_languages, language_aliases).lower()
  52. params['cookies']['SRCHHPGUSR'] = 'ADLT=' + safesearch_types.get(params['safesearch'], 'DEMOTE')
  53. params['cookies']['_EDGE_S'] = 'mkt=' + language + '&ui=' + language + '&F=1'
  54. params['url'] = base_url + search_path
  55. if params['time_range'] in time_range_dict:
  56. params['url'] += time_range_string.format(interval=time_range_dict[params['time_range']])
  57. return params
  58. # get response from search-request
  59. def response(resp):
  60. results = []
  61. dom = html.fromstring(resp.text)
  62. # parse results
  63. for result in dom.xpath('//div[@class="imgpt"]'):
  64. img_format = result.xpath('./div[contains(@class, "img_info")]/span/text()')[0]
  65. # Microsoft seems to experiment with this code so don't make the path too specific,
  66. # just catch the text section for the first anchor in img_info assuming this to be
  67. # the originating site.
  68. source = result.xpath('./div[contains(@class, "img_info")]//a/text()')[0]
  69. m = loads(result.xpath('./a/@m')[0])
  70. # strip 'Unicode private use area' highlighting, they render to Tux
  71. # the Linux penguin and a standing diamond on my machine...
  72. title = m.get('t', '').replace('\ue000', '').replace('\ue001', '')
  73. results.append(
  74. {
  75. 'template': 'images.html',
  76. 'url': m['purl'],
  77. 'thumbnail_src': m['turl'],
  78. 'img_src': m['murl'],
  79. 'content': '',
  80. 'title': title,
  81. 'source': source,
  82. 'img_format': img_format,
  83. }
  84. )
  85. return results