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