bing_images.py 3.3 KB

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