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