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