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