]> jfr.im git - yt-dlp.git/blame - youtube_dlc/extractor/nbc.py
Update to ytdl-2021.01.03
[yt-dlp.git] / youtube_dlc / extractor / nbc.py
CommitLineData
cd7ee7aa
JMF
1from __future__ import unicode_literals
2
58284890 3import base64
1139935d
RA
4import json
5import re
0bc56fa6
JMF
6
7from .common import InfoExtractor
574b2a73 8from .theplatform import ThePlatformIE
fdf9b959 9from .adobepass import AdobePassIE
d0c5fabc 10from ..compat import compat_urllib_parse_unquote
1cc79574 11from ..utils import (
895e5c03 12 int_or_none,
895e5c03 13 parse_duration,
b46b65ed 14 smuggle_url,
895e5c03
RA
15 try_get,
16 unified_timestamp,
6e416b21 17 update_url_query,
37e64add 18)
0bc56fa6
JMF
19
20
fdf9b959 21class NBCIE(AdobePassIE):
d673ab65 22 _VALID_URL = r'https?(?P<permalink>://(?:www\.)?nbc\.com/(?:classic-tv/)?[^/]+/video/[^/]+/(?P<id>n?\d+))'
58c1f6f0
S
23
24 _TESTS = [
25 {
fdf9b959 26 'url': 'http://www.nbc.com/the-tonight-show/video/jimmy-fallon-surprises-fans-at-ben-jerrys/2848237',
58c1f6f0 27 'info_dict': {
fdf9b959 28 'id': '2848237',
e881c4bc 29 'ext': 'mp4',
5c8a3f86
JMF
30 'title': 'Jimmy Fallon Surprises Fans at Ben & Jerry\'s',
31 '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 32 'timestamp': 1424246400,
33 'upload_date': '20150218',
34 'uploader': 'NBCU-COM',
58c1f6f0 35 },
e881c4bc
YCH
36 'params': {
37 # m3u8 download
38 'skip_download': True,
39 },
020cf5eb 40 },
b9b3ab45
YCH
41 {
42 'url': 'http://www.nbc.com/saturday-night-live/video/star-wars-teaser/2832821',
43 'info_dict': {
e881c4bc
YCH
44 'id': '2832821',
45 'ext': 'mp4',
b9b3ab45
YCH
46 'title': 'Star Wars Teaser',
47 'description': 'md5:0b40f9cbde5b671a7ff62fceccc4f442',
79ba9140 48 'timestamp': 1417852800,
49 'upload_date': '20141206',
50 'uploader': 'NBCU-COM',
b9b3ab45 51 },
e881c4bc
YCH
52 'params': {
53 # m3u8 download
54 'skip_download': True,
55 },
b9b3ab45 56 'skip': 'Only works from US',
0fe2ff78 57 },
e6e90515
YCH
58 {
59 # HLS streams requires the 'hdnea3' cookie
60 'url': 'http://www.nbc.com/Kings/video/goliath/n1806',
61 'info_dict': {
fdf9b959 62 'id': '101528f5a9e8127b107e98c5e6ce4638',
e6e90515
YCH
63 'ext': 'mp4',
64 'title': 'Goliath',
65 '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.',
66 'timestamp': 1237100400,
67 'upload_date': '20090315',
68 'uploader': 'NBCU-COM',
69 },
70 'params': {
71 'skip_download': True,
72 },
73 'skip': 'Only works from US',
d673ab65
L
74 },
75 {
76 'url': 'https://www.nbc.com/classic-tv/charles-in-charge/video/charles-in-charge-pilot/n3310',
77 'only_matching': True,
78 },
d0c5fabc
T
79 {
80 # Percent escaped url
81 'url': 'https://www.nbc.com/up-all-night/video/day-after-valentine%27s-day/n2189',
82 'only_matching': True,
83 }
58c1f6f0 84 ]
020cf5eb
JMF
85
86 def _real_extract(self, url):
52294cdd 87 permalink, video_id = re.match(self._VALID_URL, url).groups()
d0c5fabc 88 permalink = 'http' + compat_urllib_parse_unquote(permalink)
48ff5590 89 video_data = self._download_json(
311ee457 90 'https://friendship.nbc.co/v2/graphql', video_id, query={
48ff5590
RA
91 'query': '''query bonanzaPage(
92 $app: NBCUBrands! = nbc
93 $name: String!
94 $oneApp: Boolean
95 $platform: SupportedPlatforms! = web
96 $type: EntityPageType! = VIDEO
97 $userId: String!
98) {
99 bonanzaPage(
100 app: $app
101 name: $name
102 oneApp: $oneApp
103 platform: $platform
104 type: $type
105 userId: $userId
106 ) {
107 metadata {
311ee457
RA
108 ... on VideoPageData {
109 description
110 episodeNumber
111 keywords
112 locked
113 mpxAccountId
114 mpxGuid
115 rating
48ff5590 116 resourceId
311ee457
RA
117 seasonNumber
118 secondaryTitle
119 seriesShortTitle
120 }
121 }
122 }
48ff5590
RA
123}''',
124 'variables': json.dumps({
125 'name': permalink,
126 'oneApp': True,
127 'userId': '0',
128 }),
129 })['data']['bonanzaPage']['metadata']
2eeb588e
RA
130 query = {
131 'mbr': 'true',
132 'manifest': 'm3u',
133 }
311ee457
RA
134 video_id = video_data['mpxGuid']
135 title = video_data['secondaryTitle']
136 if video_data.get('locked'):
2eeb588e 137 resource = self._get_mvpd_resource(
48ff5590
RA
138 video_data.get('resourceId') or 'nbcentertainment',
139 title, video_id, video_data.get('rating'))
2eeb588e
RA
140 query['auth'] = self._extract_mvpd_auth(
141 url, video_id, 'nbcentertainment', resource)
142 theplatform_url = smuggle_url(update_url_query(
311ee457 143 'http://link.theplatform.com/s/NnzsPC/media/guid/%s/%s' % (video_data.get('mpxAccountId') or '2410887629', video_id),
2eeb588e
RA
144 query), {'force_smil_url': True})
145 return {
e881c4bc 146 '_type': 'url_transparent',
e881c4bc 147 'id': video_id,
2eeb588e
RA
148 'title': title,
149 'url': theplatform_url,
150 'description': video_data.get('description'),
90b633f8 151 'tags': video_data.get('keywords'),
2eeb588e
RA
152 'season_number': int_or_none(video_data.get('seasonNumber')),
153 'episode_number': int_or_none(video_data.get('episodeNumber')),
90b633f8 154 'episode': title,
311ee457 155 'series': video_data.get('seriesShortTitle'),
2eeb588e 156 'ie_key': 'ThePlatform',
e881c4bc 157 }
020cf5eb
JMF
158
159
a2a4d5fa 160class NBCSportsVPlayerIE(InfoExtractor):
29f7c58a 161 _VALID_URL_BASE = r'https?://(?:vplayer\.nbcsports\.com|(?:www\.)?nbcsports\.com/vplayer)/'
162 _VALID_URL = _VALID_URL_BASE + r'(?:[^/]+/)+(?P<id>[0-9a-zA-Z_]+)'
a28ccbab 163
5cbb2699 164 _TESTS = [{
12ea5c79 165 'url': 'https://vplayer.nbcsports.com/p/BxmELC/nbcsports_embed/select/9CsDKds0kvHI',
a28ccbab
YCH
166 'info_dict': {
167 'id': '9CsDKds0kvHI',
12ea5c79 168 'ext': 'mp4',
a28ccbab
YCH
169 'description': 'md5:df390f70a9ba7c95ff1daace988f0d8d',
170 'title': 'Tyler Kalinoski hits buzzer-beater to lift Davidson',
79ba9140 171 'timestamp': 1426270238,
172 'upload_date': '20150313',
173 'uploader': 'NBCU-SPORTS',
a28ccbab 174 }
5cbb2699 175 }, {
12ea5c79 176 'url': 'https://vplayer.nbcsports.com/p/BxmELC/nbcsports_embed/select/media/_hqLjQ95yx8Z',
5cbb2699 177 'only_matching': True,
29f7c58a 178 }, {
179 'url': 'https://www.nbcsports.com/vplayer/p/BxmELC/nbcsports/select/PHJSaFWbrTY9?form=html&autoPlay=true',
180 'only_matching': True,
5cbb2699 181 }]
a28ccbab 182
a2a4d5fa
YCH
183 @staticmethod
184 def _extract_url(webpage):
185 iframe_m = re.search(
29f7c58a 186 r'<(?:iframe[^>]+|div[^>]+data-(?:mpx-)?)src="(?P<url>%s[^"]+)"' % NBCSportsVPlayerIE._VALID_URL_BASE, webpage)
a2a4d5fa
YCH
187 if iframe_m:
188 return iframe_m.group('url')
189
a28ccbab
YCH
190 def _real_extract(self, url):
191 video_id = self._match_id(url)
192 webpage = self._download_webpage(url, video_id)
12ea5c79
PV
193 theplatform_url = self._og_search_video_url(webpage).replace(
194 'vplayer.nbcsports.com', 'player.theplatform.com')
a28ccbab
YCH
195 return self.url_result(theplatform_url, 'ThePlatform')
196
197
a2a4d5fa 198class NBCSportsIE(InfoExtractor):
29f7c58a 199 _VALID_URL = r'https?://(?:www\.)?nbcsports\.com//?(?!vplayer/)(?:[^/]+/)+(?P<id>[0-9a-z-]+)'
a2a4d5fa 200
29f7c58a 201 _TESTS = [{
202 # iframe src
a2a4d5fa 203 'url': 'http://www.nbcsports.com//college-basketball/ncaab/tom-izzo-michigan-st-has-so-much-respect-duke',
a2a4d5fa
YCH
204 'info_dict': {
205 'id': 'PHJSaFWbrTY9',
29f7c58a 206 'ext': 'mp4',
a2a4d5fa
YCH
207 'title': 'Tom Izzo, Michigan St. has \'so much respect\' for Duke',
208 'description': 'md5:ecb459c9d59e0766ac9c7d5d0eda8113',
0738187f
YCH
209 'uploader': 'NBCU-SPORTS',
210 'upload_date': '20150330',
211 'timestamp': 1427726529,
a2a4d5fa 212 }
29f7c58a 213 }, {
214 # data-mpx-src
215 'url': 'https://www.nbcsports.com/philadelphia/philadelphia-phillies/bruce-bochy-hector-neris-hes-idiot',
216 'only_matching': True,
217 }, {
218 # data-src
219 'url': 'https://www.nbcsports.com/boston/video/report-card-pats-secondary-no-match-josh-allen',
220 'only_matching': True,
221 }]
a2a4d5fa
YCH
222
223 def _real_extract(self, url):
224 video_id = self._match_id(url)
225 webpage = self._download_webpage(url, video_id)
226 return self.url_result(
227 NBCSportsVPlayerIE._extract_url(webpage), 'NBCSportsVPlayer')
228
229
1139935d
RA
230class NBCSportsStreamIE(AdobePassIE):
231 _VALID_URL = r'https?://stream\.nbcsports\.com/.+?\bpid=(?P<id>\d+)'
232 _TEST = {
233 'url': 'http://stream.nbcsports.com/nbcsn/generic?pid=206559',
234 'info_dict': {
235 'id': '206559',
236 'ext': 'mp4',
237 'title': 'Amgen Tour of California Women\'s Recap',
238 'description': 'md5:66520066b3b5281ada7698d0ea2aa894',
239 },
240 'params': {
241 # m3u8 download
242 'skip_download': True,
243 },
244 'skip': 'Requires Adobe Pass Authentication',
245 }
246
247 def _real_extract(self, url):
248 video_id = self._match_id(url)
249 live_source = self._download_json(
250 'http://stream.nbcsports.com/data/live_sources_%s.json' % video_id,
251 video_id)
252 video_source = live_source['videoSources'][0]
253 title = video_source['title']
254 source_url = None
255 for k in ('source', 'msl4source', 'iossource', 'hlsv4'):
256 sk = k + 'Url'
257 source_url = video_source.get(sk) or video_source.get(sk + 'Alt')
258 if source_url:
259 break
260 else:
261 source_url = video_source['ottStreamUrl']
262 is_live = video_source.get('type') == 'live' or video_source.get('status') == 'Live'
263 resource = self._get_mvpd_resource('nbcsports', title, video_id, '')
264 token = self._extract_mvpd_auth(url, video_id, 'nbcsports', resource)
265 tokenized_url = self._download_json(
266 'https://token.playmakerservices.com/cdn',
267 video_id, data=json.dumps({
268 'requestorId': 'nbcsports',
269 'pid': video_id,
270 'application': 'NBCSports',
271 'version': 'v1',
272 'platform': 'desktop',
273 'cdn': 'akamai',
274 'url': video_source['sourceUrl'],
275 'token': base64.b64encode(token.encode()).decode(),
276 'resourceId': base64.b64encode(resource.encode()).decode(),
277 }).encode())['tokenizedUrl']
278 formats = self._extract_m3u8_formats(tokenized_url, video_id, 'mp4')
279 self._sort_formats(formats)
280 return {
281 'id': video_id,
282 'title': self._live_title(title) if is_live else title,
283 'description': live_source.get('description'),
284 'formats': formats,
285 'is_live': is_live,
286 }
287
288
574b2a73 289class NBCNewsIE(ThePlatformIE):
a843464a 290 _VALID_URL = r'(?x)https?://(?:www\.)?(?:nbcnews|today|msnbc)\.com/([^/]+/)*(?:.*-)?(?P<id>[^/?]+)'
0bc56fa6 291
87fe568c 292 _TESTS = [
87fe568c 293 {
574b2a73 294 'url': 'http://www.nbcnews.com/watch/nbcnews-com/how-twitter-reacted-to-the-snowden-interview-269389891880',
895e5c03 295 'md5': 'cf4bc9e6ce0130f00f545d80ecedd4bf',
87fe568c 296 'info_dict': {
a843464a 297 'id': '269389891880',
10e3d734 298 'ext': 'mp4',
87fe568c
JMF
299 'title': 'How Twitter Reacted To The Snowden Interview',
300 'description': 'md5:65a0bd5d76fe114f3c2727aa3a81fe64',
0437307a
RA
301 'timestamp': 1401363060,
302 'upload_date': '20140529',
87fe568c 303 },
87fe568c 304 },
2df54b4b
S
305 {
306 'url': 'http://www.nbcnews.com/feature/dateline-full-episodes/full-episode-family-business-n285156',
307 'md5': 'fdbf39ab73a72df5896b6234ff98518a',
308 'info_dict': {
0437307a 309 'id': '529953347624',
2df54b4b
S
310 'ext': 'mp4',
311 'title': 'FULL EPISODE: Family Business',
312 'description': 'md5:757988edbaae9d7be1d585eb5d55cc04',
313 },
574b2a73 314 'skip': 'This page is unavailable.',
2df54b4b 315 },
d9aa2b78
RS
316 {
317 'url': 'http://www.nbcnews.com/nightly-news/video/nightly-news-with-brian-williams-full-broadcast-february-4-394064451844',
895e5c03 318 'md5': '8eb831eca25bfa7d25ddd83e85946548',
d9aa2b78 319 'info_dict': {
a843464a 320 'id': '394064451844',
d9aa2b78
RS
321 'ext': 'mp4',
322 'title': 'Nightly News with Brian Williams Full Broadcast (February 4)',
323 'description': 'md5:1c10c1eccbe84a26e5debb4381e2d3c5',
0437307a 324 'timestamp': 1423104900,
0437307a 325 'upload_date': '20150205',
d9aa2b78
RS
326 },
327 },
574b2a73 328 {
329 'url': 'http://www.nbcnews.com/business/autos/volkswagen-11-million-vehicles-could-have-suspect-software-emissions-scandal-n431456',
895e5c03 330 'md5': '4a8c4cec9e1ded51060bdda36ff0a5c0',
574b2a73 331 'info_dict': {
895e5c03 332 'id': 'n431456',
574b2a73 333 'ext': 'mp4',
895e5c03
RA
334 'title': "Volkswagen U.S. Chief: We 'Totally Screwed Up'",
335 'description': 'md5:d22d1281a24f22ea0880741bb4dd6301',
0437307a
RA
336 'upload_date': '20150922',
337 'timestamp': 1442917800,
574b2a73 338 },
574b2a73 339 },
cb7d4d0e 340 {
341 'url': 'http://www.today.com/video/see-the-aurora-borealis-from-space-in-stunning-new-nasa-video-669831235788',
342 'md5': '118d7ca3f0bea6534f119c68ef539f71',
343 'info_dict': {
a843464a 344 'id': '669831235788',
cb7d4d0e 345 'ext': 'mp4',
346 'title': 'See the aurora borealis from space in stunning new NASA video',
347 'description': 'md5:74752b7358afb99939c5f8bb2d1d04b1',
348 'upload_date': '20160420',
349 'timestamp': 1461152093,
0437307a
RA
350 },
351 },
352 {
353 'url': 'http://www.msnbc.com/all-in-with-chris-hayes/watch/the-chaotic-gop-immigration-vote-314487875924',
354 'md5': '6d236bf4f3dddc226633ce6e2c3f814d',
355 'info_dict': {
a843464a 356 'id': '314487875924',
0437307a
RA
357 'ext': 'mp4',
358 'title': 'The chaotic GOP immigration vote',
359 '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 360 'thumbnail': r're:^https?://.*\.jpg$',
0437307a
RA
361 'timestamp': 1406937606,
362 'upload_date': '20140802',
cb7d4d0e 363 },
364 },
3f125c8c
S
365 {
366 'url': 'http://www.nbcnews.com/watch/dateline/full-episode--deadly-betrayal-386250819952',
367 'only_matching': True,
368 },
5de008e8
YCH
369 {
370 # From http://www.vulture.com/2016/06/letterman-couldnt-care-less-about-late-night.html
371 'url': 'http://www.nbcnews.com/widget/video-embed/701714499682',
372 'only_matching': True,
373 },
87fe568c 374 ]
0bc56fa6
JMF
375
376 def _real_extract(self, url):
a843464a 377 video_id = self._match_id(url)
895e5c03
RA
378 webpage = self._download_webpage(url, video_id)
379
380 data = self._parse_json(self._search_regex(
8bdd16b4 381 r'<script[^>]+id="__NEXT_DATA__"[^>]*>({.+?})</script>',
382 webpage, 'bootstrap json'), video_id)['props']['initialState']
895e5c03
RA
383 video_data = try_get(data, lambda x: x['video']['current'], dict)
384 if not video_data:
385 video_data = data['article']['content'][0]['primaryMedia']['video']
386 title = video_data['headline']['primary']
387
388 formats = []
389 for va in video_data.get('videoAssets', []):
390 public_url = va.get('publicUrl')
391 if not public_url:
392 continue
393 if '://link.theplatform.com/' in public_url:
394 public_url = update_url_query(public_url, {'format': 'redirect'})
395 format_id = va.get('format')
396 if format_id == 'M3U':
397 formats.extend(self._extract_m3u8_formats(
398 public_url, video_id, 'mp4', 'm3u8_native',
399 m3u8_id=format_id, fatal=False))
400 continue
401 tbr = int_or_none(va.get('bitrate'), 1000)
402 if tbr:
403 format_id += '-%d' % tbr
404 formats.append({
405 'format_id': format_id,
406 'url': public_url,
407 'width': int_or_none(va.get('width')),
408 'height': int_or_none(va.get('height')),
409 'tbr': tbr,
410 'ext': 'mp4',
411 })
412 self._sort_formats(formats)
6e416b21 413
895e5c03
RA
414 subtitles = {}
415 closed_captioning = video_data.get('closedCaptioning')
416 if closed_captioning:
417 for cc_url in closed_captioning.values():
418 if not cc_url:
419 continue
420 subtitles.setdefault('en', []).append({
421 'url': cc_url,
422 })
6e416b21 423
a843464a 424 return {
a843464a 425 'id': video_id,
895e5c03
RA
426 'title': title,
427 'description': try_get(video_data, lambda x: x['description']['primary']),
428 'thumbnail': try_get(video_data, lambda x: x['primaryImage']['url']['primary']),
429 'duration': parse_duration(video_data.get('duration')),
430 'timestamp': unified_timestamp(video_data.get('datePublished')),
431 'formats': formats,
432 'subtitles': subtitles,
a843464a 433 }
be457302
YCH
434
435
436class NBCOlympicsIE(InfoExtractor):
58284890 437 IE_NAME = 'nbcolympics'
be457302
YCH
438 _VALID_URL = r'https?://www\.nbcolympics\.com/video/(?P<id>[a-z-]+)'
439
440 _TEST = {
441 # Geo-restricted to US
442 'url': 'http://www.nbcolympics.com/video/justin-roses-son-leo-was-tears-after-his-dad-won-gold',
443 'md5': '54fecf846d05429fbaa18af557ee523a',
444 'info_dict': {
445 'id': 'WjTBzDXx5AUq',
446 'display_id': 'justin-roses-son-leo-was-tears-after-his-dad-won-gold',
447 'ext': 'mp4',
448 'title': 'Rose\'s son Leo was in tears after his dad won gold',
449 'description': 'Olympic gold medalist Justin Rose gets emotional talking to the impact his win in men\'s golf has already had on his children.',
450 'timestamp': 1471274964,
451 'upload_date': '20160815',
452 'uploader': 'NBCU-SPORTS',
453 },
454 }
455
456 def _real_extract(self, url):
457 display_id = self._match_id(url)
458
459 webpage = self._download_webpage(url, display_id)
460
461 drupal_settings = self._parse_json(self._search_regex(
462 r'jQuery\.extend\(Drupal\.settings\s*,\s*({.+?})\);',
463 webpage, 'drupal settings'), display_id)
464
465 iframe_url = drupal_settings['vod']['iframe_url']
466 theplatform_url = iframe_url.replace(
467 'vplayer.nbcolympics.com', 'player.theplatform.com')
468
469 return {
470 '_type': 'url_transparent',
471 'url': theplatform_url,
472 'ie_key': ThePlatformIE.ie_key(),
473 'display_id': display_id,
474 }
58284890
RA
475
476
477class NBCOlympicsStreamIE(AdobePassIE):
478 IE_NAME = 'nbcolympics:stream'
479 _VALID_URL = r'https?://stream\.nbcolympics\.com/(?P<id>[0-9a-z-]+)'
480 _TEST = {
481 'url': 'http://stream.nbcolympics.com/2018-winter-olympics-nbcsn-evening-feb-8',
482 'info_dict': {
483 'id': '203493',
484 'ext': 'mp4',
485 'title': 're:Curling, Alpine, Luge [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
486 },
487 'params': {
488 # m3u8 download
489 'skip_download': True,
490 },
491 }
492 _DATA_URL_TEMPLATE = 'http://stream.nbcolympics.com/data/%s_%s.json'
493
494 def _real_extract(self, url):
495 display_id = self._match_id(url)
496 webpage = self._download_webpage(url, display_id)
497 pid = self._search_regex(r'pid\s*=\s*(\d+);', webpage, 'pid')
498 resource = self._search_regex(
499 r"resource\s*=\s*'(.+)';", webpage,
500 'resource').replace("' + pid + '", pid)
501 event_config = self._download_json(
502 self._DATA_URL_TEMPLATE % ('event_config', pid),
503 pid)['eventConfig']
504 title = self._live_title(event_config['eventTitle'])
505 source_url = self._download_json(
506 self._DATA_URL_TEMPLATE % ('live_sources', pid),
507 pid)['videoSources'][0]['sourceUrl']
508 media_token = self._extract_mvpd_auth(
509 url, pid, event_config.get('requestorId', 'NBCOlympics'), resource)
510 formats = self._extract_m3u8_formats(self._download_webpage(
511 'http://sp.auth.adobe.com/tvs/v1/sign', pid, query={
512 'cdn': 'akamai',
513 'mediaToken': base64.b64encode(media_token.encode()),
514 'resource': base64.b64encode(resource.encode()),
515 'url': source_url,
516 }), pid, 'mp4')
517 self._sort_formats(formats)
518
519 return {
520 'id': pid,
521 'display_id': display_id,
522 'title': title,
523 'formats': formats,
524 'is_live': True,
525 }