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