]>
Commit | Line | Data |
---|---|---|
26a78d4b JMF |
1 | from __future__ import unicode_literals |
2 | ||
3cfeb162 | 3 | import functools |
ecf6de5b | 4 | import re |
5 | ||
29825140 | 6 | from .turner import TurnerBaseIE |
3cfeb162 | 7 | from ..compat import ( |
29f7c58a | 8 | compat_str, |
9 | compat_urllib_parse_unquote, | |
3cfeb162 | 10 | ) |
7bbc6428 | 11 | from ..utils import ( |
29f7c58a | 12 | int_or_none, |
13 | merge_dicts, | |
3cfeb162 | 14 | OnDemandPagedList, |
29f7c58a | 15 | parse_duration, |
16 | parse_iso8601, | |
4dfbf869 | 17 | parse_qs, |
29f7c58a | 18 | try_get, |
19 | update_url_query, | |
20 | urljoin, | |
7bbc6428 | 21 | ) |
5b286728 PH |
22 | |
23 | ||
29f7c58a | 24 | class NBACVPBaseIE(TurnerBaseIE): |
25 | def _extract_nba_cvp_info(self, path, video_id, fatal=False): | |
26 | return self._extract_cvp_info( | |
27 | 'http://secure.nba.com/%s' % path, video_id, { | |
28 | 'default': { | |
29 | 'media_src': 'http://nba.cdn.turner.com/nba/big', | |
30 | }, | |
31 | 'm3u8': { | |
32 | 'media_src': 'http://nbavod-f.akamaihd.net', | |
33 | }, | |
34 | }, fatal=fatal) | |
35 | ||
36 | ||
37 | class NBAWatchBaseIE(NBACVPBaseIE): | |
38 | _VALID_URL_BASE = r'https?://(?:(?:www\.)?nba\.com(?:/watch)?|watch\.nba\.com)/' | |
39 | ||
40 | def _extract_video(self, filter_key, filter_value): | |
41 | video = self._download_json( | |
42 | 'https://neulionscnbav2-a.akamaihd.net/solr/nbad_program/usersearch', | |
43 | filter_value, query={ | |
44 | 'fl': 'description,image,name,pid,releaseDate,runtime,tags,seoName', | |
45 | 'q': filter_key + ':' + filter_value, | |
46 | 'wt': 'json', | |
47 | })['response']['docs'][0] | |
48 | ||
49 | video_id = str(video['pid']) | |
50 | title = video['name'] | |
51 | ||
52 | formats = [] | |
53 | m3u8_url = (self._download_json( | |
54 | 'https://watch.nba.com/service/publishpoint', video_id, query={ | |
55 | 'type': 'video', | |
56 | 'format': 'json', | |
57 | 'id': video_id, | |
58 | }, headers={ | |
59 | 'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0_1 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A402 Safari/604.1', | |
60 | }, fatal=False) or {}).get('path') | |
61 | if m3u8_url: | |
62 | m3u8_formats = self._extract_m3u8_formats( | |
63 | re.sub(r'_(?:pc|iphone)\.', '.', m3u8_url), video_id, 'mp4', | |
64 | 'm3u8_native', m3u8_id='hls', fatal=False) | |
65 | formats.extend(m3u8_formats) | |
66 | for f in m3u8_formats: | |
67 | http_f = f.copy() | |
68 | http_f.update({ | |
69 | 'format_id': http_f['format_id'].replace('hls-', 'http-'), | |
70 | 'protocol': 'http', | |
71 | 'url': http_f['url'].replace('.m3u8', ''), | |
72 | }) | |
73 | formats.append(http_f) | |
74 | ||
75 | info = { | |
76 | 'id': video_id, | |
77 | 'title': title, | |
78 | 'thumbnail': urljoin('https://nbadsdmt.akamaized.net/media/nba/nba/thumbs/', video.get('image')), | |
79 | 'description': video.get('description'), | |
80 | 'duration': int_or_none(video.get('runtime')), | |
81 | 'timestamp': parse_iso8601(video.get('releaseDate')), | |
82 | 'tags': video.get('tags'), | |
83 | } | |
84 | ||
85 | seo_name = video.get('seoName') | |
86 | if seo_name and re.search(r'\d{4}/\d{2}/\d{2}/', seo_name): | |
87 | base_path = '' | |
88 | if seo_name.startswith('teams/'): | |
89 | base_path += seo_name.split('/')[1] + '/' | |
90 | base_path += 'video/' | |
91 | cvp_info = self._extract_nba_cvp_info( | |
92 | base_path + seo_name + '.xml', video_id, False) | |
93 | if cvp_info: | |
94 | formats.extend(cvp_info['formats']) | |
95 | info = merge_dicts(info, cvp_info) | |
96 | ||
97 | self._sort_formats(formats) | |
98 | info['formats'] = formats | |
99 | return info | |
100 | ||
101 | ||
102 | class NBAWatchEmbedIE(NBAWatchBaseIE): | |
103 | IENAME = 'nba:watch:embed' | |
104 | _VALID_URL = NBAWatchBaseIE._VALID_URL_BASE + r'embed\?.*?\bid=(?P<id>\d+)' | |
105 | _TESTS = [{ | |
106 | 'url': 'http://watch.nba.com/embed?id=659395', | |
107 | 'md5': 'b7e3f9946595f4ca0a13903ce5edd120', | |
108 | 'info_dict': { | |
109 | 'id': '659395', | |
110 | 'ext': 'mp4', | |
111 | 'title': 'Mix clip: More than 7 points of Joe Ingles, Luc Mbah a Moute, Blake Griffin and 6 more in Utah Jazz vs. the Clippers, 4/15/2017', | |
112 | 'description': 'Mix clip: More than 7 points of Joe Ingles, Luc Mbah a Moute, Blake Griffin and 6 more in Utah Jazz vs. the Clippers, 4/15/2017', | |
113 | 'timestamp': 1492228800, | |
114 | 'upload_date': '20170415', | |
115 | }, | |
116 | }] | |
117 | ||
118 | def _real_extract(self, url): | |
119 | video_id = self._match_id(url) | |
120 | return self._extract_video('pid', video_id) | |
121 | ||
122 | ||
123 | class NBAWatchIE(NBAWatchBaseIE): | |
124 | IE_NAME = 'nba:watch' | |
125 | _VALID_URL = NBAWatchBaseIE._VALID_URL_BASE + r'(?:nba/)?video/(?P<id>.+?(?=/index\.html)|(?:[^/]+/)*[^/?#&]+)' | |
6a3e0103 | 126 | _TESTS = [{ |
26a78d4b | 127 | 'url': 'http://www.nba.com/video/games/nets/2012/12/04/0021200253-okc-bkn-recap.nba/index.html', |
29f7c58a | 128 | 'md5': '9d902940d2a127af3f7f9d2f3dc79c96', |
26a78d4b | 129 | 'info_dict': { |
29f7c58a | 130 | 'id': '70946', |
db9b1dbc | 131 | 'ext': 'mp4', |
26a78d4b | 132 | 'title': 'Thunder vs. Nets', |
7bbc6428 S |
133 | 'description': 'Kevin Durant scores 32 points and dishes out six assists as the Thunder beat the Nets in Brooklyn.', |
134 | 'duration': 181, | |
29f7c58a | 135 | 'timestamp': 1354597200, |
c233e6bc | 136 | 'upload_date': '20121204', |
26a78d4b | 137 | }, |
6a3e0103 PH |
138 | }, { |
139 | 'url': 'http://www.nba.com/video/games/hornets/2014/12/05/0021400276-nyk-cha-play5.nba/', | |
140 | 'only_matching': True, | |
46cc1c65 | 141 | }, { |
6a11bb77 | 142 | 'url': 'http://watch.nba.com/video/channels/playoffs/2015/05/20/0041400301-cle-atl-recap.nba', |
8fc226ef | 143 | 'md5': 'b2b39b81cf28615ae0c3360a3f9668c4', |
8a278a1d | 144 | 'info_dict': { |
29f7c58a | 145 | 'id': '330865', |
8a278a1d | 146 | 'ext': 'mp4', |
8fc226ef | 147 | 'title': 'Hawks vs. Cavaliers Game 1', |
8a278a1d YCH |
148 | 'description': 'md5:8094c3498d35a9bd6b1a8c396a071b4d', |
149 | 'duration': 228, | |
29f7c58a | 150 | 'timestamp': 1432094400, |
151 | 'upload_date': '20150521', | |
3c77a54d | 152 | }, |
86a7dbe6 | 153 | }, { |
29f7c58a | 154 | 'url': 'http://watch.nba.com/nba/video/channels/nba_tv/2015/06/11/YT_go_big_go_home_Game4_061115', |
155 | 'only_matching': True, | |
3cfeb162 | 156 | }, { |
29f7c58a | 157 | # only CVP mp4 format available |
158 | 'url': 'https://watch.nba.com/video/teams/cavaliers/2012/10/15/sloan121015mov-2249106', | |
159 | 'only_matching': True, | |
3cfeb162 | 160 | }, { |
29f7c58a | 161 | 'url': 'https://watch.nba.com/video/top-100-dunks-from-the-2019-20-season?plsrc=nba&collection=2019-20-season-highlights', |
162 | 'only_matching': True, | |
163 | }] | |
164 | ||
165 | def _real_extract(self, url): | |
166 | display_id = self._match_id(url) | |
4dfbf869 | 167 | collection_id = parse_qs(url).get('collection', [None])[0] |
f40ee5e9 | 168 | if self._yes_playlist(collection_id, display_id): |
169 | return self.url_result( | |
170 | 'https://www.nba.com/watch/list/collection/' + collection_id, | |
171 | NBAWatchCollectionIE.ie_key(), collection_id) | |
29f7c58a | 172 | return self._extract_video('seoName', display_id) |
173 | ||
174 | ||
175 | class NBAWatchCollectionIE(NBAWatchBaseIE): | |
176 | IE_NAME = 'nba:watch:collection' | |
177 | _VALID_URL = NBAWatchBaseIE._VALID_URL_BASE + r'list/collection/(?P<id>[^/?#&]+)' | |
178 | _TESTS = [{ | |
179 | 'url': 'https://watch.nba.com/list/collection/season-preview-2020', | |
3cfeb162 | 180 | 'info_dict': { |
29f7c58a | 181 | 'id': 'season-preview-2020', |
3cfeb162 | 182 | }, |
29f7c58a | 183 | 'playlist_mincount': 43, |
6a3e0103 | 184 | }] |
29f7c58a | 185 | _PAGE_SIZE = 100 |
5b286728 | 186 | |
29f7c58a | 187 | def _fetch_page(self, collection_id, page): |
188 | page += 1 | |
189 | videos = self._download_json( | |
190 | 'https://content-api-prod.nba.com/public/1/endeavor/video-list/collection/' + collection_id, | |
191 | collection_id, 'Downloading page %d JSON metadata' % page, query={ | |
192 | 'count': self._PAGE_SIZE, | |
193 | 'page': page, | |
194 | })['results']['videos'] | |
195 | for video in videos: | |
196 | program = video.get('program') or {} | |
197 | seo_name = program.get('seoName') or program.get('slug') | |
198 | if not seo_name: | |
199 | continue | |
200 | yield { | |
201 | '_type': 'url', | |
202 | 'id': program.get('id'), | |
203 | 'title': program.get('title') or video.get('title'), | |
204 | 'url': 'https://www.nba.com/watch/video/' + seo_name, | |
205 | 'thumbnail': video.get('image'), | |
206 | 'description': program.get('description') or video.get('description'), | |
207 | 'duration': parse_duration(program.get('runtimeHours')), | |
208 | 'timestamp': parse_iso8601(video.get('releaseDate')), | |
209 | } | |
3cfeb162 | 210 | |
29f7c58a | 211 | def _real_extract(self, url): |
212 | collection_id = self._match_id(url) | |
3cfeb162 | 213 | entries = OnDemandPagedList( |
29f7c58a | 214 | functools.partial(self._fetch_page, collection_id), |
6be08ce6 | 215 | self._PAGE_SIZE) |
29f7c58a | 216 | return self.playlist_result(entries, collection_id) |
3cfeb162 | 217 | |
3cfeb162 | 218 | |
29f7c58a | 219 | class NBABaseIE(NBACVPBaseIE): |
220 | _VALID_URL_BASE = r'''(?x) | |
221 | https?://(?:www\.)?nba\.com/ | |
222 | (?P<team> | |
223 | blazers| | |
224 | bucks| | |
225 | bulls| | |
226 | cavaliers| | |
227 | celtics| | |
228 | clippers| | |
229 | grizzlies| | |
230 | hawks| | |
231 | heat| | |
232 | hornets| | |
233 | jazz| | |
234 | kings| | |
235 | knicks| | |
236 | lakers| | |
237 | magic| | |
238 | mavericks| | |
239 | nets| | |
240 | nuggets| | |
241 | pacers| | |
242 | pelicans| | |
243 | pistons| | |
244 | raptors| | |
245 | rockets| | |
246 | sixers| | |
247 | spurs| | |
248 | suns| | |
249 | thunder| | |
250 | timberwolves| | |
251 | warriors| | |
252 | wizards | |
253 | ) | |
254 | (?:/play\#)?/''' | |
255 | _CHANNEL_PATH_REGEX = r'video/channel|series' | |
86a7dbe6 | 256 | |
29f7c58a | 257 | def _embed_url_result(self, team, content_id): |
258 | return self.url_result(update_url_query( | |
259 | 'https://secure.nba.com/assets/amp/include/video/iframe.html', { | |
260 | 'contentId': content_id, | |
261 | 'team': team, | |
262 | }), NBAEmbedIE.ie_key()) | |
3cfeb162 | 263 | |
29f7c58a | 264 | def _call_api(self, team, content_id, query, resource): |
265 | return self._download_json( | |
266 | 'https://api.nba.net/2/%s/video,imported_video,wsc/' % team, | |
267 | content_id, 'Download %s JSON metadata' % resource, | |
268 | query=query, headers={ | |
269 | 'accessToken': 'internal|bb88df6b4c2244e78822812cecf1ee1b', | |
270 | })['response']['result'] | |
3cfeb162 | 271 | |
29f7c58a | 272 | def _extract_video(self, video, team, extract_all=True): |
273 | video_id = compat_str(video['nid']) | |
274 | team = video['brand'] | |
86a7dbe6 | 275 | |
29f7c58a | 276 | info = { |
277 | 'id': video_id, | |
278 | 'title': video.get('title') or video.get('headline') or video['shortHeadline'], | |
279 | 'description': video.get('description'), | |
280 | 'timestamp': parse_iso8601(video.get('published')), | |
281 | } | |
282 | ||
283 | subtitles = {} | |
284 | captions = try_get(video, lambda x: x['videoCaptions']['sidecars'], dict) or {} | |
285 | for caption_url in captions.values(): | |
286 | subtitles.setdefault('en', []).append({'url': caption_url}) | |
287 | ||
288 | formats = [] | |
289 | mp4_url = video.get('mp4') | |
290 | if mp4_url: | |
291 | formats.append({ | |
292 | 'url': mp4_url, | |
c233e6bc | 293 | }) |
29f7c58a | 294 | |
295 | if extract_all: | |
296 | source_url = video.get('videoSource') | |
297 | if source_url and not source_url.startswith('s3://') and self._is_valid_url(source_url, video_id, 'source'): | |
298 | formats.append({ | |
299 | 'format_id': 'source', | |
300 | 'url': source_url, | |
f983b875 | 301 | 'quality': 1, |
29f7c58a | 302 | }) |
303 | ||
304 | m3u8_url = video.get('m3u8') | |
305 | if m3u8_url: | |
306 | if '.akamaihd.net/i/' in m3u8_url: | |
307 | formats.extend(self._extract_akamai_formats( | |
308 | m3u8_url, video_id, {'http': 'pmd.cdn.turner.com'})) | |
309 | else: | |
310 | formats.extend(self._extract_m3u8_formats( | |
311 | m3u8_url, video_id, 'mp4', | |
312 | 'm3u8_native', m3u8_id='hls', fatal=False)) | |
313 | ||
314 | content_xml = video.get('contentXml') | |
315 | if team and content_xml: | |
316 | cvp_info = self._extract_nba_cvp_info( | |
317 | team + content_xml, video_id, fatal=False) | |
318 | if cvp_info: | |
319 | formats.extend(cvp_info['formats']) | |
320 | subtitles = self._merge_subtitles(subtitles, cvp_info['subtitles']) | |
321 | info = merge_dicts(info, cvp_info) | |
322 | ||
323 | self._sort_formats(formats) | |
324 | else: | |
325 | info.update(self._embed_url_result(team, video['videoId'])) | |
326 | ||
327 | info.update({ | |
328 | 'formats': formats, | |
329 | 'subtitles': subtitles, | |
330 | }) | |
331 | ||
332 | return info | |
333 | ||
334 | def _real_extract(self, url): | |
5ad28e7f | 335 | team, display_id = self._match_valid_url(url).groups() |
29f7c58a | 336 | if '/play#/' in url: |
337 | display_id = compat_urllib_parse_unquote(display_id) | |
338 | else: | |
339 | webpage = self._download_webpage(url, display_id) | |
340 | display_id = self._search_regex( | |
341 | self._CONTENT_ID_REGEX + r'\s*:\s*"([^"]+)"', webpage, 'video id') | |
342 | return self._extract_url_results(team, display_id) | |
343 | ||
344 | ||
345 | class NBAEmbedIE(NBABaseIE): | |
346 | IENAME = 'nba:embed' | |
347 | _VALID_URL = r'https?://secure\.nba\.com/assets/amp/include/video/(?:topI|i)frame\.html\?.*?\bcontentId=(?P<id>[^?#&]+)' | |
348 | _TESTS = [{ | |
349 | 'url': 'https://secure.nba.com/assets/amp/include/video/topIframe.html?contentId=teams/bulls/2020/12/04/3478774/1607105587854-20201204_SCHEDULE_RELEASE_FINAL_DRUPAL-3478774&team=bulls&adFree=false&profile=71&videoPlayerName=TAMPCVP&baseUrl=&videoAdsection=nba.com_mobile_web_teamsites_chicagobulls&Env=', | |
350 | 'only_matching': True, | |
351 | }, { | |
352 | 'url': 'https://secure.nba.com/assets/amp/include/video/iframe.html?contentId=2016/10/29/0021600027boschaplay7&adFree=false&profile=71&team=&videoPlayerName=LAMPCVP', | |
353 | 'only_matching': True, | |
354 | }] | |
355 | ||
356 | def _real_extract(self, url): | |
4dfbf869 | 357 | qs = parse_qs(url) |
29f7c58a | 358 | content_id = qs['contentId'][0] |
359 | team = qs.get('team', [None])[0] | |
360 | if not team: | |
361 | return self.url_result( | |
362 | 'https://watch.nba.com/video/' + content_id, NBAWatchIE.ie_key()) | |
363 | video = self._call_api(team, content_id, {'videoid': content_id}, 'video')[0] | |
364 | return self._extract_video(video, team) | |
365 | ||
366 | ||
367 | class NBAIE(NBABaseIE): | |
368 | IENAME = 'nba' | |
369 | _VALID_URL = NBABaseIE._VALID_URL_BASE + '(?!%s)video/(?P<id>(?:[^/]+/)*[^/?#&]+)' % NBABaseIE._CHANNEL_PATH_REGEX | |
370 | _TESTS = [{ | |
371 | 'url': 'https://www.nba.com/bulls/video/teams/bulls/2020/12/04/3478774/1607105587854-20201204schedulereleasefinaldrupal-3478774', | |
372 | 'info_dict': { | |
373 | 'id': '45039', | |
374 | 'ext': 'mp4', | |
375 | 'title': 'AND WE BACK.', | |
376 | 'description': 'Part 1 of our 2020-21 schedule is here! Watch our games on NBC Sports Chicago.', | |
377 | 'duration': 94, | |
378 | 'timestamp': 1607112000, | |
379 | 'upload_date': '20201218', | |
380 | }, | |
381 | }, { | |
382 | 'url': 'https://www.nba.com/bucks/play#/video/teams%2Fbucks%2F2020%2F12%2F17%2F64860%2F1608252863446-Op_Dream_16x9-64860', | |
383 | 'only_matching': True, | |
384 | }, { | |
385 | 'url': 'https://www.nba.com/bucks/play#/video/wsc%2Fteams%2F2787C911AA1ACD154B5377F7577CCC7134B2A4B0', | |
386 | 'only_matching': True, | |
387 | }] | |
388 | _CONTENT_ID_REGEX = r'videoID' | |
389 | ||
390 | def _extract_url_results(self, team, content_id): | |
391 | return self._embed_url_result(team, content_id) | |
392 | ||
393 | ||
394 | class NBAChannelIE(NBABaseIE): | |
395 | IENAME = 'nba:channel' | |
396 | _VALID_URL = NBABaseIE._VALID_URL_BASE + '(?:%s)/(?P<id>[^/?#&]+)' % NBABaseIE._CHANNEL_PATH_REGEX | |
397 | _TESTS = [{ | |
398 | 'url': 'https://www.nba.com/blazers/video/channel/summer_league', | |
399 | 'info_dict': { | |
400 | 'title': 'Summer League', | |
401 | }, | |
402 | 'playlist_mincount': 138, | |
403 | }, { | |
404 | 'url': 'https://www.nba.com/bucks/play#/series/On%20This%20Date', | |
405 | 'only_matching': True, | |
406 | }] | |
407 | _CONTENT_ID_REGEX = r'videoSubCategory' | |
408 | _PAGE_SIZE = 100 | |
409 | ||
410 | def _fetch_page(self, team, channel, page): | |
411 | results = self._call_api(team, channel, { | |
412 | 'channels': channel, | |
413 | 'count': self._PAGE_SIZE, | |
414 | 'offset': page * self._PAGE_SIZE, | |
415 | }, 'page %d' % (page + 1)) | |
416 | for video in results: | |
417 | yield self._extract_video(video, team, False) | |
418 | ||
419 | def _extract_url_results(self, team, content_id): | |
420 | entries = OnDemandPagedList( | |
421 | functools.partial(self._fetch_page, team, content_id), | |
422 | self._PAGE_SIZE) | |
423 | return self.playlist_result(entries, playlist_title=content_id) |