bing_videos.py 2.8 KB

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