bing_videos.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 (
  12. set_bing_cookies,
  13. _fetch_traits,
  14. )
  15. if TYPE_CHECKING:
  16. import logging
  17. logger: logging.Logger
  18. traits: EngineTraits
  19. about = {
  20. "website": 'https://www.bing.com/videos',
  21. "wikidata_id": 'Q4914152',
  22. "official_api_documentation": 'https://www.microsoft.com/en-us/bing/apis/bing-video-search-api',
  23. "use_official_api": False,
  24. "require_api_key": False,
  25. "results": 'HTML',
  26. }
  27. # engine dependent config
  28. categories = ['videos', 'web']
  29. paging = True
  30. safesearch = True
  31. time_range_support = True
  32. base_url = 'https://www.bing.com/videos/asyncv2'
  33. """Bing (Videos) async search URL."""
  34. bing_traits_url = 'https://learn.microsoft.com/en-us/bing/search-apis/bing-video-search/reference/market-codes'
  35. """Bing (Video) search API description"""
  36. time_map = {
  37. # fmt: off
  38. 'day': 60 * 24,
  39. 'week': 60 * 24 * 7,
  40. 'month': 60 * 24 * 31,
  41. 'year': 60 * 24 * 365,
  42. # fmt: on
  43. }
  44. def request(query, params):
  45. """Assemble a Bing-Video request."""
  46. engine_region = traits.get_region(params['searxng_locale'], 'en-us')
  47. engine_language = traits.get_language(params['searxng_locale'], 'en-us')
  48. set_bing_cookies(params, engine_language, engine_region)
  49. # build URL query
  50. #
  51. # example: https://www.bing.com/videos/asyncv2?q=foo&async=content&first=1&count=35
  52. query_params = {
  53. # fmt: off
  54. 'q': query,
  55. 'async' : 'content',
  56. # to simplify the page count lets use the default of 35 images per page
  57. 'first' : (int(params.get('pageno', 1)) - 1) * 35 + 1,
  58. 'count' : 35,
  59. # fmt: on
  60. }
  61. # time range
  62. #
  63. # example: one week (10080 minutes) '&qft= filterui:videoage-lt10080' '&form=VRFLTR'
  64. if params['time_range']:
  65. query_params['form'] = 'VRFLTR'
  66. query_params['qft'] = ' filterui:videoage-lt%s' % time_map[params['time_range']]
  67. params['url'] = base_url + '?' + urlencode(query_params)
  68. return params
  69. def response(resp):
  70. """Get response from Bing-Video"""
  71. results = []
  72. dom = html.fromstring(resp.text)
  73. for result in dom.xpath('//div[@class="dg_u"]//div[contains(@id, "mc_vtvc_video")]'):
  74. metadata = json.loads(result.xpath('.//div[@class="vrhdata"]/@vrhm')[0])
  75. info = ' - '.join(result.xpath('.//div[@class="mc_vtvc_meta_block"]//span/text()')).strip()
  76. content = '{0} - {1}'.format(metadata['du'], info)
  77. thumbnail = result.xpath('.//div[contains(@class, "mc_vtvc_th")]//img/@src')[0]
  78. results.append(
  79. {
  80. 'url': metadata['murl'],
  81. 'thumbnail': thumbnail,
  82. 'title': metadata.get('vt', ''),
  83. 'content': content,
  84. 'template': 'videos.html',
  85. }
  86. )
  87. return results
  88. def fetch_traits(engine_traits: EngineTraits):
  89. """Fetch languages and regions from Bing-Videos."""
  90. xpath_market_codes = '//table[1]/tbody/tr/td[3]'
  91. # xpath_country_codes = '//table[2]/tbody/tr/td[2]'
  92. xpath_language_codes = '//table[3]/tbody/tr/td[2]'
  93. _fetch_traits(engine_traits, bing_traits_url, xpath_language_codes, xpath_market_codes)