[ie/dplay] Fix extractors (#10471)
[yt-dlp3.git] / yt_dlp / extractor / redbulltv.py
blobceeef5204560cc017e04c1715564f2bbd5153275
1 from .common import InfoExtractor
2 from ..networking.exceptions import HTTPError
3 from ..utils import (
4 ExtractorError,
5 float_or_none,
9 class RedBullTVIE(InfoExtractor):
10 _VALID_URL = r'https?://(?:www\.)?redbull(?:\.tv|\.com(?:/[^/]+)?(?:/tv)?)(?:/events/[^/]+)?/(?:videos?|live|(?:film|episode)s)/(?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,
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:5546aa612958c08a98faaad4abce484d',
30 'duration': 904,
32 'params': {
33 'skip_download': True,
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 'url': 'https://www.redbull.com/us-en/videos/AP-1YM9QCYE52111',
40 'only_matching': True,
41 }, {
42 'url': 'https://www.redbull.com/us-en/events/AP-1XV2K61Q51W11/live/AP-1XUJ86FDH1W11',
43 'only_matching': True,
44 }, {
45 'url': 'https://www.redbull.com/int-en/films/AP-1ZSMAW8FH2111',
46 'only_matching': True,
47 }, {
48 'url': 'https://www.redbull.com/int-en/episodes/AP-1TQWK7XE11W11',
49 'only_matching': True,
52 def extract_info(self, video_id):
53 session = self._download_json(
54 'https://api.redbull.tv/v3/session', video_id,
55 note='Downloading access token', query={
56 'category': 'personal_computer',
57 'os_family': 'http',
59 if session.get('code') == 'error':
60 raise ExtractorError('{} said: {}'.format(
61 self.IE_NAME, session['message']))
62 token = session['token']
64 try:
65 video = self._download_json(
66 'https://api.redbull.tv/v3/products/' + video_id,
67 video_id, note='Downloading video information',
68 headers={'Authorization': token},
70 except ExtractorError as e:
71 if isinstance(e.cause, HTTPError) and e.cause.status == 404:
72 error_message = self._parse_json(
73 e.cause.response.read().decode(), video_id)['error']
74 raise ExtractorError(f'{self.IE_NAME} said: {error_message}', expected=True)
75 raise
77 title = video['title'].strip()
79 formats, subtitles = self._extract_m3u8_formats_and_subtitles(
80 f'https://dms.redbull.tv/v3/{video_id}/{token}/playlist.m3u8',
81 video_id, 'mp4', entry_protocol='m3u8_native', m3u8_id='hls')
83 for resource in video.get('resources', []):
84 if resource.startswith('closed_caption_'):
85 splitted_resource = resource.split('_')
86 if splitted_resource[2]:
87 subtitles.setdefault('en', []).append({
88 'url': f'https://resources.redbull.tv/{video_id}/{resource}',
89 'ext': splitted_resource[2],
92 subheading = video.get('subheading')
93 if subheading:
94 title += f' - {subheading}'
96 return {
97 'id': video_id,
98 'title': title,
99 'description': video.get('long_description') or video.get(
100 'short_description'),
101 'duration': float_or_none(video.get('duration'), scale=1000),
102 'formats': formats,
103 'subtitles': subtitles,
106 def _real_extract(self, url):
107 video_id = self._match_id(url)
108 return self.extract_info(video_id)
111 class RedBullEmbedIE(RedBullTVIE): # XXX: Do not subclass from concrete IE
112 _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})'
113 _TESTS = [{
114 # HLS manifest accessible only using assetId
115 'url': 'https://www.redbull.com/embed/rrn:content:episode-videos:f3021f4f-3ed4-51ac-915a-11987126e405:en-INT',
116 'only_matching': True,
118 _VIDEO_ESSENSE_TMPL = '''... on %s {
119 videoEssence {
120 attributes
122 }'''
124 def _real_extract(self, url):
125 rrn_id = self._match_id(url)
126 asset_id = self._download_json(
127 'https://edge-graphql.crepo-production.redbullaws.com/v1/graphql',
128 rrn_id, headers={
129 'Accept': 'application/json',
130 'API-KEY': 'e90a1ff11335423998b100c929ecc866',
131 }, query={
132 'query': '''{
133 resource(id: "%s", enforceGeoBlocking: false) {
137 }''' % (rrn_id, self._VIDEO_ESSENSE_TMPL % 'LiveVideo', self._VIDEO_ESSENSE_TMPL % 'VideoResource'), # noqa: UP031
138 })['data']['resource']['videoEssence']['attributes']['assetId']
139 return self.extract_info(asset_id)
142 class RedBullTVRrnContentIE(InfoExtractor):
143 _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})'
144 _TESTS = [{
145 'url': 'https://www.redbull.com/int-en/tv/video/rrn:content:live-videos:e3e6feb4-e95f-50b7-962a-c70f8fd13c73/mens-dh-finals-fort-william',
146 'only_matching': True,
147 }, {
148 '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',
149 'only_matching': True,
150 }, {
151 'url': 'https://www.redbull.com/int-en/tv/film/rrn:content:films:d1f4d00e-4c04-5d19-b510-a805ffa2ab83/follow-me',
152 'only_matching': True,
155 def _real_extract(self, url):
156 region, lang, rrn_id = self._match_valid_url(url).groups()
157 rrn_id += f':{lang}-{region.upper()}'
158 return self.url_result(
159 'https://www.redbull.com/embed/' + rrn_id,
160 RedBullEmbedIE.ie_key(), rrn_id)
163 class RedBullIE(InfoExtractor):
164 _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>[^/?#&]+)'
165 _TESTS = [{
166 'url': 'https://www.redbull.com/int-en/episodes/grime-hashtags-s02-e04',
167 'md5': 'db8271a7200d40053a1809ed0dd574ff',
168 'info_dict': {
169 'id': 'AA-1MT8DQWA91W14',
170 'ext': 'mp4',
171 'title': 'Grime - Hashtags S2E4',
172 'description': 'md5:5546aa612958c08a98faaad4abce484d',
174 }, {
175 'url': 'https://www.redbull.com/int-en/films/kilimanjaro-mountain-of-greatness',
176 'only_matching': True,
177 }, {
178 'url': 'https://www.redbull.com/int-en/recap-videos/uci-mountain-bike-world-cup-2017-mens-xco-finals-from-vallnord',
179 'only_matching': True,
180 }, {
181 'url': 'https://www.redbull.com/int-en/trailer-videos/kings-of-content',
182 'only_matching': True,
183 }, {
184 'url': 'https://www.redbull.com/int-en/videos/tnts-style-red-bull-dance-your-style-s1-e12',
185 'only_matching': True,
186 }, {
187 'url': 'https://www.redbull.com/int-en/live/mens-dh-finals-fort-william',
188 'only_matching': True,
189 }, {
190 # only available on the int-en website so a fallback is need for the API
191 # https://www.redbull.com/v3/api/graphql/v1/v3/query/en-GB>en-INT?filter[uriSlug]=fia-wrc-saturday-recap-estonia&rb3Schema=v1:hero
192 'url': 'https://www.redbull.com/gb-en/live/fia-wrc-saturday-recap-estonia',
193 'only_matching': True,
195 _INT_FALLBACK_LIST = ['de', 'en', 'es', 'fr']
196 _LAT_FALLBACK_MAP = ['ar', 'bo', 'car', 'cl', 'co', 'mx', 'pe']
198 def _real_extract(self, url):
199 region, lang, filter_type, display_id = self._match_valid_url(url).groups()
200 if filter_type == 'episodes':
201 filter_type = 'episode-videos'
202 elif filter_type == 'live':
203 filter_type = 'live-videos'
205 regions = [region.upper()]
206 if region != 'int':
207 if region in self._LAT_FALLBACK_MAP:
208 regions.append('LAT')
209 if lang in self._INT_FALLBACK_LIST:
210 regions.append('INT')
211 locale = '>'.join([f'{lang}-{reg}' for reg in regions])
213 rrn_id = self._download_json(
214 'https://www.redbull.com/v3/api/graphql/v1/v3/query/' + locale,
215 display_id, query={
216 'filter[type]': filter_type,
217 'filter[uriSlug]': display_id,
218 'rb3Schema': 'v1:hero',
219 })['data']['id']
221 return self.url_result(
222 'https://www.redbull.com/embed/' + rrn_id,
223 RedBullEmbedIE.ie_key(), rrn_id)