[nbc] add support for today.com(closes #2909)
[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 ..utils import (
8     find_xpath_attr,
9     lowercase_escape,
10     smuggle_url,
11     unescapeHTML,
12     update_url_query,
13     int_or_none,
14     HEADRequest,
15     parse_iso8601,
16 )
17
18
19 class NBCIE(InfoExtractor):
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/segments/112966',
25             'info_dict': {
26                 'id': '112966',
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
73     def _real_extract(self, url):
74         video_id = self._match_id(url)
75         webpage = self._download_webpage(url, video_id)
76         theplatform_url = unescapeHTML(lowercase_escape(self._html_search_regex(
77             [
78                 r'(?:class="video-player video-player-full" data-mpx-url|class="player" src)="(.*?)"',
79                 r'<iframe[^>]+src="((?:https?:)?//player\.theplatform\.com/[^"]+)"',
80                 r'"embedURL"\s*:\s*"([^"]+)"'
81             ],
82             webpage, 'theplatform url').replace('_no_endcard', '').replace('\\/', '/')))
83         if theplatform_url.startswith('//'):
84             theplatform_url = 'http:' + theplatform_url
85         return {
86             '_type': 'url_transparent',
87             'ie_key': 'ThePlatform',
88             'url': smuggle_url(theplatform_url, {'source_url': url}),
89             'id': video_id,
90         }
91
92
93 class NBCSportsVPlayerIE(InfoExtractor):
94     _VALID_URL = r'https?://vplayer\.nbcsports\.com/(?:[^/]+/)+(?P<id>[0-9a-zA-Z_]+)'
95
96     _TESTS = [{
97         'url': 'https://vplayer.nbcsports.com/p/BxmELC/nbcsports_share/select/9CsDKds0kvHI',
98         'info_dict': {
99             'id': '9CsDKds0kvHI',
100             'ext': 'flv',
101             'description': 'md5:df390f70a9ba7c95ff1daace988f0d8d',
102             'title': 'Tyler Kalinoski hits buzzer-beater to lift Davidson',
103             'timestamp': 1426270238,
104             'upload_date': '20150313',
105             'uploader': 'NBCU-SPORTS',
106         }
107     }, {
108         'url': 'http://vplayer.nbcsports.com/p/BxmELC/nbc_embedshare/select/_hqLjQ95yx8Z',
109         'only_matching': True,
110     }]
111
112     @staticmethod
113     def _extract_url(webpage):
114         iframe_m = re.search(
115             r'<iframe[^>]+src="(?P<url>https?://vplayer\.nbcsports\.com/[^"]+)"', webpage)
116         if iframe_m:
117             return iframe_m.group('url')
118
119     def _real_extract(self, url):
120         video_id = self._match_id(url)
121         webpage = self._download_webpage(url, video_id)
122         theplatform_url = self._og_search_video_url(webpage)
123         return self.url_result(theplatform_url, 'ThePlatform')
124
125
126 class NBCSportsIE(InfoExtractor):
127     # Does not include https because its certificate is invalid
128     _VALID_URL = r'https?://www\.nbcsports\.com//?(?:[^/]+/)+(?P<id>[0-9a-z-]+)'
129
130     _TEST = {
131         'url': 'http://www.nbcsports.com//college-basketball/ncaab/tom-izzo-michigan-st-has-so-much-respect-duke',
132         'info_dict': {
133             'id': 'PHJSaFWbrTY9',
134             'ext': 'flv',
135             'title': 'Tom Izzo, Michigan St. has \'so much respect\' for Duke',
136             'description': 'md5:ecb459c9d59e0766ac9c7d5d0eda8113',
137         }
138     }
139
140     def _real_extract(self, url):
141         video_id = self._match_id(url)
142         webpage = self._download_webpage(url, video_id)
143         return self.url_result(
144             NBCSportsVPlayerIE._extract_url(webpage), 'NBCSportsVPlayer')
145
146
147 class CSNNEIE(InfoExtractor):
148     _VALID_URL = r'https?://www\.csnne\.com/video/(?P<id>[0-9a-z-]+)'
149
150     _TEST = {
151         'url': 'http://www.csnne.com/video/snc-evening-update-wright-named-red-sox-no-5-starter',
152         'info_dict': {
153             'id': 'yvBLLUgQ8WU0',
154             'ext': 'mp4',
155             'title': 'SNC evening update: Wright named Red Sox\' No. 5 starter.',
156             'description': 'md5:1753cfee40d9352b19b4c9b3e589b9e3',
157             'timestamp': 1459369979,
158             'upload_date': '20160330',
159             'uploader': 'NBCU-SPORTS',
160         }
161     }
162
163     def _real_extract(self, url):
164         display_id = self._match_id(url)
165         webpage = self._download_webpage(url, display_id)
166         return {
167             '_type': 'url_transparent',
168             'ie_key': 'ThePlatform',
169             'url': self._html_search_meta('twitter:player:stream', webpage),
170             'display_id': display_id,
171         }
172
173
174 class NBCNewsIE(ThePlatformIE):
175     _VALID_URL = r'''(?x)https?://(?:www\.)?(?:nbcnews|today)\.com/
176         (?:video/.+?/(?P<id>\d+)|
177         ([^/]+/)*(?P<display_id>[^/?]+))
178         '''
179
180     _TESTS = [
181         {
182             'url': 'http://www.nbcnews.com/video/nbc-news/52753292',
183             'md5': '47abaac93c6eaf9ad37ee6c4463a5179',
184             'info_dict': {
185                 'id': '52753292',
186                 'ext': 'flv',
187                 'title': 'Crew emerges after four-month Mars food study',
188                 'description': 'md5:24e632ffac72b35f8b67a12d1b6ddfc1',
189             },
190         },
191         {
192             'url': 'http://www.nbcnews.com/watch/nbcnews-com/how-twitter-reacted-to-the-snowden-interview-269389891880',
193             'md5': 'af1adfa51312291a017720403826bb64',
194             'info_dict': {
195                 'id': '269389891880',
196                 'ext': 'mp4',
197                 'title': 'How Twitter Reacted To The Snowden Interview',
198                 'description': 'md5:65a0bd5d76fe114f3c2727aa3a81fe64',
199             },
200         },
201         {
202             'url': 'http://www.nbcnews.com/feature/dateline-full-episodes/full-episode-family-business-n285156',
203             'md5': 'fdbf39ab73a72df5896b6234ff98518a',
204             'info_dict': {
205                 'id': 'Wjf9EDR3A_60',
206                 'ext': 'mp4',
207                 'title': 'FULL EPISODE: Family Business',
208                 'description': 'md5:757988edbaae9d7be1d585eb5d55cc04',
209             },
210             'skip': 'This page is unavailable.',
211         },
212         {
213             'url': 'http://www.nbcnews.com/nightly-news/video/nightly-news-with-brian-williams-full-broadcast-february-4-394064451844',
214             'md5': '73135a2e0ef819107bbb55a5a9b2a802',
215             'info_dict': {
216                 'id': '394064451844',
217                 'ext': 'mp4',
218                 'title': 'Nightly News with Brian Williams Full Broadcast (February 4)',
219                 'description': 'md5:1c10c1eccbe84a26e5debb4381e2d3c5',
220             },
221         },
222         {
223             'url': 'http://www.nbcnews.com/business/autos/volkswagen-11-million-vehicles-could-have-suspect-software-emissions-scandal-n431456',
224             'md5': 'a49e173825e5fcd15c13fc297fced39d',
225             'info_dict': {
226                 'id': '529953347624',
227                 'ext': 'mp4',
228                 'title': 'Volkswagen U.S. Chief: We \'Totally Screwed Up\'',
229                 'description': 'md5:d22d1281a24f22ea0880741bb4dd6301',
230             },
231             'expected_warnings': ['http-6000 is not available']
232         },
233         {
234             'url': 'http://www.today.com/video/see-the-aurora-borealis-from-space-in-stunning-new-nasa-video-669831235788',
235             'md5': '118d7ca3f0bea6534f119c68ef539f71',
236             'info_dict': {
237                 'id': '669831235788',
238                 'ext': 'mp4',
239                 'title': 'See the aurora borealis from space in stunning new NASA video',
240                 'description': 'md5:74752b7358afb99939c5f8bb2d1d04b1',
241                 'upload_date': '20160420',
242                 'timestamp': 1461152093,
243             },
244         },
245         {
246             'url': 'http://www.nbcnews.com/watch/dateline/full-episode--deadly-betrayal-386250819952',
247             'only_matching': True,
248         },
249     ]
250
251     def _real_extract(self, url):
252         mobj = re.match(self._VALID_URL, url)
253         video_id = mobj.group('id')
254         if video_id is not None:
255             all_info = self._download_xml('http://www.nbcnews.com/id/%s/displaymode/1219' % video_id, video_id)
256             info = all_info.find('video')
257
258             return {
259                 'id': video_id,
260                 'title': info.find('headline').text,
261                 'ext': 'flv',
262                 'url': find_xpath_attr(info, 'media', 'type', 'flashVideo').text,
263                 'description': info.find('caption').text,
264                 'thumbnail': find_xpath_attr(info, 'media', 'type', 'thumbnail').text,
265             }
266         else:
267             # "feature" and "nightly-news" pages use theplatform.com
268             display_id = mobj.group('display_id')
269             webpage = self._download_webpage(url, display_id)
270             info = None
271             bootstrap_json = self._search_regex(
272                 r'(?m)var\s+(?:bootstrapJson|playlistData)\s*=\s*({.+});?\s*$',
273                 webpage, 'bootstrap json', default=None)
274             if bootstrap_json:
275                 bootstrap = self._parse_json(bootstrap_json, display_id)
276                 info = bootstrap['results'][0]['video']
277             else:
278                 player_instance_json = self._search_regex(
279                     r'videoObj\s*:\s*({.+})', webpage, 'player instance', default=None)
280                 if not player_instance_json:
281                     player_instance_json = self._html_search_regex(
282                         r'data-video="([^"]+)"', webpage, 'video json')
283                 info = self._parse_json(player_instance_json, display_id)
284             video_id = info['mpxId']
285             title = info['title']
286
287             subtitles = {}
288             caption_links = info.get('captionLinks')
289             if caption_links:
290                 for (sub_key, sub_ext) in (('smpte-tt', 'ttml'), ('web-vtt', 'vtt'), ('srt', 'srt')):
291                     sub_url = caption_links.get(sub_key)
292                     if sub_url:
293                         subtitles.setdefault('en', []).append({
294                             'url': sub_url,
295                             'ext': sub_ext,
296                         })
297
298             formats = []
299             for video_asset in info['videoAssets']:
300                 video_url = video_asset.get('publicUrl')
301                 if not video_url:
302                     continue
303                 container = video_asset.get('format')
304                 asset_type = video_asset.get('assetType') or ''
305                 if container == 'ISM' or asset_type == 'FireTV-Once':
306                     continue
307                 elif asset_type == 'OnceURL':
308                     tp_formats, tp_subtitles = self._extract_theplatform_smil(
309                         video_url, video_id)
310                     formats.extend(tp_formats)
311                     subtitles = self._merge_subtitles(subtitles, tp_subtitles)
312                 else:
313                     tbr = int_or_none(video_asset.get('bitRate') or video_asset.get('bitrate'), 1000)
314                     format_id = 'http%s' % ('-%d' % tbr if tbr else '')
315                     video_url = update_url_query(
316                         video_url, {'format': 'redirect'})
317                     # resolve the url so that we can check availability and detect the correct extension
318                     head = self._request_webpage(
319                         HEADRequest(video_url), video_id,
320                         'Checking %s url' % format_id,
321                         '%s is not available' % format_id,
322                         fatal=False)
323                     if head:
324                         video_url = head.geturl()
325                         formats.append({
326                             'format_id': format_id,
327                             'url': video_url,
328                             'width': int_or_none(video_asset.get('width')),
329                             'height': int_or_none(video_asset.get('height')),
330                             'tbr': tbr,
331                             'container': video_asset.get('format'),
332                         })
333             self._sort_formats(formats)
334
335             return {
336                 'id': video_id,
337                 'title': title,
338                 'description': info.get('description'),
339                 'thumbnail': info.get('thumbnail'),
340                 'duration': int_or_none(info.get('duration')),
341                 'timestamp': parse_iso8601(info.get('pubDate') or info.get('pub_date')),
342                 'formats': formats,
343                 'subtitles': subtitles,
344             }
345
346
347 class MSNBCIE(InfoExtractor):
348     # https URLs redirect to corresponding http ones
349     _VALID_URL = r'https?://www\.msnbc\.com/[^/]+/watch/(?P<id>[^/]+)'
350     _TEST = {
351         'url': 'http://www.msnbc.com/all-in-with-chris-hayes/watch/the-chaotic-gop-immigration-vote-314487875924',
352         'md5': '6d236bf4f3dddc226633ce6e2c3f814d',
353         'info_dict': {
354             'id': 'n_hayes_Aimm_140801_272214',
355             'ext': 'mp4',
356             'title': 'The chaotic GOP immigration vote',
357             '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.',
358             'thumbnail': 're:^https?://.*\.jpg$',
359             'timestamp': 1406937606,
360             'upload_date': '20140802',
361             'uploader': 'NBCU-NEWS',
362             'categories': ['MSNBC/Topics/Franchise/Best of last night', 'MSNBC/Topics/General/Congress'],
363         },
364     }
365
366     def _real_extract(self, url):
367         video_id = self._match_id(url)
368         webpage = self._download_webpage(url, video_id)
369         embed_url = self._html_search_meta('embedURL', webpage)
370         return self.url_result(embed_url)