bing_videos.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # lint: pylint
  3. """Bing-Videos: 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. from searx.engines.bing_images import time_map
  14. if TYPE_CHECKING:
  15. import logging
  16. logger: logging.Logger
  17. traits: EngineTraits
  18. about = {
  19. "website": 'https://www.bing.com/videos',
  20. "wikidata_id": 'Q4914152',
  21. "official_api_documentation": 'https://www.microsoft.com/en-us/bing/apis/bing-video-search-api',
  22. "use_official_api": False,
  23. "require_api_key": False,
  24. "results": 'HTML',
  25. }
  26. # engine dependent config
  27. categories = ['videos', 'web']
  28. paging = True
  29. safesearch = True
  30. time_range_support = True
  31. base_url = 'https://www.bing.com/videos/asyncv2'
  32. """Bing (Videos) async search URL."""
  33. def request(query, params):
  34. """Assemble a Bing-Video request."""
  35. engine_region = traits.get_region(params['searxng_locale'], traits.all_locale) # type: ignore
  36. engine_language = traits.get_language(params['searxng_locale'], 'en') # type: ignore
  37. set_bing_cookies(params, engine_language, engine_region)
  38. # build URL query
  39. #
  40. # example: https://www.bing.com/videos/asyncv2?q=foo&async=content&first=1&count=35
  41. query_params = {
  42. 'q': query,
  43. 'async': 'content',
  44. # to simplify the page count lets use the default of 35 images per page
  45. 'first': (int(params.get('pageno', 1)) - 1) * 35 + 1,
  46. 'count': 35,
  47. }
  48. # time range
  49. #
  50. # example: one week (10080 minutes) '&qft= filterui:videoage-lt10080' '&form=VRFLTR'
  51. if params['time_range']:
  52. query_params['form'] = 'VRFLTR'
  53. query_params['qft'] = ' filterui:videoage-lt%s' % time_map[params['time_range']]
  54. params['url'] = base_url + '?' + urlencode(query_params)
  55. return params
  56. def response(resp):
  57. """Get response from Bing-Video"""
  58. results = []
  59. dom = html.fromstring(resp.text)
  60. for result in dom.xpath('//div[@class="dg_u"]//div[contains(@id, "mc_vtvc_video")]'):
  61. metadata = json.loads(result.xpath('.//div[@class="vrhdata"]/@vrhm')[0])
  62. info = ' - '.join(result.xpath('.//div[@class="mc_vtvc_meta_block"]//span/text()')).strip()
  63. content = '{0} - {1}'.format(metadata['du'], info)
  64. thumbnail = result.xpath('.//div[contains(@class, "mc_vtvc_th")]//img/@src')[0]
  65. results.append(
  66. {
  67. 'url': metadata['murl'],
  68. 'thumbnail': thumbnail,
  69. 'title': metadata.get('vt', ''),
  70. 'content': content,
  71. 'template': 'videos.html',
  72. }
  73. )
  74. return results