bing_images.py 3.2 KB

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