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