]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/nbc.py
[misc] Add `hatch`, `ruff`, `pre-commit` and improve dev docs (#7409)
[yt-dlp.git] / yt_dlp / extractor / nbc.py
CommitLineData
58284890 1import base64
1139935d
RA
2import json
3import re
d4f14a72 4import xml.etree.ElementTree
0bc56fa6 5
e897bd82 6from .adobepass import AdobePassIE
0bc56fa6 7from .common import InfoExtractor
176a068c 8from .theplatform import ThePlatformIE, default_ns
d0c5fabc 9from ..compat import compat_urllib_parse_unquote
3d2623a8 10from ..networking import HEADRequest
1cc79574 11from ..utils import (
11398b92 12 ExtractorError,
cb73b846 13 RegexNotFoundError,
14 UserNotLive,
15 clean_html,
9be0fe1f 16 determine_ext,
17 float_or_none,
895e5c03 18 int_or_none,
9be0fe1f 19 mimetype2ext,
9160a0c6 20 parse_age_limit,
895e5c03 21 parse_duration,
9be0fe1f 22 remove_end,
b46b65ed 23 smuggle_url,
11398b92 24 traverse_obj,
895e5c03 25 try_get,
cb73b846 26 unescapeHTML,
895e5c03 27 unified_timestamp,
6e416b21 28 update_url_query,
11398b92 29 url_basename,
37e64add 30)
0bc56fa6
JMF
31
32
6368e2e6 33class NBCIE(ThePlatformIE): # XXX: Do not subclass from concrete IE
cb73b846 34 _VALID_URL = r'https?(?P<permalink>://(?:www\.)?nbc\.com/(?:classic-tv/)?[^/]+/video/[^/]+/(?P<id>(?:NBCE|n)?\d+))'
58c1f6f0
S
35
36 _TESTS = [
37 {
fdf9b959 38 'url': 'http://www.nbc.com/the-tonight-show/video/jimmy-fallon-surprises-fans-at-ben-jerrys/2848237',
58c1f6f0 39 'info_dict': {
fdf9b959 40 'id': '2848237',
e881c4bc 41 'ext': 'mp4',
5c8a3f86
JMF
42 'title': 'Jimmy Fallon Surprises Fans at Ben & Jerry\'s',
43 'description': 'Jimmy gives out free scoops of his new "Tonight Dough" ice cream flavor by surprising customers at the Ben & Jerry\'s scoop shop.',
79ba9140 44 'timestamp': 1424246400,
45 'upload_date': '20150218',
46 'uploader': 'NBCU-COM',
cb73b846 47 'episode': 'Jimmy Fallon Surprises Fans at Ben & Jerry\'s',
48 'episode_number': 86,
49 'season': 'Season 2',
50 'season_number': 2,
51 'series': 'Tonight Show: Jimmy Fallon',
52 'duration': 237.0,
53 'chapters': 'count:1',
54 'tags': 'count:4',
55 'thumbnail': r're:https?://.+\.jpg',
7e09c147 56 'categories': ['Series/The Tonight Show Starring Jimmy Fallon'],
57 'media_type': 'Full Episode',
58c1f6f0 58 },
e881c4bc 59 'params': {
cb73b846 60 'skip_download': 'm3u8',
e881c4bc 61 },
020cf5eb 62 },
b9b3ab45
YCH
63 {
64 'url': 'http://www.nbc.com/saturday-night-live/video/star-wars-teaser/2832821',
65 'info_dict': {
e881c4bc
YCH
66 'id': '2832821',
67 'ext': 'mp4',
b9b3ab45
YCH
68 'title': 'Star Wars Teaser',
69 'description': 'md5:0b40f9cbde5b671a7ff62fceccc4f442',
79ba9140 70 'timestamp': 1417852800,
71 'upload_date': '20141206',
72 'uploader': 'NBCU-COM',
b9b3ab45 73 },
cb73b846 74 'skip': 'page not found',
0fe2ff78 75 },
e6e90515
YCH
76 {
77 # HLS streams requires the 'hdnea3' cookie
78 'url': 'http://www.nbc.com/Kings/video/goliath/n1806',
79 'info_dict': {
fdf9b959 80 'id': '101528f5a9e8127b107e98c5e6ce4638',
e6e90515
YCH
81 'ext': 'mp4',
82 'title': 'Goliath',
83 'description': 'When an unknown soldier saves the life of the King\'s son in battle, he\'s thrust into the limelight and politics of the kingdom.',
84 'timestamp': 1237100400,
85 'upload_date': '20090315',
86 'uploader': 'NBCU-COM',
87 },
cb73b846 88 'skip': 'page not found',
89 },
90 {
91 # manifest url does not have extension
92 'url': 'https://www.nbc.com/the-golden-globe-awards/video/oprah-winfrey-receives-cecil-b-de-mille-award-at-the-2018-golden-globes/3646439',
93 'info_dict': {
94 'id': '3646439',
95 'ext': 'mp4',
96 'title': 'Oprah Winfrey Receives Cecil B. de Mille Award at the 2018 Golden Globes',
97 'episode': 'Oprah Winfrey Receives Cecil B. de Mille Award at the 2018 Golden Globes',
98 'episode_number': 1,
99 'season': 'Season 75',
100 'season_number': 75,
101 'series': 'The Golden Globe Awards',
102 'description': 'Oprah Winfrey receives the Cecil B. de Mille Award at the 75th Annual Golden Globe Awards.',
103 'uploader': 'NBCU-COM',
104 'upload_date': '20180107',
105 'timestamp': 1515312000,
106 'duration': 570.0,
107 'tags': 'count:8',
108 'thumbnail': r're:https?://.+\.jpg',
109 'chapters': 'count:1',
110 },
111 'params': {
112 'skip_download': 'm3u8',
113 },
114 },
115 {
116 # new video_id format
117 'url': 'https://www.nbc.com/quantum-leap/video/bens-first-leap-nbcs-quantum-leap/NBCE125189978',
118 'info_dict': {
119 'id': 'NBCE125189978',
120 'ext': 'mp4',
121 'title': 'Ben\'s First Leap | NBC\'s Quantum Leap',
122 'description': 'md5:a82762449b7ec4bb83291a7b355ebf8e',
123 'uploader': 'NBCU-COM',
124 'series': 'Quantum Leap',
125 'season': 'Season 1',
126 'season_number': 1,
127 'episode': 'Ben\'s First Leap | NBC\'s Quantum Leap',
128 'episode_number': 1,
129 'duration': 170.171,
130 'chapters': [],
131 'timestamp': 1663956155,
132 'upload_date': '20220923',
133 'tags': 'count:10',
134 'age_limit': 0,
135 'thumbnail': r're:https?://.+\.jpg',
7e09c147 136 'categories': ['Series/Quantum Leap 2022'],
137 'media_type': 'Highlight',
cb73b846 138 },
e6e90515 139 'params': {
cb73b846 140 'skip_download': 'm3u8',
e6e90515 141 },
d673ab65
L
142 },
143 {
144 'url': 'https://www.nbc.com/classic-tv/charles-in-charge/video/charles-in-charge-pilot/n3310',
145 'only_matching': True,
146 },
d0c5fabc
T
147 {
148 # Percent escaped url
149 'url': 'https://www.nbc.com/up-all-night/video/day-after-valentine%27s-day/n2189',
150 'only_matching': True,
151 }
58c1f6f0 152 ]
020cf5eb
JMF
153
154 def _real_extract(self, url):
5ad28e7f 155 permalink, video_id = self._match_valid_url(url).groups()
d0c5fabc 156 permalink = 'http' + compat_urllib_parse_unquote(permalink)
48ff5590 157 video_data = self._download_json(
311ee457 158 'https://friendship.nbc.co/v2/graphql', video_id, query={
48ff5590
RA
159 'query': '''query bonanzaPage(
160 $app: NBCUBrands! = nbc
161 $name: String!
162 $oneApp: Boolean
163 $platform: SupportedPlatforms! = web
164 $type: EntityPageType! = VIDEO
165 $userId: String!
166) {
167 bonanzaPage(
168 app: $app
169 name: $name
170 oneApp: $oneApp
171 platform: $platform
172 type: $type
173 userId: $userId
174 ) {
175 metadata {
311ee457
RA
176 ... on VideoPageData {
177 description
178 episodeNumber
179 keywords
180 locked
181 mpxAccountId
182 mpxGuid
183 rating
48ff5590 184 resourceId
311ee457
RA
185 seasonNumber
186 secondaryTitle
187 seriesShortTitle
188 }
189 }
190 }
48ff5590
RA
191}''',
192 'variables': json.dumps({
193 'name': permalink,
194 'oneApp': True,
195 'userId': '0',
196 }),
197 })['data']['bonanzaPage']['metadata']
2eeb588e
RA
198 query = {
199 'mbr': 'true',
200 'manifest': 'm3u',
c3366fdf 201 'switch': 'HLSServiceSecure',
2eeb588e 202 }
311ee457 203 video_id = video_data['mpxGuid']
9160a0c6
TSJ
204 tp_path = 'NnzsPC/media/guid/%s/%s' % (video_data.get('mpxAccountId') or '2410887629', video_id)
205 tpm = self._download_theplatform_metadata(tp_path, video_id)
206 title = tpm.get('title') or video_data.get('secondaryTitle')
311ee457 207 if video_data.get('locked'):
2eeb588e 208 resource = self._get_mvpd_resource(
48ff5590
RA
209 video_data.get('resourceId') or 'nbcentertainment',
210 title, video_id, video_data.get('rating'))
2eeb588e
RA
211 query['auth'] = self._extract_mvpd_auth(
212 url, video_id, 'nbcentertainment', resource)
213 theplatform_url = smuggle_url(update_url_query(
311ee457 214 'http://link.theplatform.com/s/NnzsPC/media/guid/%s/%s' % (video_data.get('mpxAccountId') or '2410887629', video_id),
2eeb588e 215 query), {'force_smil_url': True})
9160a0c6
TSJ
216
217 # Empty string or 0 can be valid values for these. So the check must be `is None`
218 description = video_data.get('description')
219 if description is None:
220 description = tpm.get('description')
221 episode_number = int_or_none(video_data.get('episodeNumber'))
222 if episode_number is None:
223 episode_number = int_or_none(tpm.get('nbcu$airOrder'))
224 rating = video_data.get('rating')
225 if rating is None:
226 try_get(tpm, lambda x: x['ratings'][0]['rating'])
227 season_number = int_or_none(video_data.get('seasonNumber'))
228 if season_number is None:
229 season_number = int_or_none(tpm.get('nbcu$seasonNumber'))
230 series = video_data.get('seriesShortTitle')
231 if series is None:
232 series = tpm.get('nbcu$seriesShortTitle')
233 tags = video_data.get('keywords')
234 if tags is None or len(tags) == 0:
235 tags = tpm.get('keywords')
236
2eeb588e 237 return {
e881c4bc 238 '_type': 'url_transparent',
9160a0c6
TSJ
239 'age_limit': parse_age_limit(rating),
240 'description': description,
241 'episode': title,
242 'episode_number': episode_number,
e881c4bc 243 'id': video_id,
9160a0c6
TSJ
244 'ie_key': 'ThePlatform',
245 'season_number': season_number,
246 'series': series,
247 'tags': tags,
2eeb588e
RA
248 'title': title,
249 'url': theplatform_url,
e881c4bc 250 }
020cf5eb
JMF
251
252
a2a4d5fa 253class NBCSportsVPlayerIE(InfoExtractor):
29f7c58a 254 _VALID_URL_BASE = r'https?://(?:vplayer\.nbcsports\.com|(?:www\.)?nbcsports\.com/vplayer)/'
255 _VALID_URL = _VALID_URL_BASE + r'(?:[^/]+/)+(?P<id>[0-9a-zA-Z_]+)'
bfd973ec 256 _EMBED_REGEX = [r'(?:iframe[^>]+|var video|div[^>]+data-(?:mpx-)?)[sS]rc\s?=\s?"(?P<url>%s[^\"]+)' % _VALID_URL_BASE]
a28ccbab 257
5cbb2699 258 _TESTS = [{
12ea5c79 259 'url': 'https://vplayer.nbcsports.com/p/BxmELC/nbcsports_embed/select/9CsDKds0kvHI',
a28ccbab
YCH
260 'info_dict': {
261 'id': '9CsDKds0kvHI',
12ea5c79 262 'ext': 'mp4',
a28ccbab
YCH
263 'description': 'md5:df390f70a9ba7c95ff1daace988f0d8d',
264 'title': 'Tyler Kalinoski hits buzzer-beater to lift Davidson',
79ba9140 265 'timestamp': 1426270238,
266 'upload_date': '20150313',
267 'uploader': 'NBCU-SPORTS',
bfa0e270 268 'duration': 72.818,
269 'chapters': [],
270 'thumbnail': r're:^https?://.*\.jpg$'
a28ccbab 271 }
5cbb2699 272 }, {
bfa0e270 273 'url': 'https://vplayer.nbcsports.com/p/BxmELC/nbcsports_embed/select/media/PEgOtlNcC_y2',
5cbb2699 274 'only_matching': True,
29f7c58a 275 }, {
276 'url': 'https://www.nbcsports.com/vplayer/p/BxmELC/nbcsports/select/PHJSaFWbrTY9?form=html&autoPlay=true',
277 'only_matching': True,
5cbb2699 278 }]
a28ccbab
YCH
279
280 def _real_extract(self, url):
281 video_id = self._match_id(url)
282 webpage = self._download_webpage(url, video_id)
bfa0e270 283 theplatform_url = self._html_search_regex(r'tp:releaseUrl="(.+?)"', webpage, 'url')
a28ccbab
YCH
284 return self.url_result(theplatform_url, 'ThePlatform')
285
286
a2a4d5fa 287class NBCSportsIE(InfoExtractor):
29f7c58a 288 _VALID_URL = r'https?://(?:www\.)?nbcsports\.com//?(?!vplayer/)(?:[^/]+/)+(?P<id>[0-9a-z-]+)'
a2a4d5fa 289
29f7c58a 290 _TESTS = [{
291 # iframe src
19c90e40 292 'url': 'https://www.nbcsports.com/watch/nfl/profootballtalk/pft-pm/unpacking-addisons-reckless-driving-citation',
a2a4d5fa
YCH
293 'info_dict': {
294 'id': 'PHJSaFWbrTY9',
29f7c58a 295 'ext': 'mp4',
a2a4d5fa
YCH
296 'title': 'Tom Izzo, Michigan St. has \'so much respect\' for Duke',
297 'description': 'md5:ecb459c9d59e0766ac9c7d5d0eda8113',
0738187f
YCH
298 'uploader': 'NBCU-SPORTS',
299 'upload_date': '20150330',
300 'timestamp': 1427726529,
bfa0e270 301 'chapters': [],
302 'thumbnail': 'https://hdliveextra-a.akamaihd.net/HD/image_sports/NBCU_Sports_Group_-_nbcsports/253/303/izzodps.jpg',
303 'duration': 528.395,
a2a4d5fa 304 }
29f7c58a 305 }, {
306 # data-mpx-src
307 'url': 'https://www.nbcsports.com/philadelphia/philadelphia-phillies/bruce-bochy-hector-neris-hes-idiot',
308 'only_matching': True,
309 }, {
310 # data-src
311 'url': 'https://www.nbcsports.com/boston/video/report-card-pats-secondary-no-match-josh-allen',
312 'only_matching': True,
313 }]
a2a4d5fa
YCH
314
315 def _real_extract(self, url):
316 video_id = self._match_id(url)
317 webpage = self._download_webpage(url, video_id)
318 return self.url_result(
319 NBCSportsVPlayerIE._extract_url(webpage), 'NBCSportsVPlayer')
320
321
1139935d
RA
322class NBCSportsStreamIE(AdobePassIE):
323 _VALID_URL = r'https?://stream\.nbcsports\.com/.+?\bpid=(?P<id>\d+)'
324 _TEST = {
325 'url': 'http://stream.nbcsports.com/nbcsn/generic?pid=206559',
326 'info_dict': {
327 'id': '206559',
328 'ext': 'mp4',
329 'title': 'Amgen Tour of California Women\'s Recap',
330 'description': 'md5:66520066b3b5281ada7698d0ea2aa894',
331 },
332 'params': {
333 # m3u8 download
334 'skip_download': True,
335 },
336 'skip': 'Requires Adobe Pass Authentication',
337 }
338
339 def _real_extract(self, url):
340 video_id = self._match_id(url)
341 live_source = self._download_json(
342 'http://stream.nbcsports.com/data/live_sources_%s.json' % video_id,
343 video_id)
344 video_source = live_source['videoSources'][0]
345 title = video_source['title']
346 source_url = None
347 for k in ('source', 'msl4source', 'iossource', 'hlsv4'):
348 sk = k + 'Url'
349 source_url = video_source.get(sk) or video_source.get(sk + 'Alt')
350 if source_url:
351 break
352 else:
353 source_url = video_source['ottStreamUrl']
354 is_live = video_source.get('type') == 'live' or video_source.get('status') == 'Live'
355 resource = self._get_mvpd_resource('nbcsports', title, video_id, '')
356 token = self._extract_mvpd_auth(url, video_id, 'nbcsports', resource)
357 tokenized_url = self._download_json(
358 'https://token.playmakerservices.com/cdn',
359 video_id, data=json.dumps({
360 'requestorId': 'nbcsports',
361 'pid': video_id,
362 'application': 'NBCSports',
363 'version': 'v1',
364 'platform': 'desktop',
365 'cdn': 'akamai',
366 'url': video_source['sourceUrl'],
367 'token': base64.b64encode(token.encode()).decode(),
368 'resourceId': base64.b64encode(resource.encode()).decode(),
369 }).encode())['tokenizedUrl']
370 formats = self._extract_m3u8_formats(tokenized_url, video_id, 'mp4')
1139935d
RA
371 return {
372 'id': video_id,
39ca3b5c 373 'title': title,
1139935d
RA
374 'description': live_source.get('description'),
375 'formats': formats,
376 'is_live': is_live,
377 }
378
379
6368e2e6 380class NBCNewsIE(ThePlatformIE): # XXX: Do not subclass from concrete IE
a843464a 381 _VALID_URL = r'(?x)https?://(?:www\.)?(?:nbcnews|today|msnbc)\.com/([^/]+/)*(?:.*-)?(?P<id>[^/?]+)'
bfd973ec 382 _EMBED_REGEX = [r'<iframe[^>]+src=(["\'])(?P<url>(?:https?:)?//www\.nbcnews\.com/widget/video-embed/[^"\']+)\1']
0bc56fa6 383
87fe568c 384 _TESTS = [
87fe568c 385 {
574b2a73 386 'url': 'http://www.nbcnews.com/watch/nbcnews-com/how-twitter-reacted-to-the-snowden-interview-269389891880',
19c90e40 387 'md5': 'fb3dcd2d7b1dd9804305fa2fc95ab610', # md5 tends to fluctuate
87fe568c 388 'info_dict': {
a843464a 389 'id': '269389891880',
10e3d734 390 'ext': 'mp4',
87fe568c
JMF
391 'title': 'How Twitter Reacted To The Snowden Interview',
392 'description': 'md5:65a0bd5d76fe114f3c2727aa3a81fe64',
0437307a
RA
393 'timestamp': 1401363060,
394 'upload_date': '20140529',
19c90e40 395 'duration': 46.0,
396 'thumbnail': 'https://media-cldnry.s-nbcnews.com/image/upload/MSNBC/Components/Video/140529/p_tweet_snow_140529.jpg',
87fe568c 397 },
87fe568c 398 },
2df54b4b
S
399 {
400 'url': 'http://www.nbcnews.com/feature/dateline-full-episodes/full-episode-family-business-n285156',
401 'md5': 'fdbf39ab73a72df5896b6234ff98518a',
402 'info_dict': {
0437307a 403 'id': '529953347624',
2df54b4b
S
404 'ext': 'mp4',
405 'title': 'FULL EPISODE: Family Business',
406 'description': 'md5:757988edbaae9d7be1d585eb5d55cc04',
407 },
574b2a73 408 'skip': 'This page is unavailable.',
2df54b4b 409 },
d9aa2b78
RS
410 {
411 'url': 'http://www.nbcnews.com/nightly-news/video/nightly-news-with-brian-williams-full-broadcast-february-4-394064451844',
19c90e40 412 'md5': '40d0e48c68896359c80372306ece0fc3',
d9aa2b78 413 'info_dict': {
a843464a 414 'id': '394064451844',
d9aa2b78
RS
415 'ext': 'mp4',
416 'title': 'Nightly News with Brian Williams Full Broadcast (February 4)',
417 'description': 'md5:1c10c1eccbe84a26e5debb4381e2d3c5',
0437307a 418 'timestamp': 1423104900,
0437307a 419 'upload_date': '20150205',
19c90e40 420 'duration': 1236.0,
421 'thumbnail': 'https://media-cldnry.s-nbcnews.com/image/upload/MSNBC/Components/Video/__NEW/nn_netcast_150204.jpg',
d9aa2b78
RS
422 },
423 },
574b2a73 424 {
425 'url': 'http://www.nbcnews.com/business/autos/volkswagen-11-million-vehicles-could-have-suspect-software-emissions-scandal-n431456',
19c90e40 426 'md5': 'ffb59bcf0733dc3c7f0ace907f5e3939',
574b2a73 427 'info_dict': {
895e5c03 428 'id': 'n431456',
574b2a73 429 'ext': 'mp4',
895e5c03
RA
430 'title': "Volkswagen U.S. Chief: We 'Totally Screwed Up'",
431 'description': 'md5:d22d1281a24f22ea0880741bb4dd6301',
0437307a
RA
432 'upload_date': '20150922',
433 'timestamp': 1442917800,
19c90e40 434 'duration': 37.0,
435 'thumbnail': 'https://media-cldnry.s-nbcnews.com/image/upload/MSNBC/Components/Video/__NEW/x_lon_vwhorn_150922.jpg',
574b2a73 436 },
574b2a73 437 },
cb7d4d0e 438 {
439 'url': 'http://www.today.com/video/see-the-aurora-borealis-from-space-in-stunning-new-nasa-video-669831235788',
19c90e40 440 'md5': '693d1fa21d23afcc9b04c66b227ed9ff',
cb7d4d0e 441 'info_dict': {
a843464a 442 'id': '669831235788',
cb7d4d0e 443 'ext': 'mp4',
444 'title': 'See the aurora borealis from space in stunning new NASA video',
445 'description': 'md5:74752b7358afb99939c5f8bb2d1d04b1',
446 'upload_date': '20160420',
447 'timestamp': 1461152093,
19c90e40 448 'duration': 69.0,
449 'thumbnail': 'https://media-cldnry.s-nbcnews.com/image/upload/MSNBC/Components/Video/201604/2016-04-20T11-35-09-133Z--1280x720.jpg',
0437307a
RA
450 },
451 },
452 {
453 'url': 'http://www.msnbc.com/all-in-with-chris-hayes/watch/the-chaotic-gop-immigration-vote-314487875924',
454 'md5': '6d236bf4f3dddc226633ce6e2c3f814d',
455 'info_dict': {
a843464a 456 'id': '314487875924',
0437307a
RA
457 'ext': 'mp4',
458 'title': 'The chaotic GOP immigration vote',
459 'description': 'The Republican House votes on a border bill that has no chance of getting through the Senate or signed by the President and is drawing criticism from all sides.',
ec85ded8 460 'thumbnail': r're:^https?://.*\.jpg$',
0437307a
RA
461 'timestamp': 1406937606,
462 'upload_date': '20140802',
19c90e40 463 'duration': 940.0,
cb7d4d0e 464 },
465 },
3f125c8c
S
466 {
467 'url': 'http://www.nbcnews.com/watch/dateline/full-episode--deadly-betrayal-386250819952',
468 'only_matching': True,
469 },
5de008e8
YCH
470 {
471 # From http://www.vulture.com/2016/06/letterman-couldnt-care-less-about-late-night.html
472 'url': 'http://www.nbcnews.com/widget/video-embed/701714499682',
473 'only_matching': True,
474 },
87fe568c 475 ]
0bc56fa6
JMF
476
477 def _real_extract(self, url):
a843464a 478 video_id = self._match_id(url)
895e5c03
RA
479 webpage = self._download_webpage(url, video_id)
480
135dfa2c 481 data = self._search_nextjs_data(webpage, video_id)['props']['initialState']
895e5c03
RA
482 video_data = try_get(data, lambda x: x['video']['current'], dict)
483 if not video_data:
484 video_data = data['article']['content'][0]['primaryMedia']['video']
485 title = video_data['headline']['primary']
486
487 formats = []
488 for va in video_data.get('videoAssets', []):
489 public_url = va.get('publicUrl')
490 if not public_url:
491 continue
492 if '://link.theplatform.com/' in public_url:
493 public_url = update_url_query(public_url, {'format': 'redirect'})
494 format_id = va.get('format')
495 if format_id == 'M3U':
496 formats.extend(self._extract_m3u8_formats(
497 public_url, video_id, 'mp4', 'm3u8_native',
498 m3u8_id=format_id, fatal=False))
499 continue
500 tbr = int_or_none(va.get('bitrate'), 1000)
501 if tbr:
502 format_id += '-%d' % tbr
503 formats.append({
504 'format_id': format_id,
505 'url': public_url,
506 'width': int_or_none(va.get('width')),
507 'height': int_or_none(va.get('height')),
508 'tbr': tbr,
509 'ext': 'mp4',
510 })
6e416b21 511
895e5c03
RA
512 subtitles = {}
513 closed_captioning = video_data.get('closedCaptioning')
514 if closed_captioning:
515 for cc_url in closed_captioning.values():
516 if not cc_url:
517 continue
518 subtitles.setdefault('en', []).append({
519 'url': cc_url,
520 })
6e416b21 521
a843464a 522 return {
a843464a 523 'id': video_id,
895e5c03
RA
524 'title': title,
525 'description': try_get(video_data, lambda x: x['description']['primary']),
526 'thumbnail': try_get(video_data, lambda x: x['primaryImage']['url']['primary']),
527 'duration': parse_duration(video_data.get('duration')),
528 'timestamp': unified_timestamp(video_data.get('datePublished')),
529 'formats': formats,
530 'subtitles': subtitles,
a843464a 531 }
be457302
YCH
532
533
534class NBCOlympicsIE(InfoExtractor):
58284890 535 IE_NAME = 'nbcolympics'
3e376d18 536 _VALID_URL = r'https?://www\.nbcolympics\.com/videos?/(?P<id>[0-9a-z-]+)'
be457302
YCH
537
538 _TEST = {
539 # Geo-restricted to US
540 'url': 'http://www.nbcolympics.com/video/justin-roses-son-leo-was-tears-after-his-dad-won-gold',
541 'md5': '54fecf846d05429fbaa18af557ee523a',
542 'info_dict': {
543 'id': 'WjTBzDXx5AUq',
544 'display_id': 'justin-roses-son-leo-was-tears-after-his-dad-won-gold',
545 'ext': 'mp4',
546 'title': 'Rose\'s son Leo was in tears after his dad won gold',
547 'description': 'Olympic gold medalist Justin Rose gets emotional talking to the impact his win in men\'s golf has already had on his children.',
548 'timestamp': 1471274964,
549 'upload_date': '20160815',
550 'uploader': 'NBCU-SPORTS',
551 },
19c90e40 552 'skip': '404 Not Found',
be457302
YCH
553 }
554
555 def _real_extract(self, url):
556 display_id = self._match_id(url)
557
558 webpage = self._download_webpage(url, display_id)
559
3e376d18
W
560 try:
561 drupal_settings = self._parse_json(self._search_regex(
562 r'jQuery\.extend\(Drupal\.settings\s*,\s*({.+?})\);',
563 webpage, 'drupal settings'), display_id)
564
565 iframe_url = drupal_settings['vod']['iframe_url']
566 theplatform_url = iframe_url.replace(
567 'vplayer.nbcolympics.com', 'player.theplatform.com')
568 except RegexNotFoundError:
569 theplatform_url = self._search_regex(
570 r"([\"'])embedUrl\1: *([\"'])(?P<embedUrl>.+)\2",
571 webpage, 'embedding URL', group="embedUrl")
be457302
YCH
572
573 return {
574 '_type': 'url_transparent',
575 'url': theplatform_url,
576 'ie_key': ThePlatformIE.ie_key(),
577 'display_id': display_id,
578 }
58284890
RA
579
580
581class NBCOlympicsStreamIE(AdobePassIE):
582 IE_NAME = 'nbcolympics:stream'
583 _VALID_URL = r'https?://stream\.nbcolympics\.com/(?P<id>[0-9a-z-]+)'
bb36a55c 584 _TESTS = [
585 {
586 'note': 'Tokenized m3u8 source URL',
587 'url': 'https://stream.nbcolympics.com/womens-soccer-group-round-11',
588 'info_dict': {
589 'id': '2019740',
590 'ext': 'mp4',
591 'title': r"re:Women's Group Stage - Netherlands vs\. Brazil [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$",
592 },
593 'params': {
594 'skip_download': 'm3u8',
595 },
19c90e40 596 'skip': 'Livestream',
bb36a55c 597 }, {
598 'note': 'Plain m3u8 source URL',
599 'url': 'https://stream.nbcolympics.com/gymnastics-event-finals-mens-floor-pommel-horse-womens-vault-bars',
600 'info_dict': {
601 'id': '2021729',
602 'ext': 'mp4',
603 'title': r're:Event Finals: M Floor, W Vault, M Pommel, W Uneven Bars [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
604 },
605 'params': {
606 'skip_download': 'm3u8',
607 },
19c90e40 608 'skip': 'Livestream',
58284890 609 },
bb36a55c 610 ]
58284890
RA
611
612 def _real_extract(self, url):
613 display_id = self._match_id(url)
614 webpage = self._download_webpage(url, display_id)
615 pid = self._search_regex(r'pid\s*=\s*(\d+);', webpage, 'pid')
bb36a55c 616
58284890 617 event_config = self._download_json(
bb36a55c 618 f'http://stream.nbcolympics.com/data/event_config_{pid}.json',
619 pid, 'Downloading event config')['eventConfig']
620
621 title = event_config['eventTitle']
622 is_live = {'live': True, 'replay': False}.get(event_config.get('eventStatus'))
bb36a55c 623
58284890 624 source_url = self._download_json(
bb36a55c 625 f'https://api-leap.nbcsports.com/feeds/assets/{pid}?application=NBCOlympics&platform=desktop&format=nbc-player&env=staging',
626 pid, 'Downloading leap config'
627 )['videoSources'][0]['cdnSources']['primary'][0]['sourceUrl']
628
629 if event_config.get('cdnToken'):
630 ap_resource = self._get_mvpd_resource(
631 event_config.get('resourceId', 'NBCOlympics'),
632 re.sub(r'[^\w\d ]+', '', event_config['eventTitle']), pid,
633 event_config.get('ratingId', 'NO VALUE'))
634 media_token = self._extract_mvpd_auth(url, pid, event_config.get('requestorId', 'NBCOlympics'), ap_resource)
635
636 source_url = self._download_json(
637 'https://tokens.playmakerservices.com/', pid, 'Retrieving tokenized URL',
638 data=json.dumps({
639 'application': 'NBCSports',
640 'authentication-type': 'adobe-pass',
641 'cdn': 'akamai',
642 'pid': pid,
643 'platform': 'desktop',
644 'requestorId': 'NBCOlympics',
645 'resourceId': base64.b64encode(ap_resource.encode()).decode(),
646 'token': base64.b64encode(media_token.encode()).decode(),
647 'url': source_url,
648 'version': 'v1',
649 }).encode(),
650 )['akamai'][0]['tokenizedUrl']
651
652 formats = self._extract_m3u8_formats(source_url, pid, 'mp4', live=is_live)
653 for f in formats:
654 # -http_seekable requires ffmpeg 4.3+ but it doesnt seem possible to
655 # download with ffmpeg without this option
0a5a191a 656 f['downloader_options'] = {'ffmpeg_args': ['-seekable', '0', '-http_seekable', '0', '-icy', '0']}
58284890
RA
657
658 return {
659 'id': pid,
660 'display_id': display_id,
661 'title': title,
662 'formats': formats,
bb36a55c 663 'is_live': is_live,
58284890 664 }
11398b92 665
666
667class NBCStationsIE(InfoExtractor):
668 _DOMAIN_RE = '|'.join(map(re.escape, (
669 'nbcbayarea', 'nbcboston', 'nbcchicago', 'nbcconnecticut', 'nbcdfw', 'nbclosangeles',
670 'nbcmiami', 'nbcnewyork', 'nbcphiladelphia', 'nbcsandiego', 'nbcwashington',
671 'necn', 'telemundo52', 'telemundoarizona', 'telemundochicago', 'telemundonuevainglaterra',
672 )))
673 _VALID_URL = rf'https?://(?:www\.)?(?P<site>{_DOMAIN_RE})\.com/(?:[^/?#]+/)*(?P<id>[^/?#]+)/?(?:$|[#?])'
674
675 _TESTS = [{
676 'url': 'https://www.nbclosangeles.com/news/local/large-structure-fire-in-downtown-la-prompts-smoke-odor-advisory/2968618/',
11398b92 677 'info_dict': {
678 'id': '2968618',
679 'ext': 'mp4',
680 'title': 'Large Structure Fire in Downtown LA Prompts Smoke Odor Advisory',
cb73b846 681 'description': 'md5:417ed3c2d91fe9d301e6db7b0942f182',
9be0fe1f 682 'duration': 112.513,
11398b92 683 'timestamp': 1661135892,
cb73b846 684 'upload_date': '20220822',
11398b92 685 'uploader': 'NBC 4',
cb73b846 686 'channel_id': 'KNBC',
11398b92 687 'channel': 'nbclosangeles',
688 },
cb73b846 689 'params': {
690 'skip_download': 'm3u8',
691 },
11398b92 692 }, {
693 'url': 'https://www.telemundoarizona.com/responde/huracan-complica-reembolso-para-televidente-de-tucson/2247002/',
11398b92 694 'info_dict': {
695 'id': '2247002',
696 'ext': 'mp4',
cb73b846 697 'title': 'Huracán complica que televidente de Tucson reciba reembolso',
11398b92 698 'description': 'md5:af298dc73aab74d4fca6abfb12acb6cf',
9be0fe1f 699 'duration': 172.406,
11398b92 700 'timestamp': 1660886507,
701 'upload_date': '20220819',
702 'uploader': 'Telemundo Arizona',
cb73b846 703 'channel_id': 'KTAZ',
11398b92 704 'channel': 'telemundoarizona',
705 },
cb73b846 706 'params': {
707 'skip_download': 'm3u8',
708 },
9be0fe1f 709 }, {
710 # direct mp4 link
711 'url': 'https://www.nbcboston.com/weather/video-weather/highs-near-freezing-in-boston-on-wednesday/2961135/',
712 'md5': '9bf8c41dc7abbb75b1a44f1491a4cc85',
713 'info_dict': {
714 'id': '2961135',
715 'ext': 'mp4',
716 'title': 'Highs Near Freezing in Boston on Wednesday',
717 'description': 'md5:3ec486609a926c99f00a3512e6c0e85b',
718 'duration': 235.669,
719 'timestamp': 1675268656,
720 'upload_date': '20230201',
721 'uploader': '',
722 'channel_id': 'WBTS',
723 'channel': 'nbcboston',
724 },
11398b92 725 }]
726
727 _RESOLUTIONS = {
728 '1080': '1920',
729 '720': '1280',
730 '540': '960',
731 '360': '640',
732 '234': '416',
733 }
734
735 def _real_extract(self, url):
736 channel, video_id = self._match_valid_url(url).group('site', 'id')
737 webpage = self._download_webpage(url, video_id)
738
739 nbc_data = self._search_json(
176a068c 740 r'<script>\s*var\s+nbc\s*=', webpage, 'NBC JSON data', video_id)
11398b92 741 pdk_acct = nbc_data.get('pdkAcct') or 'Yh1nAC'
742 fw_ssid = traverse_obj(nbc_data, ('video', 'fwSSID'))
11398b92 743
cb73b846 744 video_data = self._search_json(
745 r'data-videos="\[', webpage, 'video data', video_id, default={}, transform_source=unescapeHTML)
746 video_data.update(self._search_json(
747 r'data-meta="', webpage, 'metadata', video_id, default={}, transform_source=unescapeHTML))
748 if not video_data:
749 raise ExtractorError('No video metadata found in webpage', expected=True)
11398b92 750
9be0fe1f 751 info, formats = {}, []
cb73b846 752 is_live = int_or_none(video_data.get('mpx_is_livestream')) == 1
753 query = {
754 'formats': 'MPEG-DASH none,M3U none,MPEG-DASH none,MPEG4,MP3',
755 'format': 'SMIL',
756 'fwsitesection': fw_ssid,
757 'fwNetworkID': traverse_obj(nbc_data, ('video', 'fwNetworkID'), default='382114'),
758 'pprofile': 'ots_desktop_html',
759 'sensitive': 'false',
760 'w': '1920',
761 'h': '1080',
762 'mode': 'LIVE' if is_live else 'on-demand',
763 'vpaid': 'script',
764 'schema': '2.0',
765 'sdk': 'PDK 6.1.3',
766 }
11398b92 767
cb73b846 768 if is_live:
769 player_id = traverse_obj(video_data, ((None, ('video', 'meta')), (
770 'mpx_m3upid', 'mpx_pid', 'pid_streaming_web_medium')), get_all=False)
771 info['title'] = f'{channel} livestream'
11398b92 772
773 else:
cb73b846 774 player_id = traverse_obj(video_data, (
775 (None, ('video', 'meta')), ('pid_streaming_web_high', 'mpx_pid')), get_all=False)
11398b92 776
777 date_string = traverse_obj(video_data, 'date_string', 'date_gmt')
778 if date_string:
779 date_string = self._search_regex(
780 r'datetime="([^"]+)"', date_string, 'date string', fatal=False)
781 else:
782 date_string = traverse_obj(
cb73b846 783 nbc_data, ('dataLayer', 'adobe', ('prop70', 'eVar70', 'eVar59')), get_all=False)
11398b92 784
cb73b846 785 video_url = traverse_obj(video_data, ((None, ('video', 'meta')), 'mp4_url'), get_all=False)
11398b92 786 if video_url:
9be0fe1f 787 ext = determine_ext(video_url)
cb73b846 788 height = self._search_regex(r'\d+-(\d+)p', url_basename(video_url), 'height', default=None)
11398b92 789 formats.append({
790 'url': video_url,
9be0fe1f 791 'ext': ext,
11398b92 792 'width': int_or_none(self._RESOLUTIONS.get(height)),
793 'height': int_or_none(height),
9be0fe1f 794 'format_id': f'http-{ext}',
11398b92 795 })
796
cb73b846 797 info.update({
798 'title': video_data.get('title') or traverse_obj(nbc_data, (
799 'dataLayer', (None, 'adobe'), ('contenttitle', 'title', 'prop22')), get_all=False),
800 'description':
801 traverse_obj(video_data, 'summary', 'excerpt', 'video_hero_text')
802 or clean_html(traverse_obj(nbc_data, ('dataLayer', 'summary'))),
803 'timestamp': unified_timestamp(date_string),
804 })
11398b92 805
cb73b846 806 smil = None
807 if player_id and fw_ssid:
808 smil = self._download_xml(
809 f'https://link.theplatform.com/s/{pdk_acct}/{player_id}', video_id,
810 note='Downloading SMIL data', query=query, fatal=is_live)
d4f14a72 811 if not isinstance(smil, xml.etree.ElementTree.Element):
812 smil = None
813 subtitles = self._parse_smil_subtitles(smil, default_ns) if smil is not None else {}
814 for video in smil.findall(self._xpath_ns('.//video', default_ns)) if smil is not None else []:
9be0fe1f 815 info['duration'] = float_or_none(remove_end(video.get('dur'), 'ms'), 1000)
816 video_src_url = video.get('src')
817 ext = mimetype2ext(video.get('type'), default=determine_ext(video_src_url))
818 if ext == 'm3u8':
819 fmts, subs = self._extract_m3u8_formats_and_subtitles(
820 video_src_url, video_id, 'mp4', m3u8_id='hls', fatal=is_live,
821 live=is_live, errnote='No HLS formats found')
822 formats.extend(fmts)
823 self._merge_subtitles(subs, target=subtitles)
824 elif video_src_url:
825 formats.append({
826 'url': video_src_url,
827 'format_id': f'https-{ext}',
828 'ext': ext,
829 'width': int_or_none(video.get('width')),
830 'height': int_or_none(video.get('height')),
831 })
cb73b846 832
833 if not formats:
834 self.raise_no_formats('No video content found in webpage', expected=True)
835 elif is_live:
836 try:
837 self._request_webpage(
838 HEADRequest(formats[0]['url']), video_id, note='Checking live status')
839 except ExtractorError:
840 raise UserNotLive(video_id=channel)
11398b92 841
842 return {
cb73b846 843 'id': video_id,
11398b92 844 'channel': channel,
cb73b846 845 'channel_id': nbc_data.get('callLetters'),
846 'uploader': nbc_data.get('on_air_name'),
11398b92 847 'formats': formats,
cb73b846 848 'subtitles': subtitles,
849 'is_live': is_live,
11398b92 850 **info,
851 }