bing_videos.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """
  3. Bing (Videos)
  4. """
  5. from json import loads
  6. from lxml import html
  7. from urllib.parse import urlencode
  8. from searx.utils import match_language
  9. from searx.engines.bing import language_aliases
  10. from searx.engines.bing import _fetch_supported_languages, supported_languages_url # NOQA # pylint: disable=unused-import
  11. # about
  12. about = {
  13. "website": 'https://www.bing.com/videos',
  14. "wikidata_id": 'Q4914152',
  15. "official_api_documentation": 'https://www.microsoft.com/en-us/bing/apis/bing-video-search-api',
  16. "use_official_api": False,
  17. "require_api_key": False,
  18. "results": 'HTML',
  19. }
  20. categories = ['videos']
  21. paging = True
  22. safesearch = True
  23. time_range_support = True
  24. number_of_results = 28
  25. base_url = 'https://www.bing.com/'
  26. search_string = 'videos/search'\
  27. '?{query}'\
  28. '&count={count}'\
  29. '&first={first}'\
  30. '&scope=video'\
  31. '&FORM=QBLH'
  32. time_range_string = '&qft=+filterui:videoage-lt{interval}'
  33. time_range_dict = {'day': '1440',
  34. 'week': '10080',
  35. 'month': '43200',
  36. 'year': '525600'}
  37. # safesearch definitions
  38. safesearch_types = {2: 'STRICT',
  39. 1: 'DEMOTE',
  40. 0: 'OFF'}
  41. # do search-request
  42. def request(query, params):
  43. offset = ((params['pageno'] - 1) * number_of_results) + 1
  44. search_path = search_string.format(
  45. query=urlencode({'q': query}),
  46. count=number_of_results,
  47. first=offset)
  48. # safesearch cookie
  49. params['cookies']['SRCHHPGUSR'] = \
  50. 'ADLT=' + safesearch_types.get(params['safesearch'], 'DEMOTE')
  51. # language cookie
  52. language = match_language(params['language'], supported_languages, language_aliases).lower()
  53. params['cookies']['_EDGE_S'] = 'mkt=' + language + '&F=1'
  54. # query and paging
  55. params['url'] = base_url + search_path
  56. # time range
  57. if params['time_range'] in time_range_dict:
  58. params['url'] += time_range_string.format(interval=time_range_dict[params['time_range']])
  59. # bing videos did not like "older" versions < 70.0.1 when selectin other
  60. # languages then 'en' .. very strange ?!?!
  61. params['headers']['User-Agent'] = 'Mozilla/5.0 (X11; Linux x86_64; rv:73.0.1) Gecko/20100101 Firefox/73.0.1'
  62. return params
  63. # get response from search-request
  64. def response(resp):
  65. results = []
  66. dom = html.fromstring(resp.text)
  67. for result in dom.xpath('//div[@class="dg_u"]'):
  68. try:
  69. metadata = loads(result.xpath('.//div[@class="vrhdata"]/@vrhm')[0])
  70. info = ' - '.join(result.xpath('.//div[@class="mc_vtvc_meta_block"]//span/text()')).strip()
  71. content = '{0} - {1}'.format(metadata['du'], info)
  72. thumbnail = '{0}th?id={1}'.format(base_url, metadata['thid'])
  73. results.append({'url': metadata['murl'],
  74. 'thumbnail': thumbnail,
  75. 'title': metadata.get('vt', ''),
  76. 'content': content,
  77. 'template': 'videos.html'})
  78. except:
  79. continue
  80. return results