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