You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
3.5 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. from .common import InfoExtractor
  4. from ..compat import compat_HTTPError
  5. from ..utils import (
  6. float_or_none,
  7. ExtractorError,
  8. )
  9. class RedBullTVIE(InfoExtractor):
  10. _VALID_URL = r'https?://(?:www\.)?redbull(?:\.tv|\.com/(?:[^/]+/)?tv)/video/(?P<id>AP-\w+)'
  11. _TESTS = [{
  12. # film
  13. 'url': 'https://www.redbull.tv/video/AP-1Q6XCDTAN1W11',
  14. 'md5': 'fb0445b98aa4394e504b413d98031d1f',
  15. 'info_dict': {
  16. 'id': 'AP-1Q6XCDTAN1W11',
  17. 'ext': 'mp4',
  18. 'title': 'ABC of... WRC - ABC of... S1E6',
  19. 'description': 'md5:5c7ed8f4015c8492ecf64b6ab31e7d31',
  20. 'duration': 1582.04,
  21. },
  22. }, {
  23. # episode
  24. 'url': 'https://www.redbull.tv/video/AP-1PMHKJFCW1W11',
  25. 'info_dict': {
  26. 'id': 'AP-1PMHKJFCW1W11',
  27. 'ext': 'mp4',
  28. 'title': 'Grime - Hashtags S2E4',
  29. 'description': 'md5:b5f522b89b72e1e23216e5018810bb25',
  30. 'duration': 904.6,
  31. },
  32. 'params': {
  33. 'skip_download': True,
  34. },
  35. }, {
  36. 'url': 'https://www.redbull.com/int-en/tv/video/AP-1UWHCAR9S1W11/rob-meets-sam-gaze?playlist=playlists::3f81040a-2f31-4832-8e2e-545b1d39d173',
  37. 'only_matching': True,
  38. }]
  39. def _real_extract(self, url):
  40. video_id = self._match_id(url)
  41. session = self._download_json(
  42. 'https://api.redbull.tv/v3/session', video_id,
  43. note='Downloading access token', query={
  44. 'category': 'personal_computer',
  45. 'os_family': 'http',
  46. })
  47. if session.get('code') == 'error':
  48. raise ExtractorError('%s said: %s' % (
  49. self.IE_NAME, session['message']))
  50. token = session['token']
  51. try:
  52. video = self._download_json(
  53. 'https://api.redbull.tv/v3/products/' + video_id,
  54. video_id, note='Downloading video information',
  55. headers={'Authorization': token}
  56. )
  57. except ExtractorError as e:
  58. if isinstance(e.cause, compat_HTTPError) and e.cause.code == 404:
  59. error_message = self._parse_json(
  60. e.cause.read().decode(), video_id)['error']
  61. raise ExtractorError('%s said: %s' % (
  62. self.IE_NAME, error_message), expected=True)
  63. raise
  64. title = video['title'].strip()
  65. formats = self._extract_m3u8_formats(
  66. 'https://dms.redbull.tv/v3/%s/%s/playlist.m3u8' % (video_id, token),
  67. video_id, 'mp4', entry_protocol='m3u8_native', m3u8_id='hls')
  68. self._sort_formats(formats)
  69. subtitles = {}
  70. for resource in video.get('resources', []):
  71. if resource.startswith('closed_caption_'):
  72. splitted_resource = resource.split('_')
  73. if splitted_resource[2]:
  74. subtitles.setdefault('en', []).append({
  75. 'url': 'https://resources.redbull.tv/%s/%s' % (video_id, resource),
  76. 'ext': splitted_resource[2],
  77. })
  78. subheading = video.get('subheading')
  79. if subheading:
  80. title += ' - %s' % subheading
  81. return {
  82. 'id': video_id,
  83. 'title': title,
  84. 'description': video.get('long_description') or video.get(
  85. 'short_description'),
  86. 'duration': float_or_none(video.get('duration'), scale=1000),
  87. 'formats': formats,
  88. 'subtitles': subtitles,
  89. }