]> jfr.im git - yt-dlp.git/blob - youtube_dl/extractor/nbc.py
[NBC] Enhance extraction of ThePlatform URL (fixes #5470)
[yt-dlp.git] / 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 )
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 # md5 checksum is not stable
23 'info_dict': {
24 'id': 'c9xnCo0YPOPH',
25 'ext': 'flv',
26 'title': 'Jimmy Fallon Surprises Fans at Ben & Jerry\'s',
27 'description': 'Jimmy gives out free scoops of his new "Tonight Dough" ice cream flavor by surprising customers at the Ben & Jerry\'s scoop shop.',
28 },
29 },
30 {
31 'url': 'http://www.nbc.com/the-tonight-show/episodes/176',
32 'info_dict': {
33 'id': 'XwU9KZkp98TH',
34 'ext': 'flv',
35 'title': 'Ricky Gervais, Steven Van Zandt, ILoveMakonnen',
36 'description': 'A brand new episode of The Tonight Show welcomes Ricky Gervais, Steven Van Zandt and ILoveMakonnen.',
37 },
38 'skip': 'Only works from US',
39 },
40 {
41 'url': 'http://www.nbc.com/saturday-night-live/video/star-wars-teaser/2832821',
42 'info_dict': {
43 'id': '8iUuyzWDdYUZ',
44 'ext': 'flv',
45 'title': 'Star Wars Teaser',
46 'description': 'md5:0b40f9cbde5b671a7ff62fceccc4f442',
47 },
48 'skip': 'Only works from US',
49 }
50 ]
51
52 def _real_extract(self, url):
53 video_id = self._match_id(url)
54 webpage = self._download_webpage(url, video_id)
55 theplatform_url = self._search_regex(
56 [
57 r'(?:class="video-player video-player-full" data-mpx-url|class="player" src)="(.*?)"',
58 r'"embedURL"\s*:\s*"([^"]+)"'
59 ],
60 webpage, 'theplatform url').replace('_no_endcard', '')
61 if theplatform_url.startswith('//'):
62 theplatform_url = 'http:' + theplatform_url
63 return self.url_result(theplatform_url)
64
65
66 class NBCSportsVPlayerIE(InfoExtractor):
67 _VALID_URL = r'https?://vplayer\.nbcsports\.com/(?:[^/]+/)+(?P<id>[0-9a-zA-Z_]+)'
68
69 _TESTS = [{
70 'url': 'https://vplayer.nbcsports.com/p/BxmELC/nbcsports_share/select/9CsDKds0kvHI',
71 'info_dict': {
72 'id': '9CsDKds0kvHI',
73 'ext': 'flv',
74 'description': 'md5:df390f70a9ba7c95ff1daace988f0d8d',
75 'title': 'Tyler Kalinoski hits buzzer-beater to lift Davidson',
76 }
77 }, {
78 'url': 'http://vplayer.nbcsports.com/p/BxmELC/nbc_embedshare/select/_hqLjQ95yx8Z',
79 'only_matching': True,
80 }]
81
82 @staticmethod
83 def _extract_url(webpage):
84 iframe_m = re.search(
85 r'<iframe[^>]+src="(?P<url>https?://vplayer\.nbcsports\.com/[^"]+)"', webpage)
86 if iframe_m:
87 return iframe_m.group('url')
88
89 def _real_extract(self, url):
90 video_id = self._match_id(url)
91 webpage = self._download_webpage(url, video_id)
92 theplatform_url = self._og_search_video_url(webpage)
93 return self.url_result(theplatform_url, 'ThePlatform')
94
95
96 class NBCSportsIE(InfoExtractor):
97 # Does not include https becuase its certificate is invalid
98 _VALID_URL = r'http://www\.nbcsports\.com//?(?:[^/]+/)+(?P<id>[0-9a-z-]+)'
99
100 _TEST = {
101 'url': 'http://www.nbcsports.com//college-basketball/ncaab/tom-izzo-michigan-st-has-so-much-respect-duke',
102 'info_dict': {
103 'id': 'PHJSaFWbrTY9',
104 'ext': 'flv',
105 'title': 'Tom Izzo, Michigan St. has \'so much respect\' for Duke',
106 'description': 'md5:ecb459c9d59e0766ac9c7d5d0eda8113',
107 }
108 }
109
110 def _real_extract(self, url):
111 video_id = self._match_id(url)
112 webpage = self._download_webpage(url, video_id)
113 return self.url_result(
114 NBCSportsVPlayerIE._extract_url(webpage), 'NBCSportsVPlayer')
115
116
117 class NBCNewsIE(InfoExtractor):
118 _VALID_URL = r'''(?x)https?://(?:www\.)?nbcnews\.com/
119 (?:video/.+?/(?P<id>\d+)|
120 (?:feature|nightly-news)/[^/]+/(?P<title>.+))
121 '''
122
123 _TESTS = [
124 {
125 'url': 'http://www.nbcnews.com/video/nbc-news/52753292',
126 'md5': '47abaac93c6eaf9ad37ee6c4463a5179',
127 'info_dict': {
128 'id': '52753292',
129 'ext': 'flv',
130 'title': 'Crew emerges after four-month Mars food study',
131 'description': 'md5:24e632ffac72b35f8b67a12d1b6ddfc1',
132 },
133 },
134 {
135 'url': 'http://www.nbcnews.com/feature/edward-snowden-interview/how-twitter-reacted-snowden-interview-n117236',
136 'md5': 'b2421750c9f260783721d898f4c42063',
137 'info_dict': {
138 'id': 'I1wpAI_zmhsQ',
139 'ext': 'mp4',
140 'title': 'How Twitter Reacted To The Snowden Interview',
141 'description': 'md5:65a0bd5d76fe114f3c2727aa3a81fe64',
142 },
143 'add_ie': ['ThePlatform'],
144 },
145 {
146 'url': 'http://www.nbcnews.com/feature/dateline-full-episodes/full-episode-family-business-n285156',
147 'md5': 'fdbf39ab73a72df5896b6234ff98518a',
148 'info_dict': {
149 'id': 'Wjf9EDR3A_60',
150 'ext': 'mp4',
151 'title': 'FULL EPISODE: Family Business',
152 'description': 'md5:757988edbaae9d7be1d585eb5d55cc04',
153 },
154 },
155 {
156 'url': 'http://www.nbcnews.com/nightly-news/video/nightly-news-with-brian-williams-full-broadcast-february-4-394064451844',
157 'md5': 'b5dda8cddd8650baa0dcb616dd2cf60d',
158 'info_dict': {
159 'id': 'sekXqyTVnmN3',
160 'ext': 'mp4',
161 'title': 'Nightly News with Brian Williams Full Broadcast (February 4)',
162 'description': 'md5:1c10c1eccbe84a26e5debb4381e2d3c5',
163 },
164 },
165 ]
166
167 def _real_extract(self, url):
168 mobj = re.match(self._VALID_URL, url)
169 video_id = mobj.group('id')
170 if video_id is not None:
171 all_info = self._download_xml('http://www.nbcnews.com/id/%s/displaymode/1219' % video_id, video_id)
172 info = all_info.find('video')
173
174 return {
175 'id': video_id,
176 'title': info.find('headline').text,
177 'ext': 'flv',
178 'url': find_xpath_attr(info, 'media', 'type', 'flashVideo').text,
179 'description': compat_str(info.find('caption').text),
180 'thumbnail': find_xpath_attr(info, 'media', 'type', 'thumbnail').text,
181 }
182 else:
183 # "feature" and "nightly-news" pages use theplatform.com
184 title = mobj.group('title')
185 webpage = self._download_webpage(url, title)
186 bootstrap_json = self._search_regex(
187 r'var\s+(?:bootstrapJson|playlistData)\s*=\s*({.+});?\s*$',
188 webpage, 'bootstrap json', flags=re.MULTILINE)
189 bootstrap = self._parse_json(bootstrap_json, video_id)
190 info = bootstrap['results'][0]['video']
191 mpxid = info['mpxId']
192
193 base_urls = [
194 info['fallbackPlaylistUrl'],
195 info['associatedPlaylistUrl'],
196 ]
197
198 for base_url in base_urls:
199 if not base_url:
200 continue
201 playlist_url = base_url + '?form=MPXNBCNewsAPI'
202
203 try:
204 all_videos = self._download_json(playlist_url, title)
205 except ExtractorError as ee:
206 if isinstance(ee.cause, compat_HTTPError):
207 continue
208 raise
209
210 if not all_videos or 'videos' not in all_videos:
211 continue
212
213 try:
214 info = next(v for v in all_videos['videos'] if v['mpxId'] == mpxid)
215 break
216 except StopIteration:
217 continue
218
219 if info is None:
220 raise ExtractorError('Could not find video in playlists')
221
222 return {
223 '_type': 'url',
224 # We get the best quality video
225 'url': info['videoAssets'][-1]['publicUrl'],
226 'ie_key': 'ThePlatform',
227 }