bing_images.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """Bing-Images: description see :py:obj:`searx.engines.bing`.
  3. """
  4. # pylint: disable=invalid-name
  5. from typing import TYPE_CHECKING
  6. import json
  7. from urllib.parse import urlencode
  8. from lxml import html
  9. from searx.enginelib.traits import EngineTraits
  10. from searx.engines.bing import set_bing_cookies
  11. from searx.engines.bing import fetch_traits # pylint: disable=unused-import
  12. if TYPE_CHECKING:
  13. import logging
  14. logger = logging.getLogger()
  15. traits: EngineTraits
  16. # about
  17. about = {
  18. "website": 'https://www.bing.com/images',
  19. "wikidata_id": 'Q182496',
  20. "official_api_documentation": 'https://www.microsoft.com/en-us/bing/apis/bing-image-search-api',
  21. "use_official_api": False,
  22. "require_api_key": False,
  23. "results": 'HTML',
  24. }
  25. # engine dependent config
  26. categories = ['images', 'web']
  27. paging = True
  28. safesearch = True
  29. time_range_support = True
  30. base_url = 'https://www.bing.com/images/async'
  31. """Bing (Images) search URL"""
  32. time_map = {
  33. 'day': 60 * 24,
  34. 'week': 60 * 24 * 7,
  35. 'month': 60 * 24 * 31,
  36. 'year': 60 * 24 * 365,
  37. }
  38. def request(query, params):
  39. """Assemble a Bing-Image request."""
  40. engine_region = traits.get_region(params['searxng_locale'], traits.all_locale) # type: ignore
  41. engine_language = traits.get_language(params['searxng_locale'], 'en') # type: ignore
  42. set_bing_cookies(params, engine_language, engine_region)
  43. # build URL query
  44. # - example: https://www.bing.com/images/async?q=foo&async=content&first=1&count=35
  45. query_params = {
  46. 'q': query,
  47. 'async': '1',
  48. # to simplify the page count lets use the default of 35 images per page
  49. 'first': (int(params.get('pageno', 1)) - 1) * 35 + 1,
  50. 'count': 35,
  51. }
  52. # time range
  53. # - example: one year (525600 minutes) 'qft=+filterui:age-lt525600'
  54. if params['time_range']:
  55. query_params['qft'] = 'filterui:age-lt%s' % time_map[params['time_range']]
  56. params['url'] = base_url + '?' + urlencode(query_params)
  57. return params
  58. def response(resp):
  59. """Get response from Bing-Images"""
  60. results = []
  61. dom = html.fromstring(resp.text)
  62. for result in dom.xpath('//ul[contains(@class, "dgControl_list")]/li'):
  63. metadata = result.xpath('.//a[@class="iusc"]/@m')
  64. if not metadata:
  65. continue
  66. metadata = json.loads(result.xpath('.//a[@class="iusc"]/@m')[0])
  67. title = ' '.join(result.xpath('.//div[@class="infnmpt"]//a/text()')).strip()
  68. img_format = ' '.join(result.xpath('.//div[@class="imgpt"]/div/span/text()')).strip().split(" · ")
  69. source = ' '.join(result.xpath('.//div[@class="imgpt"]//div[@class="lnkw"]//a/text()')).strip()
  70. results.append(
  71. {
  72. 'template': 'images.html',
  73. 'url': metadata['purl'],
  74. 'thumbnail_src': metadata['turl'],
  75. 'img_src': metadata['murl'],
  76. 'content': metadata['desc'],
  77. 'title': title,
  78. 'source': source,
  79. 'resolution': img_format[0],
  80. 'img_format': img_format[1] if len(img_format) >= 2 else None,
  81. }
  82. )
  83. return results