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