bing_videos.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. """
  2. Bing (Videos)
  3. @website https://www.bing.com/videos
  4. @provide-api yes (http://datamarket.azure.com/dataset/bing/search)
  5. @using-api no
  6. @results HTML
  7. @stable no
  8. @parse url, title, content, thumbnail
  9. """
  10. from json import loads
  11. from lxml import html
  12. from searx.engines.bing_images import _fetch_supported_languages, supported_languages_url
  13. from searx.url_utils import urlencode
  14. from searx.utils import match_language
  15. categories = ['videos']
  16. paging = True
  17. safesearch = True
  18. time_range_support = True
  19. number_of_results = 28
  20. language_support = True
  21. base_url = 'https://www.bing.com/'
  22. search_string = 'videos/search'\
  23. '?{query}'\
  24. '&count={count}'\
  25. '&first={first}'\
  26. '&scope=video'\
  27. '&FORM=QBLH'
  28. time_range_string = '&qft=+filterui:videoage-lt{interval}'
  29. time_range_dict = {'day': '1440',
  30. 'week': '10080',
  31. 'month': '43200',
  32. 'year': '525600'}
  33. # safesearch definitions
  34. safesearch_types = {2: 'STRICT',
  35. 1: 'DEMOTE',
  36. 0: 'OFF'}
  37. # do search-request
  38. def request(query, params):
  39. offset = ((params['pageno'] - 1) * number_of_results) + 1
  40. search_path = search_string.format(
  41. query=urlencode({'q': query}),
  42. count=number_of_results,
  43. first=offset)
  44. # safesearch cookie
  45. params['cookies']['SRCHHPGUSR'] = \
  46. 'ADLT=' + safesearch_types.get(params['safesearch'], 'DEMOTE')
  47. # language cookie
  48. language = match_language(params['language'], supported_languages, language_aliases).lower()
  49. params['cookies']['_EDGE_S'] = 'mkt=' + language + '&F=1'
  50. # query and paging
  51. params['url'] = base_url + search_path
  52. # time range
  53. if params['time_range'] in time_range_dict:
  54. params['url'] += time_range_string.format(interval=time_range_dict[params['time_range']])
  55. return params
  56. # get response from search-request
  57. def response(resp):
  58. results = []
  59. dom = html.fromstring(resp.text)
  60. for result in dom.xpath('//div[@class="dg_u"]'):
  61. try:
  62. metadata = loads(result.xpath('.//div[@class="vrhdata"]/@vrhm')[0])
  63. info = ' - '.join(result.xpath('.//div[@class="mc_vtvc_meta_block"]//span/text()')).strip()
  64. content = '{0} - {1}'.format(metadata['du'], info)
  65. thumbnail = '{0}th?id={1}'.format(base_url, metadata['thid'])
  66. results.append({'url': metadata['murl'],
  67. 'thumbnail': thumbnail,
  68. 'title': metadata.get('vt', ''),
  69. 'content': content,
  70. 'template': 'videos.html'})
  71. except:
  72. continue
  73. return results