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