logo

searx

My custom branche(s) on searx, a meta-search engine git clone https://hacktivis.me/git/searx.git

bing_videos.py (2715B)


  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.engines.xpath import extract_text
  14. from searx.url_utils import urlencode
  15. from searx.utils import match_language
  16. categories = ['videos']
  17. paging = True
  18. safesearch = True
  19. time_range_support = True
  20. number_of_results = 10
  21. language_support = True
  22. search_url = 'https://www.bing.com/videos/asyncv2?{query}&async=content&'\
  23. 'first={offset}&count={number_of_results}&CW=1366&CH=25&FORM=R5VR5'
  24. time_range_string = '&qft=+filterui:videoage-lt{interval}'
  25. time_range_dict = {'day': '1440',
  26. 'week': '10080',
  27. 'month': '43200',
  28. 'year': '525600'}
  29. # safesearch definitions
  30. safesearch_types = {2: 'STRICT',
  31. 1: 'DEMOTE',
  32. 0: 'OFF'}
  33. # do search-request
  34. def request(query, params):
  35. offset = (params['pageno'] - 1) * 10 + 1
  36. # safesearch cookie
  37. params['cookies']['SRCHHPGUSR'] = \
  38. 'ADLT=' + safesearch_types.get(params['safesearch'], 'DEMOTE')
  39. # language cookie
  40. language = match_language(params['language'], supported_languages).lower()
  41. params['cookies']['_EDGE_S'] = 'mkt=' + language + '&F=1'
  42. # query and paging
  43. params['url'] = search_url.format(query=urlencode({'q': query}),
  44. offset=offset,
  45. number_of_results=number_of_results)
  46. # time range
  47. if params['time_range'] in time_range_dict:
  48. params['url'] += time_range_string.format(interval=time_range_dict[params['time_range']])
  49. return params
  50. # get response from search-request
  51. def response(resp):
  52. results = []
  53. dom = html.fromstring(resp.text)
  54. for result in dom.xpath('//div[@class="dg_u"]'):
  55. url = result.xpath('./div[@class="mc_vtvc"]/a/@href')[0]
  56. url = 'https://bing.com' + url
  57. title = extract_text(result.xpath('./div/a/div/div[@class="mc_vtvc_title"]/@title'))
  58. content = extract_text(result.xpath('./div/a/div/div/div/div/text()'))
  59. thumbnail = result.xpath('./div/a/div/div/img/@src')[0]
  60. results.append({'url': url,
  61. 'title': title,
  62. 'content': content,
  63. 'thumbnail': thumbnail,
  64. 'template': 'videos.html'})
  65. if len(results) >= number_of_results:
  66. break
  67. return results