redbulltv.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. from .common import InfoExtractor
  2. from ..networking.exceptions import HTTPError
  3. from ..utils import (
  4. ExtractorError,
  5. float_or_none,
  6. )
  7. class RedBullTVIE(InfoExtractor):
  8. _VALID_URL = r'https?://(?:www\.)?redbull(?:\.tv|\.com(?:/[^/]+)?(?:/tv)?)(?:/events/[^/]+)?/(?:videos?|live|(?:film|episode)s)/(?P<id>AP-\w+)'
  9. _TESTS = [{
  10. # film
  11. 'url': 'https://www.redbull.tv/video/AP-1Q6XCDTAN1W11',
  12. 'md5': 'fb0445b98aa4394e504b413d98031d1f',
  13. 'info_dict': {
  14. 'id': 'AP-1Q6XCDTAN1W11',
  15. 'ext': 'mp4',
  16. 'title': 'ABC of... WRC - ABC of... S1E6',
  17. 'description': 'md5:5c7ed8f4015c8492ecf64b6ab31e7d31',
  18. 'duration': 1582.04,
  19. },
  20. }, {
  21. # episode
  22. 'url': 'https://www.redbull.tv/video/AP-1PMHKJFCW1W11',
  23. 'info_dict': {
  24. 'id': 'AP-1PMHKJFCW1W11',
  25. 'ext': 'mp4',
  26. 'title': 'Grime - Hashtags S2E4',
  27. 'description': 'md5:5546aa612958c08a98faaad4abce484d',
  28. 'duration': 904,
  29. },
  30. 'params': {
  31. 'skip_download': True,
  32. },
  33. }, {
  34. 'url': 'https://www.redbull.com/int-en/tv/video/AP-1UWHCAR9S1W11/rob-meets-sam-gaze?playlist=playlists::3f81040a-2f31-4832-8e2e-545b1d39d173',
  35. 'only_matching': True,
  36. }, {
  37. 'url': 'https://www.redbull.com/us-en/videos/AP-1YM9QCYE52111',
  38. 'only_matching': True,
  39. }, {
  40. 'url': 'https://www.redbull.com/us-en/events/AP-1XV2K61Q51W11/live/AP-1XUJ86FDH1W11',
  41. 'only_matching': True,
  42. }, {
  43. 'url': 'https://www.redbull.com/int-en/films/AP-1ZSMAW8FH2111',
  44. 'only_matching': True,
  45. }, {
  46. 'url': 'https://www.redbull.com/int-en/episodes/AP-1TQWK7XE11W11',
  47. 'only_matching': True,
  48. }]
  49. def extract_info(self, video_id):
  50. session = self._download_json(
  51. 'https://api.redbull.tv/v3/session', video_id,
  52. note='Downloading access token', query={
  53. 'category': 'personal_computer',
  54. 'os_family': 'http',
  55. })
  56. if session.get('code') == 'error':
  57. raise ExtractorError('{} said: {}'.format(
  58. self.IE_NAME, session['message']))
  59. token = session['token']
  60. try:
  61. video = self._download_json(
  62. 'https://api.redbull.tv/v3/products/' + video_id,
  63. video_id, note='Downloading video information',
  64. headers={'Authorization': token},
  65. )
  66. except ExtractorError as e:
  67. if isinstance(e.cause, HTTPError) and e.cause.status == 404:
  68. error_message = self._parse_json(
  69. e.cause.response.read().decode(), video_id)['error']
  70. raise ExtractorError(f'{self.IE_NAME} said: {error_message}', expected=True)
  71. raise
  72. title = video['title'].strip()
  73. formats, subtitles = self._extract_m3u8_formats_and_subtitles(
  74. f'https://dms.redbull.tv/v3/{video_id}/{token}/playlist.m3u8',
  75. video_id, 'mp4', entry_protocol='m3u8_native', m3u8_id='hls')
  76. for resource in video.get('resources', []):
  77. if resource.startswith('closed_caption_'):
  78. splitted_resource = resource.split('_')
  79. if splitted_resource[2]:
  80. subtitles.setdefault('en', []).append({
  81. 'url': f'https://resources.redbull.tv/{video_id}/{resource}',
  82. 'ext': splitted_resource[2],
  83. })
  84. subheading = video.get('subheading')
  85. if subheading:
  86. title += f' - {subheading}'
  87. return {
  88. 'id': video_id,
  89. 'title': title,
  90. 'description': video.get('long_description') or video.get(
  91. 'short_description'),
  92. 'duration': float_or_none(video.get('duration'), scale=1000),
  93. 'formats': formats,
  94. 'subtitles': subtitles,
  95. }
  96. def _real_extract(self, url):
  97. video_id = self._match_id(url)
  98. return self.extract_info(video_id)
  99. class RedBullEmbedIE(RedBullTVIE): # XXX: Do not subclass from concrete IE
  100. _VALID_URL = r'https?://(?:www\.)?redbull\.com/embed/(?P<id>rrn:content:[^:]+:[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}:[a-z]{2}-[A-Z]{2,3})'
  101. _TESTS = [{
  102. # HLS manifest accessible only using assetId
  103. 'url': 'https://www.redbull.com/embed/rrn:content:episode-videos:f3021f4f-3ed4-51ac-915a-11987126e405:en-INT',
  104. 'only_matching': True,
  105. }]
  106. _VIDEO_ESSENSE_TMPL = '''... on %s {
  107. videoEssence {
  108. attributes
  109. }
  110. }'''
  111. def _real_extract(self, url):
  112. rrn_id = self._match_id(url)
  113. asset_id = self._download_json(
  114. 'https://edge-graphql.crepo-production.redbullaws.com/v1/graphql',
  115. rrn_id, headers={
  116. 'Accept': 'application/json',
  117. 'API-KEY': 'e90a1ff11335423998b100c929ecc866',
  118. }, query={
  119. 'query': '''{
  120. resource(id: "%s", enforceGeoBlocking: false) {
  121. %s
  122. %s
  123. }
  124. }''' % (rrn_id, self._VIDEO_ESSENSE_TMPL % 'LiveVideo', self._VIDEO_ESSENSE_TMPL % 'VideoResource'), # noqa: UP031
  125. })['data']['resource']['videoEssence']['attributes']['assetId']
  126. return self.extract_info(asset_id)
  127. class RedBullTVRrnContentIE(InfoExtractor):
  128. _VALID_URL = r'https?://(?:www\.)?redbull\.com/(?P<region>[a-z]{2,3})-(?P<lang>[a-z]{2})/tv/(?:video|live|film)/(?P<id>rrn:content:[^:]+:[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12})'
  129. _TESTS = [{
  130. 'url': 'https://www.redbull.com/int-en/tv/video/rrn:content:live-videos:e3e6feb4-e95f-50b7-962a-c70f8fd13c73/mens-dh-finals-fort-william',
  131. 'only_matching': True,
  132. }, {
  133. 'url': 'https://www.redbull.com/int-en/tv/video/rrn:content:videos:a36a0f36-ff1b-5db8-a69d-ee11a14bf48b/tn-ts-style?playlist=rrn:content:event-profiles:83f05926-5de8-5389-b5e4-9bb312d715e8:extras',
  134. 'only_matching': True,
  135. }, {
  136. 'url': 'https://www.redbull.com/int-en/tv/film/rrn:content:films:d1f4d00e-4c04-5d19-b510-a805ffa2ab83/follow-me',
  137. 'only_matching': True,
  138. }]
  139. def _real_extract(self, url):
  140. region, lang, rrn_id = self._match_valid_url(url).groups()
  141. rrn_id += f':{lang}-{region.upper()}'
  142. return self.url_result(
  143. 'https://www.redbull.com/embed/' + rrn_id,
  144. RedBullEmbedIE.ie_key(), rrn_id)
  145. class RedBullIE(InfoExtractor):
  146. _VALID_URL = r'https?://(?:www\.)?redbull\.com/(?P<region>[a-z]{2,3})-(?P<lang>[a-z]{2})/(?P<type>(?:episode|film|(?:(?:recap|trailer)-)?video)s|live)/(?!AP-|rrn:content:)(?P<id>[^/?#&]+)'
  147. _TESTS = [{
  148. 'url': 'https://www.redbull.com/int-en/episodes/grime-hashtags-s02-e04',
  149. 'md5': 'db8271a7200d40053a1809ed0dd574ff',
  150. 'info_dict': {
  151. 'id': 'AA-1MT8DQWA91W14',
  152. 'ext': 'mp4',
  153. 'title': 'Grime - Hashtags S2E4',
  154. 'description': 'md5:5546aa612958c08a98faaad4abce484d',
  155. },
  156. }, {
  157. 'url': 'https://www.redbull.com/int-en/films/kilimanjaro-mountain-of-greatness',
  158. 'only_matching': True,
  159. }, {
  160. 'url': 'https://www.redbull.com/int-en/recap-videos/uci-mountain-bike-world-cup-2017-mens-xco-finals-from-vallnord',
  161. 'only_matching': True,
  162. }, {
  163. 'url': 'https://www.redbull.com/int-en/trailer-videos/kings-of-content',
  164. 'only_matching': True,
  165. }, {
  166. 'url': 'https://www.redbull.com/int-en/videos/tnts-style-red-bull-dance-your-style-s1-e12',
  167. 'only_matching': True,
  168. }, {
  169. 'url': 'https://www.redbull.com/int-en/live/mens-dh-finals-fort-william',
  170. 'only_matching': True,
  171. }, {
  172. # only available on the int-en website so a fallback is need for the API
  173. # https://www.redbull.com/v3/api/graphql/v1/v3/query/en-GB>en-INT?filter[uriSlug]=fia-wrc-saturday-recap-estonia&rb3Schema=v1:hero
  174. 'url': 'https://www.redbull.com/gb-en/live/fia-wrc-saturday-recap-estonia',
  175. 'only_matching': True,
  176. }]
  177. _INT_FALLBACK_LIST = ['de', 'en', 'es', 'fr']
  178. _LAT_FALLBACK_MAP = ['ar', 'bo', 'car', 'cl', 'co', 'mx', 'pe']
  179. def _real_extract(self, url):
  180. region, lang, filter_type, display_id = self._match_valid_url(url).groups()
  181. if filter_type == 'episodes':
  182. filter_type = 'episode-videos'
  183. elif filter_type == 'live':
  184. filter_type = 'live-videos'
  185. regions = [region.upper()]
  186. if region != 'int':
  187. if region in self._LAT_FALLBACK_MAP:
  188. regions.append('LAT')
  189. if lang in self._INT_FALLBACK_LIST:
  190. regions.append('INT')
  191. locale = '>'.join([f'{lang}-{reg}' for reg in regions])
  192. rrn_id = self._download_json(
  193. 'https://www.redbull.com/v3/api/graphql/v1/v3/query/' + locale,
  194. display_id, query={
  195. 'filter[type]': filter_type,
  196. 'filter[uriSlug]': display_id,
  197. 'rb3Schema': 'v1:hero',
  198. })['data']['id']
  199. return self.url_result(
  200. 'https://www.redbull.com/embed/' + rrn_id,
  201. RedBullEmbedIE.ie_key(), rrn_id)