]> jfr.im git - yt-dlp.git/blob - youtube_dlc/extractor/nbc.py
Update to ytdl-2021.01.03
[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_BASE = r'https?://(?:vplayer\.nbcsports\.com|(?:www\.)?nbcsports\.com/vplayer)/'
162 _VALID_URL = _VALID_URL_BASE + r'(?:[^/]+/)+(?P<id>[0-9a-zA-Z_]+)'
163
164 _TESTS = [{
165 'url': 'https://vplayer.nbcsports.com/p/BxmELC/nbcsports_embed/select/9CsDKds0kvHI',
166 'info_dict': {
167 'id': '9CsDKds0kvHI',
168 'ext': 'mp4',
169 'description': 'md5:df390f70a9ba7c95ff1daace988f0d8d',
170 'title': 'Tyler Kalinoski hits buzzer-beater to lift Davidson',
171 'timestamp': 1426270238,
172 'upload_date': '20150313',
173 'uploader': 'NBCU-SPORTS',
174 }
175 }, {
176 'url': 'https://vplayer.nbcsports.com/p/BxmELC/nbcsports_embed/select/media/_hqLjQ95yx8Z',
177 'only_matching': True,
178 }, {
179 'url': 'https://www.nbcsports.com/vplayer/p/BxmELC/nbcsports/select/PHJSaFWbrTY9?form=html&autoPlay=true',
180 'only_matching': True,
181 }]
182
183 @staticmethod
184 def _extract_url(webpage):
185 iframe_m = re.search(
186 r'<(?:iframe[^>]+|div[^>]+data-(?:mpx-)?)src="(?P<url>%s[^"]+)"' % NBCSportsVPlayerIE._VALID_URL_BASE, webpage)
187 if iframe_m:
188 return iframe_m.group('url')
189
190 def _real_extract(self, url):
191 video_id = self._match_id(url)
192 webpage = self._download_webpage(url, video_id)
193 theplatform_url = self._og_search_video_url(webpage).replace(
194 'vplayer.nbcsports.com', 'player.theplatform.com')
195 return self.url_result(theplatform_url, 'ThePlatform')
196
197
198 class NBCSportsIE(InfoExtractor):
199 _VALID_URL = r'https?://(?:www\.)?nbcsports\.com//?(?!vplayer/)(?:[^/]+/)+(?P<id>[0-9a-z-]+)'
200
201 _TESTS = [{
202 # iframe src
203 'url': 'http://www.nbcsports.com//college-basketball/ncaab/tom-izzo-michigan-st-has-so-much-respect-duke',
204 'info_dict': {
205 'id': 'PHJSaFWbrTY9',
206 'ext': 'mp4',
207 'title': 'Tom Izzo, Michigan St. has \'so much respect\' for Duke',
208 'description': 'md5:ecb459c9d59e0766ac9c7d5d0eda8113',
209 'uploader': 'NBCU-SPORTS',
210 'upload_date': '20150330',
211 'timestamp': 1427726529,
212 }
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 }]
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
230 class 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
289 class NBCNewsIE(ThePlatformIE):
290 _VALID_URL = r'(?x)https?://(?:www\.)?(?:nbcnews|today|msnbc)\.com/([^/]+/)*(?:.*-)?(?P<id>[^/?]+)'
291
292 _TESTS = [
293 {
294 'url': 'http://www.nbcnews.com/watch/nbcnews-com/how-twitter-reacted-to-the-snowden-interview-269389891880',
295 'md5': 'cf4bc9e6ce0130f00f545d80ecedd4bf',
296 'info_dict': {
297 'id': '269389891880',
298 'ext': 'mp4',
299 'title': 'How Twitter Reacted To The Snowden Interview',
300 'description': 'md5:65a0bd5d76fe114f3c2727aa3a81fe64',
301 'timestamp': 1401363060,
302 'upload_date': '20140529',
303 },
304 },
305 {
306 'url': 'http://www.nbcnews.com/feature/dateline-full-episodes/full-episode-family-business-n285156',
307 'md5': 'fdbf39ab73a72df5896b6234ff98518a',
308 'info_dict': {
309 'id': '529953347624',
310 'ext': 'mp4',
311 'title': 'FULL EPISODE: Family Business',
312 'description': 'md5:757988edbaae9d7be1d585eb5d55cc04',
313 },
314 'skip': 'This page is unavailable.',
315 },
316 {
317 'url': 'http://www.nbcnews.com/nightly-news/video/nightly-news-with-brian-williams-full-broadcast-february-4-394064451844',
318 'md5': '8eb831eca25bfa7d25ddd83e85946548',
319 'info_dict': {
320 'id': '394064451844',
321 'ext': 'mp4',
322 'title': 'Nightly News with Brian Williams Full Broadcast (February 4)',
323 'description': 'md5:1c10c1eccbe84a26e5debb4381e2d3c5',
324 'timestamp': 1423104900,
325 'upload_date': '20150205',
326 },
327 },
328 {
329 'url': 'http://www.nbcnews.com/business/autos/volkswagen-11-million-vehicles-could-have-suspect-software-emissions-scandal-n431456',
330 'md5': '4a8c4cec9e1ded51060bdda36ff0a5c0',
331 'info_dict': {
332 'id': 'n431456',
333 'ext': 'mp4',
334 'title': "Volkswagen U.S. Chief: We 'Totally Screwed Up'",
335 'description': 'md5:d22d1281a24f22ea0880741bb4dd6301',
336 'upload_date': '20150922',
337 'timestamp': 1442917800,
338 },
339 },
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': {
344 'id': '669831235788',
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,
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': {
356 'id': '314487875924',
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.',
360 'thumbnail': r're:^https?://.*\.jpg$',
361 'timestamp': 1406937606,
362 'upload_date': '20140802',
363 },
364 },
365 {
366 'url': 'http://www.nbcnews.com/watch/dateline/full-episode--deadly-betrayal-386250819952',
367 'only_matching': True,
368 },
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 },
374 ]
375
376 def _real_extract(self, url):
377 video_id = self._match_id(url)
378 webpage = self._download_webpage(url, video_id)
379
380 data = self._parse_json(self._search_regex(
381 r'<script[^>]+id="__NEXT_DATA__"[^>]*>({.+?})</script>',
382 webpage, 'bootstrap json'), video_id)['props']['initialState']
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)
413
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 })
423
424 return {
425 'id': video_id,
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,
433 }
434
435
436 class NBCOlympicsIE(InfoExtractor):
437 IE_NAME = 'nbcolympics'
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 }
475
476
477 class 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 }