[nbc] add support adobe pass auth(closes #12006)
[youtube-dl] / youtube_dl / extractor / nbc.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from .theplatform import ThePlatformIE
7 from .adobepass import AdobePassIE
8 from ..compat import compat_urllib_parse_urlparse
9 from ..utils import (
10     find_xpath_attr,
11     lowercase_escape,
12     smuggle_url,
13     unescapeHTML,
14     update_url_query,
15     int_or_none,
16 )
17
18
19 class NBCIE(AdobePassIE):
20     _VALID_URL = r'https?://(?:www\.)?nbc\.com/(?:[^/]+/)+(?P<id>n?\d+)'
21
22     _TESTS = [
23         {
24             'url': 'http://www.nbc.com/the-tonight-show/video/jimmy-fallon-surprises-fans-at-ben-jerrys/2848237',
25             'info_dict': {
26                 'id': '2848237',
27                 'ext': 'mp4',
28                 'title': 'Jimmy Fallon Surprises Fans at Ben & Jerry\'s',
29                 'description': 'Jimmy gives out free scoops of his new "Tonight Dough" ice cream flavor by surprising customers at the Ben & Jerry\'s scoop shop.',
30                 'timestamp': 1424246400,
31                 'upload_date': '20150218',
32                 'uploader': 'NBCU-COM',
33             },
34             'params': {
35                 # m3u8 download
36                 'skip_download': True,
37             },
38         },
39         {
40             'url': 'http://www.nbc.com/the-tonight-show/episodes/176',
41             'info_dict': {
42                 'id': '176',
43                 'ext': 'flv',
44                 'title': 'Ricky Gervais, Steven Van Zandt, ILoveMakonnen',
45                 'description': 'A brand new episode of The Tonight Show welcomes Ricky Gervais, Steven Van Zandt and ILoveMakonnen.',
46             },
47             'skip': '404 Not Found',
48         },
49         {
50             'url': 'http://www.nbc.com/saturday-night-live/video/star-wars-teaser/2832821',
51             'info_dict': {
52                 'id': '2832821',
53                 'ext': 'mp4',
54                 'title': 'Star Wars Teaser',
55                 'description': 'md5:0b40f9cbde5b671a7ff62fceccc4f442',
56                 'timestamp': 1417852800,
57                 'upload_date': '20141206',
58                 'uploader': 'NBCU-COM',
59             },
60             'params': {
61                 # m3u8 download
62                 'skip_download': True,
63             },
64             'skip': 'Only works from US',
65         },
66         {
67             # This video has expired but with an escaped embedURL
68             'url': 'http://www.nbc.com/parenthood/episode-guide/season-5/just-like-at-home/515',
69             'only_matching': True,
70         },
71         {
72             # HLS streams requires the 'hdnea3' cookie
73             'url': 'http://www.nbc.com/Kings/video/goliath/n1806',
74             'info_dict': {
75                 'id': '101528f5a9e8127b107e98c5e6ce4638',
76                 'ext': 'mp4',
77                 'title': 'Goliath',
78                 '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.',
79                 'timestamp': 1237100400,
80                 'upload_date': '20090315',
81                 'uploader': 'NBCU-COM',
82             },
83             'params': {
84                 'skip_download': True,
85             },
86             'skip': 'Only works from US',
87         }
88     ]
89
90     def _real_extract(self, url):
91         video_id = self._match_id(url)
92         webpage = self._download_webpage(url, video_id)
93         info = {
94             '_type': 'url_transparent',
95             'ie_key': 'ThePlatform',
96             'id': video_id,
97         }
98         video_data = None
99         preload = self._search_regex(
100             r'PRELOAD\s*=\s*({.+})', webpage, 'preload data', default=None)
101         if preload:
102             preload_data = self._parse_json(preload, video_id)
103             path = compat_urllib_parse_urlparse(url).path.rstrip('/')
104             entity_id = preload_data.get('xref', {}).get(path)
105             video_data = preload_data.get('entities', {}).get(entity_id)
106         if video_data:
107             query = {
108                 'mbr': 'true',
109                 'manifest': 'm3u',
110             }
111             video_id = video_data['guid']
112             title = video_data['title']
113             if video_data.get('entitlement') == 'auth':
114                 resource = self._get_mvpd_resource(
115                     'nbcentertainment', title, video_id,
116                     video_data.get('vChipRating'))
117                 query['auth'] = self._extract_mvpd_auth(
118                     url, video_id, 'nbcentertainment', resource)
119             theplatform_url = smuggle_url(update_url_query(
120                 'http://link.theplatform.com/s/NnzsPC/media/guid/2410887629/' + video_id,
121                 query), {'force_smil_url': True})
122             info.update({
123                 'id': video_id,
124                 'title': title,
125                 'url': theplatform_url,
126                 'description': video_data.get('description'),
127                 'keywords': video_data.get('keywords'),
128                 'season_number': int_or_none(video_data.get('seasonNumber')),
129                 'episode_number': int_or_none(video_data.get('episodeNumber')),
130                 'series': video_data.get('showName'),
131             })
132         else:
133             theplatform_url = unescapeHTML(lowercase_escape(self._html_search_regex(
134                 [
135                     r'(?:class="video-player video-player-full" data-mpx-url|class="player" src)="(.*?)"',
136                     r'<iframe[^>]+src="((?:https?:)?//player\.theplatform\.com/[^"]+)"',
137                     r'"embedURL"\s*:\s*"([^"]+)"'
138                 ],
139                 webpage, 'theplatform url').replace('_no_endcard', '').replace('\\/', '/')))
140             if theplatform_url.startswith('//'):
141                 theplatform_url = 'http:' + theplatform_url
142             info['url'] = smuggle_url(theplatform_url, {'source_url': url})
143         return info
144
145
146 class NBCSportsVPlayerIE(InfoExtractor):
147     _VALID_URL = r'https?://vplayer\.nbcsports\.com/(?:[^/]+/)+(?P<id>[0-9a-zA-Z_]+)'
148
149     _TESTS = [{
150         'url': 'https://vplayer.nbcsports.com/p/BxmELC/nbcsports_share/select/9CsDKds0kvHI',
151         'info_dict': {
152             'id': '9CsDKds0kvHI',
153             'ext': 'flv',
154             'description': 'md5:df390f70a9ba7c95ff1daace988f0d8d',
155             'title': 'Tyler Kalinoski hits buzzer-beater to lift Davidson',
156             'timestamp': 1426270238,
157             'upload_date': '20150313',
158             'uploader': 'NBCU-SPORTS',
159         }
160     }, {
161         'url': 'http://vplayer.nbcsports.com/p/BxmELC/nbc_embedshare/select/_hqLjQ95yx8Z',
162         'only_matching': True,
163     }]
164
165     @staticmethod
166     def _extract_url(webpage):
167         iframe_m = re.search(
168             r'<iframe[^>]+src="(?P<url>https?://vplayer\.nbcsports\.com/[^"]+)"', webpage)
169         if iframe_m:
170             return iframe_m.group('url')
171
172     def _real_extract(self, url):
173         video_id = self._match_id(url)
174         webpage = self._download_webpage(url, video_id)
175         theplatform_url = self._og_search_video_url(webpage)
176         return self.url_result(theplatform_url, 'ThePlatform')
177
178
179 class NBCSportsIE(InfoExtractor):
180     # Does not include https because its certificate is invalid
181     _VALID_URL = r'https?://(?:www\.)?nbcsports\.com//?(?:[^/]+/)+(?P<id>[0-9a-z-]+)'
182
183     _TEST = {
184         'url': 'http://www.nbcsports.com//college-basketball/ncaab/tom-izzo-michigan-st-has-so-much-respect-duke',
185         'info_dict': {
186             'id': 'PHJSaFWbrTY9',
187             'ext': 'flv',
188             'title': 'Tom Izzo, Michigan St. has \'so much respect\' for Duke',
189             'description': 'md5:ecb459c9d59e0766ac9c7d5d0eda8113',
190             'uploader': 'NBCU-SPORTS',
191             'upload_date': '20150330',
192             'timestamp': 1427726529,
193         }
194     }
195
196     def _real_extract(self, url):
197         video_id = self._match_id(url)
198         webpage = self._download_webpage(url, video_id)
199         return self.url_result(
200             NBCSportsVPlayerIE._extract_url(webpage), 'NBCSportsVPlayer')
201
202
203 class CSNNEIE(InfoExtractor):
204     _VALID_URL = r'https?://(?:www\.)?csnne\.com/video/(?P<id>[0-9a-z-]+)'
205
206     _TEST = {
207         'url': 'http://www.csnne.com/video/snc-evening-update-wright-named-red-sox-no-5-starter',
208         'info_dict': {
209             'id': 'yvBLLUgQ8WU0',
210             'ext': 'mp4',
211             'title': 'SNC evening update: Wright named Red Sox\' No. 5 starter.',
212             'description': 'md5:1753cfee40d9352b19b4c9b3e589b9e3',
213             'timestamp': 1459369979,
214             'upload_date': '20160330',
215             'uploader': 'NBCU-SPORTS',
216         }
217     }
218
219     def _real_extract(self, url):
220         display_id = self._match_id(url)
221         webpage = self._download_webpage(url, display_id)
222         return {
223             '_type': 'url_transparent',
224             'ie_key': 'ThePlatform',
225             'url': self._html_search_meta('twitter:player:stream', webpage),
226             'display_id': display_id,
227         }
228
229
230 class NBCNewsIE(ThePlatformIE):
231     _VALID_URL = r'''(?x)https?://(?:www\.)?(?:nbcnews|today|msnbc)\.com/
232         (?:video/.+?/(?P<id>\d+)|
233         ([^/]+/)*(?:.*-)?(?P<mpx_id>[^/?]+))
234         '''
235
236     _TESTS = [
237         {
238             'url': 'http://www.nbcnews.com/video/nbc-news/52753292',
239             'md5': '47abaac93c6eaf9ad37ee6c4463a5179',
240             'info_dict': {
241                 'id': '52753292',
242                 'ext': 'flv',
243                 'title': 'Crew emerges after four-month Mars food study',
244                 'description': 'md5:24e632ffac72b35f8b67a12d1b6ddfc1',
245             },
246         },
247         {
248             'url': 'http://www.nbcnews.com/watch/nbcnews-com/how-twitter-reacted-to-the-snowden-interview-269389891880',
249             'md5': 'af1adfa51312291a017720403826bb64',
250             'info_dict': {
251                 'id': 'p_tweet_snow_140529',
252                 'ext': 'mp4',
253                 'title': 'How Twitter Reacted To The Snowden Interview',
254                 'description': 'md5:65a0bd5d76fe114f3c2727aa3a81fe64',
255                 'uploader': 'NBCU-NEWS',
256                 'timestamp': 1401363060,
257                 'upload_date': '20140529',
258             },
259         },
260         {
261             'url': 'http://www.nbcnews.com/feature/dateline-full-episodes/full-episode-family-business-n285156',
262             'md5': 'fdbf39ab73a72df5896b6234ff98518a',
263             'info_dict': {
264                 'id': '529953347624',
265                 'ext': 'mp4',
266                 'title': 'FULL EPISODE: Family Business',
267                 'description': 'md5:757988edbaae9d7be1d585eb5d55cc04',
268             },
269             'skip': 'This page is unavailable.',
270         },
271         {
272             'url': 'http://www.nbcnews.com/nightly-news/video/nightly-news-with-brian-williams-full-broadcast-february-4-394064451844',
273             'md5': '73135a2e0ef819107bbb55a5a9b2a802',
274             'info_dict': {
275                 'id': 'nn_netcast_150204',
276                 'ext': 'mp4',
277                 'title': 'Nightly News with Brian Williams Full Broadcast (February 4)',
278                 'description': 'md5:1c10c1eccbe84a26e5debb4381e2d3c5',
279                 'timestamp': 1423104900,
280                 'uploader': 'NBCU-NEWS',
281                 'upload_date': '20150205',
282             },
283         },
284         {
285             'url': 'http://www.nbcnews.com/business/autos/volkswagen-11-million-vehicles-could-have-suspect-software-emissions-scandal-n431456',
286             'md5': 'a49e173825e5fcd15c13fc297fced39d',
287             'info_dict': {
288                 'id': 'x_lon_vwhorn_150922',
289                 'ext': 'mp4',
290                 'title': 'Volkswagen U.S. Chief:\xa0 We Have Totally Screwed Up',
291                 'description': 'md5:c8be487b2d80ff0594c005add88d8351',
292                 'upload_date': '20150922',
293                 'timestamp': 1442917800,
294                 'uploader': 'NBCU-NEWS',
295             },
296         },
297         {
298             'url': 'http://www.today.com/video/see-the-aurora-borealis-from-space-in-stunning-new-nasa-video-669831235788',
299             'md5': '118d7ca3f0bea6534f119c68ef539f71',
300             'info_dict': {
301                 'id': 'tdy_al_space_160420',
302                 'ext': 'mp4',
303                 'title': 'See the aurora borealis from space in stunning new NASA video',
304                 'description': 'md5:74752b7358afb99939c5f8bb2d1d04b1',
305                 'upload_date': '20160420',
306                 'timestamp': 1461152093,
307                 'uploader': 'NBCU-NEWS',
308             },
309         },
310         {
311             'url': 'http://www.msnbc.com/all-in-with-chris-hayes/watch/the-chaotic-gop-immigration-vote-314487875924',
312             'md5': '6d236bf4f3dddc226633ce6e2c3f814d',
313             'info_dict': {
314                 'id': 'n_hayes_Aimm_140801_272214',
315                 'ext': 'mp4',
316                 'title': 'The chaotic GOP immigration vote',
317                 '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.',
318                 'thumbnail': r're:^https?://.*\.jpg$',
319                 'timestamp': 1406937606,
320                 'upload_date': '20140802',
321                 'uploader': 'NBCU-NEWS',
322             },
323         },
324         {
325             'url': 'http://www.nbcnews.com/watch/dateline/full-episode--deadly-betrayal-386250819952',
326             'only_matching': True,
327         },
328         {
329             # From http://www.vulture.com/2016/06/letterman-couldnt-care-less-about-late-night.html
330             'url': 'http://www.nbcnews.com/widget/video-embed/701714499682',
331             'only_matching': True,
332         },
333     ]
334
335     def _real_extract(self, url):
336         mobj = re.match(self._VALID_URL, url)
337         video_id = mobj.group('id')
338         if video_id is not None:
339             all_info = self._download_xml('http://www.nbcnews.com/id/%s/displaymode/1219' % video_id, video_id)
340             info = all_info.find('video')
341
342             return {
343                 'id': video_id,
344                 'title': info.find('headline').text,
345                 'ext': 'flv',
346                 'url': find_xpath_attr(info, 'media', 'type', 'flashVideo').text,
347                 'description': info.find('caption').text,
348                 'thumbnail': find_xpath_attr(info, 'media', 'type', 'thumbnail').text,
349             }
350         else:
351             # "feature" and "nightly-news" pages use theplatform.com
352             video_id = mobj.group('mpx_id')
353             webpage = self._download_webpage(url, video_id)
354
355             filter_param = 'byId'
356             bootstrap_json = self._search_regex(
357                 [r'(?m)(?:var\s+(?:bootstrapJson|playlistData)|NEWS\.videoObj)\s*=\s*({.+});?\s*$',
358                  r'videoObj\s*:\s*({.+})', r'data-video="([^"]+)"',
359                  r'jQuery\.extend\(Drupal\.settings\s*,\s*({.+?})\);'],
360                 webpage, 'bootstrap json', default=None)
361             if bootstrap_json:
362                 bootstrap = self._parse_json(
363                     bootstrap_json, video_id, transform_source=unescapeHTML)
364
365                 info = None
366                 if 'results' in bootstrap:
367                     info = bootstrap['results'][0]['video']
368                 elif 'video' in bootstrap:
369                     info = bootstrap['video']
370                 elif 'msnbcVideoInfo' in bootstrap:
371                     info = bootstrap['msnbcVideoInfo']['meta']
372                 elif 'msnbcThePlatform' in bootstrap:
373                     info = bootstrap['msnbcThePlatform']['videoPlayer']['video']
374                 else:
375                     info = bootstrap
376
377                 if 'guid' in info:
378                     video_id = info['guid']
379                     filter_param = 'byGuid'
380                 elif 'mpxId' in info:
381                     video_id = info['mpxId']
382
383             return {
384                 '_type': 'url_transparent',
385                 'id': video_id,
386                 # http://feed.theplatform.com/f/2E2eJC/nbcnews also works
387                 'url': update_url_query('http://feed.theplatform.com/f/2E2eJC/nnd_NBCNews', {filter_param: video_id}),
388                 'ie_key': 'ThePlatformFeed',
389             }
390
391
392 class NBCOlympicsIE(InfoExtractor):
393     _VALID_URL = r'https?://www\.nbcolympics\.com/video/(?P<id>[a-z-]+)'
394
395     _TEST = {
396         # Geo-restricted to US
397         'url': 'http://www.nbcolympics.com/video/justin-roses-son-leo-was-tears-after-his-dad-won-gold',
398         'md5': '54fecf846d05429fbaa18af557ee523a',
399         'info_dict': {
400             'id': 'WjTBzDXx5AUq',
401             'display_id': 'justin-roses-son-leo-was-tears-after-his-dad-won-gold',
402             'ext': 'mp4',
403             'title': 'Rose\'s son Leo was in tears after his dad won gold',
404             'description': 'Olympic gold medalist Justin Rose gets emotional talking to the impact his win in men\'s golf has already had on his children.',
405             'timestamp': 1471274964,
406             'upload_date': '20160815',
407             'uploader': 'NBCU-SPORTS',
408         },
409     }
410
411     def _real_extract(self, url):
412         display_id = self._match_id(url)
413
414         webpage = self._download_webpage(url, display_id)
415
416         drupal_settings = self._parse_json(self._search_regex(
417             r'jQuery\.extend\(Drupal\.settings\s*,\s*({.+?})\);',
418             webpage, 'drupal settings'), display_id)
419
420         iframe_url = drupal_settings['vod']['iframe_url']
421         theplatform_url = iframe_url.replace(
422             'vplayer.nbcolympics.com', 'player.theplatform.com')
423
424         return {
425             '_type': 'url_transparent',
426             'url': theplatform_url,
427             'ie_key': ThePlatformIE.ie_key(),
428             'display_id': display_id,
429         }