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