]> gitweb @ CieloNegro.org - youtube-dl.git/blob - youtube_dl/extractor/nbc.py
[cbs] extract smpte and vtt subtitles
[youtube-dl.git] / youtube_dl / extractor / nbc.py
1 from __future__ import unicode_literals
2
3 import base64
4 import json
5 import re
6
7 from .common import InfoExtractor
8 from .theplatform import ThePlatformIE
9 from .adobepass import AdobePassIE
10 from ..compat import compat_urllib_parse_unquote
11 from ..utils import (
12     smuggle_url,
13     try_get,
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             # Percent escaped url
79             'url': 'https://www.nbc.com/up-all-night/video/day-after-valentine%27s-day/n2189',
80             'only_matching': True,
81         }
82     ]
83
84     def _real_extract(self, url):
85         permalink, video_id = re.match(self._VALID_URL, url).groups()
86         permalink = 'http' + compat_urllib_parse_unquote(permalink)
87         response = self._download_json(
88             'https://api.nbc.com/v3/videos', video_id, query={
89                 'filter[permalink]': permalink,
90                 'fields[videos]': 'description,entitlement,episodeNumber,guid,keywords,seasonNumber,title,vChipRating',
91                 'fields[shows]': 'shortTitle',
92                 'include': 'show.shortTitle',
93             })
94         video_data = response['data'][0]['attributes']
95         query = {
96             'mbr': 'true',
97             'manifest': 'm3u',
98         }
99         video_id = video_data['guid']
100         title = video_data['title']
101         if video_data.get('entitlement') == 'auth':
102             resource = self._get_mvpd_resource(
103                 'nbcentertainment', title, video_id,
104                 video_data.get('vChipRating'))
105             query['auth'] = self._extract_mvpd_auth(
106                 url, video_id, 'nbcentertainment', resource)
107         theplatform_url = smuggle_url(update_url_query(
108             'http://link.theplatform.com/s/NnzsPC/media/guid/2410887629/' + video_id,
109             query), {'force_smil_url': True})
110         return {
111             '_type': 'url_transparent',
112             'id': video_id,
113             'title': title,
114             'url': theplatform_url,
115             'description': video_data.get('description'),
116             'tags': video_data.get('keywords'),
117             'season_number': int_or_none(video_data.get('seasonNumber')),
118             'episode_number': int_or_none(video_data.get('episodeNumber')),
119             'episode': title,
120             'series': try_get(response, lambda x: x['included'][0]['attributes']['shortTitle']),
121             'ie_key': 'ThePlatform',
122         }
123
124
125 class NBCSportsVPlayerIE(InfoExtractor):
126     _VALID_URL = r'https?://vplayer\.nbcsports\.com/(?:[^/]+/)+(?P<id>[0-9a-zA-Z_]+)'
127
128     _TESTS = [{
129         'url': 'https://vplayer.nbcsports.com/p/BxmELC/nbcsports_embed/select/9CsDKds0kvHI',
130         'info_dict': {
131             'id': '9CsDKds0kvHI',
132             'ext': 'mp4',
133             'description': 'md5:df390f70a9ba7c95ff1daace988f0d8d',
134             'title': 'Tyler Kalinoski hits buzzer-beater to lift Davidson',
135             'timestamp': 1426270238,
136             'upload_date': '20150313',
137             'uploader': 'NBCU-SPORTS',
138         }
139     }, {
140         'url': 'https://vplayer.nbcsports.com/p/BxmELC/nbcsports_embed/select/media/_hqLjQ95yx8Z',
141         'only_matching': True,
142     }]
143
144     @staticmethod
145     def _extract_url(webpage):
146         iframe_m = re.search(
147             r'<iframe[^>]+src="(?P<url>https?://vplayer\.nbcsports\.com/[^"]+)"', webpage)
148         if iframe_m:
149             return iframe_m.group('url')
150
151     def _real_extract(self, url):
152         video_id = self._match_id(url)
153         webpage = self._download_webpage(url, video_id)
154         theplatform_url = self._og_search_video_url(webpage).replace(
155             'vplayer.nbcsports.com', 'player.theplatform.com')
156         return self.url_result(theplatform_url, 'ThePlatform')
157
158
159 class NBCSportsIE(InfoExtractor):
160     # Does not include https because its certificate is invalid
161     _VALID_URL = r'https?://(?:www\.)?nbcsports\.com//?(?:[^/]+/)+(?P<id>[0-9a-z-]+)'
162
163     _TEST = {
164         'url': 'http://www.nbcsports.com//college-basketball/ncaab/tom-izzo-michigan-st-has-so-much-respect-duke',
165         'info_dict': {
166             'id': 'PHJSaFWbrTY9',
167             'ext': 'flv',
168             'title': 'Tom Izzo, Michigan St. has \'so much respect\' for Duke',
169             'description': 'md5:ecb459c9d59e0766ac9c7d5d0eda8113',
170             'uploader': 'NBCU-SPORTS',
171             'upload_date': '20150330',
172             'timestamp': 1427726529,
173         }
174     }
175
176     def _real_extract(self, url):
177         video_id = self._match_id(url)
178         webpage = self._download_webpage(url, video_id)
179         return self.url_result(
180             NBCSportsVPlayerIE._extract_url(webpage), 'NBCSportsVPlayer')
181
182
183 class NBCSportsStreamIE(AdobePassIE):
184     _VALID_URL = r'https?://stream\.nbcsports\.com/.+?\bpid=(?P<id>\d+)'
185     _TEST = {
186         'url': 'http://stream.nbcsports.com/nbcsn/generic?pid=206559',
187         'info_dict': {
188             'id': '206559',
189             'ext': 'mp4',
190             'title': 'Amgen Tour of California Women\'s Recap',
191             'description': 'md5:66520066b3b5281ada7698d0ea2aa894',
192         },
193         'params': {
194             # m3u8 download
195             'skip_download': True,
196         },
197         'skip': 'Requires Adobe Pass Authentication',
198     }
199
200     def _real_extract(self, url):
201         video_id = self._match_id(url)
202         live_source = self._download_json(
203             'http://stream.nbcsports.com/data/live_sources_%s.json' % video_id,
204             video_id)
205         video_source = live_source['videoSources'][0]
206         title = video_source['title']
207         source_url = None
208         for k in ('source', 'msl4source', 'iossource', 'hlsv4'):
209             sk = k + 'Url'
210             source_url = video_source.get(sk) or video_source.get(sk + 'Alt')
211             if source_url:
212                 break
213         else:
214             source_url = video_source['ottStreamUrl']
215         is_live = video_source.get('type') == 'live' or video_source.get('status') == 'Live'
216         resource = self._get_mvpd_resource('nbcsports', title, video_id, '')
217         token = self._extract_mvpd_auth(url, video_id, 'nbcsports', resource)
218         tokenized_url = self._download_json(
219             'https://token.playmakerservices.com/cdn',
220             video_id, data=json.dumps({
221                 'requestorId': 'nbcsports',
222                 'pid': video_id,
223                 'application': 'NBCSports',
224                 'version': 'v1',
225                 'platform': 'desktop',
226                 'cdn': 'akamai',
227                 'url': video_source['sourceUrl'],
228                 'token': base64.b64encode(token.encode()).decode(),
229                 'resourceId': base64.b64encode(resource.encode()).decode(),
230             }).encode())['tokenizedUrl']
231         formats = self._extract_m3u8_formats(tokenized_url, video_id, 'mp4')
232         self._sort_formats(formats)
233         return {
234             'id': video_id,
235             'title': self._live_title(title) if is_live else title,
236             'description': live_source.get('description'),
237             'formats': formats,
238             'is_live': is_live,
239         }
240
241
242 class CSNNEIE(InfoExtractor):
243     _VALID_URL = r'https?://(?:www\.)?csnne\.com/video/(?P<id>[0-9a-z-]+)'
244
245     _TEST = {
246         'url': 'http://www.csnne.com/video/snc-evening-update-wright-named-red-sox-no-5-starter',
247         'info_dict': {
248             'id': 'yvBLLUgQ8WU0',
249             'ext': 'mp4',
250             'title': 'SNC evening update: Wright named Red Sox\' No. 5 starter.',
251             'description': 'md5:1753cfee40d9352b19b4c9b3e589b9e3',
252             'timestamp': 1459369979,
253             'upload_date': '20160330',
254             'uploader': 'NBCU-SPORTS',
255         }
256     }
257
258     def _real_extract(self, url):
259         display_id = self._match_id(url)
260         webpage = self._download_webpage(url, display_id)
261         return {
262             '_type': 'url_transparent',
263             'ie_key': 'ThePlatform',
264             'url': self._html_search_meta('twitter:player:stream', webpage),
265             'display_id': display_id,
266         }
267
268
269 class NBCNewsIE(ThePlatformIE):
270     _VALID_URL = r'(?x)https?://(?:www\.)?(?:nbcnews|today|msnbc)\.com/([^/]+/)*(?:.*-)?(?P<id>[^/?]+)'
271
272     _TESTS = [
273         {
274             'url': 'http://www.nbcnews.com/watch/nbcnews-com/how-twitter-reacted-to-the-snowden-interview-269389891880',
275             'md5': 'af1adfa51312291a017720403826bb64',
276             'info_dict': {
277                 'id': '269389891880',
278                 'ext': 'mp4',
279                 'title': 'How Twitter Reacted To The Snowden Interview',
280                 'description': 'md5:65a0bd5d76fe114f3c2727aa3a81fe64',
281                 'uploader': 'NBCU-NEWS',
282                 'timestamp': 1401363060,
283                 'upload_date': '20140529',
284             },
285         },
286         {
287             'url': 'http://www.nbcnews.com/feature/dateline-full-episodes/full-episode-family-business-n285156',
288             'md5': 'fdbf39ab73a72df5896b6234ff98518a',
289             'info_dict': {
290                 'id': '529953347624',
291                 'ext': 'mp4',
292                 'title': 'FULL EPISODE: Family Business',
293                 'description': 'md5:757988edbaae9d7be1d585eb5d55cc04',
294             },
295             'skip': 'This page is unavailable.',
296         },
297         {
298             'url': 'http://www.nbcnews.com/nightly-news/video/nightly-news-with-brian-williams-full-broadcast-february-4-394064451844',
299             'md5': '73135a2e0ef819107bbb55a5a9b2a802',
300             'info_dict': {
301                 'id': '394064451844',
302                 'ext': 'mp4',
303                 'title': 'Nightly News with Brian Williams Full Broadcast (February 4)',
304                 'description': 'md5:1c10c1eccbe84a26e5debb4381e2d3c5',
305                 'timestamp': 1423104900,
306                 'uploader': 'NBCU-NEWS',
307                 'upload_date': '20150205',
308             },
309         },
310         {
311             'url': 'http://www.nbcnews.com/business/autos/volkswagen-11-million-vehicles-could-have-suspect-software-emissions-scandal-n431456',
312             'md5': 'a49e173825e5fcd15c13fc297fced39d',
313             'info_dict': {
314                 'id': '529953347624',
315                 'ext': 'mp4',
316                 'title': 'Volkswagen U.S. Chief:\xa0 We Have Totally Screwed Up',
317                 'description': 'md5:c8be487b2d80ff0594c005add88d8351',
318                 'upload_date': '20150922',
319                 'timestamp': 1442917800,
320                 'uploader': 'NBCU-NEWS',
321             },
322         },
323         {
324             'url': 'http://www.today.com/video/see-the-aurora-borealis-from-space-in-stunning-new-nasa-video-669831235788',
325             'md5': '118d7ca3f0bea6534f119c68ef539f71',
326             'info_dict': {
327                 'id': '669831235788',
328                 'ext': 'mp4',
329                 'title': 'See the aurora borealis from space in stunning new NASA video',
330                 'description': 'md5:74752b7358afb99939c5f8bb2d1d04b1',
331                 'upload_date': '20160420',
332                 'timestamp': 1461152093,
333                 'uploader': 'NBCU-NEWS',
334             },
335         },
336         {
337             'url': 'http://www.msnbc.com/all-in-with-chris-hayes/watch/the-chaotic-gop-immigration-vote-314487875924',
338             'md5': '6d236bf4f3dddc226633ce6e2c3f814d',
339             'info_dict': {
340                 'id': '314487875924',
341                 'ext': 'mp4',
342                 'title': 'The chaotic GOP immigration vote',
343                 '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.',
344                 'thumbnail': r're:^https?://.*\.jpg$',
345                 'timestamp': 1406937606,
346                 'upload_date': '20140802',
347                 'uploader': 'NBCU-NEWS',
348             },
349         },
350         {
351             'url': 'http://www.nbcnews.com/watch/dateline/full-episode--deadly-betrayal-386250819952',
352             'only_matching': True,
353         },
354         {
355             # From http://www.vulture.com/2016/06/letterman-couldnt-care-less-about-late-night.html
356             'url': 'http://www.nbcnews.com/widget/video-embed/701714499682',
357             'only_matching': True,
358         },
359     ]
360
361     def _real_extract(self, url):
362         video_id = self._match_id(url)
363         if not video_id.isdigit():
364             webpage = self._download_webpage(url, video_id)
365
366             data = self._parse_json(self._search_regex(
367                 r'window\.__data\s*=\s*({.+});', webpage,
368                 'bootstrap json'), video_id)
369             video_id = data['article']['content'][0]['primaryMedia']['video']['mpxMetadata']['id']
370
371         return {
372             '_type': 'url_transparent',
373             'id': video_id,
374             # http://feed.theplatform.com/f/2E2eJC/nbcnews also works
375             'url': update_url_query('http://feed.theplatform.com/f/2E2eJC/nnd_NBCNews', {'byId': video_id}),
376             'ie_key': 'ThePlatformFeed',
377         }
378
379
380 class NBCOlympicsIE(InfoExtractor):
381     IE_NAME = 'nbcolympics'
382     _VALID_URL = r'https?://www\.nbcolympics\.com/video/(?P<id>[a-z-]+)'
383
384     _TEST = {
385         # Geo-restricted to US
386         'url': 'http://www.nbcolympics.com/video/justin-roses-son-leo-was-tears-after-his-dad-won-gold',
387         'md5': '54fecf846d05429fbaa18af557ee523a',
388         'info_dict': {
389             'id': 'WjTBzDXx5AUq',
390             'display_id': 'justin-roses-son-leo-was-tears-after-his-dad-won-gold',
391             'ext': 'mp4',
392             'title': 'Rose\'s son Leo was in tears after his dad won gold',
393             'description': 'Olympic gold medalist Justin Rose gets emotional talking to the impact his win in men\'s golf has already had on his children.',
394             'timestamp': 1471274964,
395             'upload_date': '20160815',
396             'uploader': 'NBCU-SPORTS',
397         },
398     }
399
400     def _real_extract(self, url):
401         display_id = self._match_id(url)
402
403         webpage = self._download_webpage(url, display_id)
404
405         drupal_settings = self._parse_json(self._search_regex(
406             r'jQuery\.extend\(Drupal\.settings\s*,\s*({.+?})\);',
407             webpage, 'drupal settings'), display_id)
408
409         iframe_url = drupal_settings['vod']['iframe_url']
410         theplatform_url = iframe_url.replace(
411             'vplayer.nbcolympics.com', 'player.theplatform.com')
412
413         return {
414             '_type': 'url_transparent',
415             'url': theplatform_url,
416             'ie_key': ThePlatformIE.ie_key(),
417             'display_id': display_id,
418         }
419
420
421 class NBCOlympicsStreamIE(AdobePassIE):
422     IE_NAME = 'nbcolympics:stream'
423     _VALID_URL = r'https?://stream\.nbcolympics\.com/(?P<id>[0-9a-z-]+)'
424     _TEST = {
425         'url': 'http://stream.nbcolympics.com/2018-winter-olympics-nbcsn-evening-feb-8',
426         'info_dict': {
427             'id': '203493',
428             'ext': 'mp4',
429             'title': 're:Curling, Alpine, Luge [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
430         },
431         'params': {
432             # m3u8 download
433             'skip_download': True,
434         },
435     }
436     _DATA_URL_TEMPLATE = 'http://stream.nbcolympics.com/data/%s_%s.json'
437
438     def _real_extract(self, url):
439         display_id = self._match_id(url)
440         webpage = self._download_webpage(url, display_id)
441         pid = self._search_regex(r'pid\s*=\s*(\d+);', webpage, 'pid')
442         resource = self._search_regex(
443             r"resource\s*=\s*'(.+)';", webpage,
444             'resource').replace("' + pid + '", pid)
445         event_config = self._download_json(
446             self._DATA_URL_TEMPLATE % ('event_config', pid),
447             pid)['eventConfig']
448         title = self._live_title(event_config['eventTitle'])
449         source_url = self._download_json(
450             self._DATA_URL_TEMPLATE % ('live_sources', pid),
451             pid)['videoSources'][0]['sourceUrl']
452         media_token = self._extract_mvpd_auth(
453             url, pid, event_config.get('requestorId', 'NBCOlympics'), resource)
454         formats = self._extract_m3u8_formats(self._download_webpage(
455             'http://sp.auth.adobe.com/tvs/v1/sign', pid, query={
456                 'cdn': 'akamai',
457                 'mediaToken': base64.b64encode(media_token.encode()),
458                 'resource': base64.b64encode(resource.encode()),
459                 'url': source_url,
460             }), pid, 'mp4')
461         self._sort_formats(formats)
462
463         return {
464             'id': pid,
465             'display_id': display_id,
466             'title': title,
467             'formats': formats,
468             'is_live': True,
469         }