bing_images.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. 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', 'web']
  25. paging = True
  26. safesearch = True
  27. time_range_support = True
  28. send_accept_language_header = True
  29. supported_languages_url = 'https://www.bing.com/account/general'
  30. number_of_results = 28
  31. # search-url
  32. base_url = 'https://www.bing.com/'
  33. search_string = (
  34. # fmt: off
  35. 'images/search'
  36. '?{query}'
  37. '&count={count}'
  38. '&first={first}'
  39. '&tsc=ImageHoverTitle'
  40. # fmt: on
  41. )
  42. time_range_string = '&qft=+filterui:age-lt{interval}'
  43. time_range_dict = {'day': '1440', 'week': '10080', 'month': '43200', 'year': '525600'}
  44. # safesearch definitions
  45. safesearch_types = {2: 'STRICT', 1: 'DEMOTE', 0: 'OFF'}
  46. # do search-request
  47. def request(query, params):
  48. offset = ((params['pageno'] - 1) * number_of_results) + 1
  49. search_path = search_string.format(query=urlencode({'q': query}), count=number_of_results, first=offset)
  50. language = match_language(params['language'], supported_languages, language_aliases).lower()
  51. params['cookies']['SRCHHPGUSR'] = 'ADLT=' + safesearch_types.get(params['safesearch'], 'DEMOTE')
  52. params['cookies']['_EDGE_S'] = 'mkt=' + language + '&ui=' + language + '&F=1'
  53. params['url'] = base_url + search_path
  54. if params['time_range'] in time_range_dict:
  55. params['url'] += time_range_string.format(interval=time_range_dict[params['time_range']])
  56. return params
  57. # get response from search-request
  58. def response(resp):
  59. results = []
  60. dom = html.fromstring(resp.text)
  61. # parse results
  62. for result in dom.xpath('//div[@class="imgpt"]'):
  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. return results