Merge branch 'gamekings_fix' of https://github.com/rrooij/youtube-dl into rrooij...
[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 ..compat import compat_HTTPError
7 from ..utils import (
8     ExtractorError,
9     find_xpath_attr,
10     lowercase_escape,
11     smuggle_url,
12     unescapeHTML,
13 )
14
15
16 class NBCIE(InfoExtractor):
17     _VALID_URL = r'https?://www\.nbc\.com/(?:[^/]+/)+(?P<id>n?\d+)'
18
19     _TESTS = [
20         {
21             'url': 'http://www.nbc.com/the-tonight-show/segments/112966',
22             'info_dict': {
23                 'id': '112966',
24                 'ext': 'mp4',
25                 'title': 'Jimmy Fallon Surprises Fans at Ben & Jerry\'s',
26                 'description': 'Jimmy gives out free scoops of his new "Tonight Dough" ice cream flavor by surprising customers at the Ben & Jerry\'s scoop shop.',
27             },
28             'params': {
29                 # m3u8 download
30                 'skip_download': True,
31             },
32         },
33         {
34             'url': 'http://www.nbc.com/the-tonight-show/episodes/176',
35             'info_dict': {
36                 'id': '176',
37                 'ext': 'flv',
38                 'title': 'Ricky Gervais, Steven Van Zandt, ILoveMakonnen',
39                 'description': 'A brand new episode of The Tonight Show welcomes Ricky Gervais, Steven Van Zandt and ILoveMakonnen.',
40             },
41             'skip': '404 Not Found',
42         },
43         {
44             'url': 'http://www.nbc.com/saturday-night-live/video/star-wars-teaser/2832821',
45             'info_dict': {
46                 'id': '2832821',
47                 'ext': 'mp4',
48                 'title': 'Star Wars Teaser',
49                 'description': 'md5:0b40f9cbde5b671a7ff62fceccc4f442',
50             },
51             'params': {
52                 # m3u8 download
53                 'skip_download': True,
54             },
55             'skip': 'Only works from US',
56         },
57         {
58             # This video has expired but with an escaped embedURL
59             'url': 'http://www.nbc.com/parenthood/episode-guide/season-5/just-like-at-home/515',
60             'skip': 'Expired'
61         }
62     ]
63
64     def _real_extract(self, url):
65         video_id = self._match_id(url)
66         webpage = self._download_webpage(url, video_id)
67         theplatform_url = unescapeHTML(lowercase_escape(self._html_search_regex(
68             [
69                 r'(?:class="video-player video-player-full" data-mpx-url|class="player" src)="(.*?)"',
70                 r'<iframe[^>]+src="((?:https?:)?//player\.theplatform\.com/[^"]+)"',
71                 r'"embedURL"\s*:\s*"([^"]+)"'
72             ],
73             webpage, 'theplatform url').replace('_no_endcard', '').replace('\\/', '/')))
74         if theplatform_url.startswith('//'):
75             theplatform_url = 'http:' + theplatform_url
76         return {
77             '_type': 'url_transparent',
78             'url': smuggle_url(theplatform_url, {'source_url': url}),
79             'id': video_id,
80         }
81
82
83 class NBCSportsVPlayerIE(InfoExtractor):
84     _VALID_URL = r'https?://vplayer\.nbcsports\.com/(?:[^/]+/)+(?P<id>[0-9a-zA-Z_]+)'
85
86     _TESTS = [{
87         'url': 'https://vplayer.nbcsports.com/p/BxmELC/nbcsports_share/select/9CsDKds0kvHI',
88         'info_dict': {
89             'id': '9CsDKds0kvHI',
90             'ext': 'flv',
91             'description': 'md5:df390f70a9ba7c95ff1daace988f0d8d',
92             'title': 'Tyler Kalinoski hits buzzer-beater to lift Davidson',
93         }
94     }, {
95         'url': 'http://vplayer.nbcsports.com/p/BxmELC/nbc_embedshare/select/_hqLjQ95yx8Z',
96         'only_matching': True,
97     }]
98
99     @staticmethod
100     def _extract_url(webpage):
101         iframe_m = re.search(
102             r'<iframe[^>]+src="(?P<url>https?://vplayer\.nbcsports\.com/[^"]+)"', webpage)
103         if iframe_m:
104             return iframe_m.group('url')
105
106     def _real_extract(self, url):
107         video_id = self._match_id(url)
108         webpage = self._download_webpage(url, video_id)
109         theplatform_url = self._og_search_video_url(webpage)
110         return self.url_result(theplatform_url, 'ThePlatform')
111
112
113 class NBCSportsIE(InfoExtractor):
114     # Does not include https because its certificate is invalid
115     _VALID_URL = r'http://www\.nbcsports\.com//?(?:[^/]+/)+(?P<id>[0-9a-z-]+)'
116
117     _TEST = {
118         'url': 'http://www.nbcsports.com//college-basketball/ncaab/tom-izzo-michigan-st-has-so-much-respect-duke',
119         'info_dict': {
120             'id': 'PHJSaFWbrTY9',
121             'ext': 'flv',
122             'title': 'Tom Izzo, Michigan St. has \'so much respect\' for Duke',
123             'description': 'md5:ecb459c9d59e0766ac9c7d5d0eda8113',
124         }
125     }
126
127     def _real_extract(self, url):
128         video_id = self._match_id(url)
129         webpage = self._download_webpage(url, video_id)
130         return self.url_result(
131             NBCSportsVPlayerIE._extract_url(webpage), 'NBCSportsVPlayer')
132
133
134 class NBCNewsIE(InfoExtractor):
135     _VALID_URL = r'''(?x)https?://(?:www\.)?nbcnews\.com/
136         (?:video/.+?/(?P<id>\d+)|
137         (?:watch|feature|nightly-news)/[^/]+/(?P<title>.+))
138         '''
139
140     _TESTS = [
141         {
142             'url': 'http://www.nbcnews.com/video/nbc-news/52753292',
143             'md5': '47abaac93c6eaf9ad37ee6c4463a5179',
144             'info_dict': {
145                 'id': '52753292',
146                 'ext': 'flv',
147                 'title': 'Crew emerges after four-month Mars food study',
148                 'description': 'md5:24e632ffac72b35f8b67a12d1b6ddfc1',
149             },
150         },
151         {
152             'url': 'http://www.nbcnews.com/feature/edward-snowden-interview/how-twitter-reacted-snowden-interview-n117236',
153             'md5': 'b2421750c9f260783721d898f4c42063',
154             'info_dict': {
155                 'id': 'I1wpAI_zmhsQ',
156                 'ext': 'mp4',
157                 'title': 'How Twitter Reacted To The Snowden Interview',
158                 'description': 'md5:65a0bd5d76fe114f3c2727aa3a81fe64',
159             },
160             'add_ie': ['ThePlatform'],
161         },
162         {
163             'url': 'http://www.nbcnews.com/feature/dateline-full-episodes/full-episode-family-business-n285156',
164             'md5': 'fdbf39ab73a72df5896b6234ff98518a',
165             'info_dict': {
166                 'id': 'Wjf9EDR3A_60',
167                 'ext': 'mp4',
168                 'title': 'FULL EPISODE: Family Business',
169                 'description': 'md5:757988edbaae9d7be1d585eb5d55cc04',
170             },
171         },
172         {
173             'url': 'http://www.nbcnews.com/nightly-news/video/nightly-news-with-brian-williams-full-broadcast-february-4-394064451844',
174             'md5': 'b5dda8cddd8650baa0dcb616dd2cf60d',
175             'info_dict': {
176                 'id': 'sekXqyTVnmN3',
177                 'ext': 'mp4',
178                 'title': 'Nightly News with Brian Williams Full Broadcast (February 4)',
179                 'description': 'md5:1c10c1eccbe84a26e5debb4381e2d3c5',
180             },
181         },
182         {
183             'url': 'http://www.nbcnews.com/watch/dateline/full-episode--deadly-betrayal-386250819952',
184             'only_matching': True,
185         },
186     ]
187
188     def _real_extract(self, url):
189         mobj = re.match(self._VALID_URL, url)
190         video_id = mobj.group('id')
191         if video_id is not None:
192             all_info = self._download_xml('http://www.nbcnews.com/id/%s/displaymode/1219' % video_id, video_id)
193             info = all_info.find('video')
194
195             return {
196                 'id': video_id,
197                 'title': info.find('headline').text,
198                 'ext': 'flv',
199                 'url': find_xpath_attr(info, 'media', 'type', 'flashVideo').text,
200                 'description': info.find('caption').text,
201                 'thumbnail': find_xpath_attr(info, 'media', 'type', 'thumbnail').text,
202             }
203         else:
204             # "feature" and "nightly-news" pages use theplatform.com
205             title = mobj.group('title')
206             webpage = self._download_webpage(url, title)
207             bootstrap_json = self._search_regex(
208                 r'var\s+(?:bootstrapJson|playlistData)\s*=\s*({.+});?\s*$',
209                 webpage, 'bootstrap json', flags=re.MULTILINE)
210             bootstrap = self._parse_json(bootstrap_json, video_id)
211             info = bootstrap['results'][0]['video']
212             mpxid = info['mpxId']
213
214             base_urls = [
215                 info['fallbackPlaylistUrl'],
216                 info['associatedPlaylistUrl'],
217             ]
218
219             for base_url in base_urls:
220                 if not base_url:
221                     continue
222                 playlist_url = base_url + '?form=MPXNBCNewsAPI'
223
224                 try:
225                     all_videos = self._download_json(playlist_url, title)
226                 except ExtractorError as ee:
227                     if isinstance(ee.cause, compat_HTTPError):
228                         continue
229                     raise
230
231                 if not all_videos or 'videos' not in all_videos:
232                     continue
233
234                 try:
235                     info = next(v for v in all_videos['videos'] if v['mpxId'] == mpxid)
236                     break
237                 except StopIteration:
238                     continue
239
240             if info is None:
241                 raise ExtractorError('Could not find video in playlists')
242
243             return {
244                 '_type': 'url',
245                 # We get the best quality video
246                 'url': info['videoAssets'][-1]['publicUrl'],
247                 'ie_key': 'ThePlatform',
248             }
249
250
251 class MSNBCIE(InfoExtractor):
252     # https URLs redirect to corresponding http ones
253     _VALID_URL = r'http://www\.msnbc\.com/[^/]+/watch/(?P<id>[^/]+)'
254     _TEST = {
255         'url': 'http://www.msnbc.com/all-in-with-chris-hayes/watch/the-chaotic-gop-immigration-vote-314487875924',
256         'md5': '6d236bf4f3dddc226633ce6e2c3f814d',
257         'info_dict': {
258             'id': 'n_hayes_Aimm_140801_272214',
259             'ext': 'mp4',
260             'title': 'The chaotic GOP immigration vote',
261             '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.',
262             'thumbnail': 're:^https?://.*\.jpg$',
263             'timestamp': 1406937606,
264             'upload_date': '20140802',
265             'categories': ['MSNBC/Topics/Franchise/Best of last night', 'MSNBC/Topics/General/Congress'],
266         },
267     }
268
269     def _real_extract(self, url):
270         video_id = self._match_id(url)
271         webpage = self._download_webpage(url, video_id)
272         embed_url = self._html_search_meta('embedURL', webpage)
273         return self.url_result(embed_url)