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