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