]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/nba.py
[openload] Update algorithm again (#10408)
[yt-dlp.git] / youtube_dl / extractor / nba.py
CommitLineData
26a78d4b
JMF
1from __future__ import unicode_literals
2
3cfeb162 3import functools
86a7dbe6 4import os.path
ecf6de5b 5import re
6
5b286728 7from .common import InfoExtractor
3cfeb162 8from ..compat import (
15707c7e 9 compat_urllib_parse_urlencode,
3cfeb162
YCH
10 compat_urlparse,
11)
7bbc6428 12from ..utils import (
8fc226ef 13 int_or_none,
3cfeb162 14 OnDemandPagedList,
86a7dbe6
YCH
15 parse_duration,
16 remove_start,
46cc1c65 17 xpath_text,
18 xpath_attr,
7bbc6428 19)
5b286728
PH
20
21
c233e6bc 22class NBAIE(InfoExtractor):
86a7dbe6 23 _VALID_URL = r'https?://(?:watch\.|www\.)?nba\.com/(?P<path>(?:[^/]+/)+(?P<id>[^?]*?))/?(?:/index\.html)?(?:\?.*)?$'
6a3e0103 24 _TESTS = [{
26a78d4b 25 'url': 'http://www.nba.com/video/games/nets/2012/12/04/0021200253-okc-bkn-recap.nba/index.html',
6a11bb77 26 'md5': '9e7729d3010a9c71506fd1248f74e4f4',
26a78d4b 27 'info_dict': {
c233e6bc 28 'id': '0021200253-okc-bkn-recap',
db9b1dbc 29 'ext': 'mp4',
26a78d4b 30 'title': 'Thunder vs. Nets',
7bbc6428
S
31 'description': 'Kevin Durant scores 32 points and dishes out six assists as the Thunder beat the Nets in Brooklyn.',
32 'duration': 181,
c233e6bc 33 'timestamp': 1354638466,
34 'upload_date': '20121204',
26a78d4b 35 },
db9b1dbc
YCH
36 'params': {
37 # m3u8 download
38 'skip_download': True,
39 },
6a3e0103
PH
40 }, {
41 'url': 'http://www.nba.com/video/games/hornets/2014/12/05/0021400276-nyk-cha-play5.nba/',
42 'only_matching': True,
46cc1c65 43 }, {
6a11bb77 44 'url': 'http://watch.nba.com/video/channels/playoffs/2015/05/20/0041400301-cle-atl-recap.nba',
8fc226ef 45 'md5': 'b2b39b81cf28615ae0c3360a3f9668c4',
8a278a1d 46 'info_dict': {
c233e6bc 47 'id': '0041400301-cle-atl-recap',
8a278a1d 48 'ext': 'mp4',
8fc226ef 49 'title': 'Hawks vs. Cavaliers Game 1',
8a278a1d
YCH
50 'description': 'md5:8094c3498d35a9bd6b1a8c396a071b4d',
51 'duration': 228,
c233e6bc 52 'timestamp': 1432134543,
8fc226ef 53 'upload_date': '20150520',
8a278a1d 54 }
86a7dbe6
YCH
55 }, {
56 'url': 'http://www.nba.com/clippers/news/doc-rivers-were-not-trading-blake',
57 'info_dict': {
58 'id': '1455672027478-Doc_Feb16_720',
59 'ext': 'mp4',
60 'title': 'Practice: Doc Rivers - 2/16/16',
61 'description': 'Head Coach Doc Rivers addresses the media following practice.',
62 'upload_date': '20160217',
63 'timestamp': 1455672000,
64 },
65 'params': {
66 # m3u8 download
67 'skip_download': True,
68 },
3cfeb162
YCH
69 }, {
70 'url': 'http://www.nba.com/timberwolves/wiggins-shootaround#',
71 'info_dict': {
72 'id': 'timberwolves',
73 'title': 'Shootaround Access - Dec. 12 | Andrew Wiggins',
74 },
75 'playlist_count': 30,
76 'params': {
77 # Download the whole playlist takes too long time
78 'playlist_items': '1-30',
79 },
80 }, {
81 'url': 'http://www.nba.com/timberwolves/wiggins-shootaround#',
82 'info_dict': {
83 'id': 'Wigginsmp4',
84 'ext': 'mp4',
85 'title': 'Shootaround Access - Dec. 12 | Andrew Wiggins',
86 'description': 'Wolves rookie Andrew Wiggins addresses the media after Friday\'s shootaround.',
87 'upload_date': '20141212',
88 'timestamp': 1418418600,
89 },
90 'params': {
91 'noplaylist': True,
92 # m3u8 download
93 'skip_download': True,
94 },
6a3e0103 95 }]
5b286728 96
3cfeb162
YCH
97 _PAGE_SIZE = 30
98
99 def _fetch_page(self, team, video_id, page):
15707c7e 100 search_url = 'http://searchapp2.nba.com/nba-search/query.jsp?' + compat_urllib_parse_urlencode({
3cfeb162
YCH
101 'type': 'teamvideo',
102 'start': page * self._PAGE_SIZE + 1,
103 'npp': (page + 1) * self._PAGE_SIZE + 1,
104 'sort': 'recent',
105 'output': 'json',
106 'site': team,
107 })
108 results = self._download_json(
109 search_url, video_id, note='Download page %d of playlist data' % page)['results'][0]
110 for item in results:
111 yield self.url_result(compat_urlparse.urljoin('http://www.nba.com/', item['url']))
112
113 def _extract_playlist(self, orig_path, video_id, webpage):
114 team = orig_path.split('/')[0]
115
116 if self._downloader.params.get('noplaylist'):
117 self.to_screen('Downloading just video because of --no-playlist')
118 video_path = self._search_regex(
119 r'nbaVideoCore\.firstVideo\s*=\s*\'([^\']+)\';', webpage, 'video path')
120 video_url = 'http://www.nba.com/%s/video/%s' % (team, video_path)
121 return self.url_result(video_url)
122
123 self.to_screen('Downloading playlist - add --no-playlist to just download video')
124 playlist_title = self._og_search_title(webpage, fatal=False)
125 entries = OnDemandPagedList(
126 functools.partial(self._fetch_page, team, video_id),
127 self._PAGE_SIZE, use_cache=True)
128
129 return self.playlist_result(entries, team, playlist_title)
130
c233e6bc 131 def _real_extract(self, url):
6a11bb77 132 path, video_id = re.match(self._VALID_URL, url).groups()
3cfeb162 133 orig_path = path
b80d4beb 134 if path.startswith('nba/'):
135 path = path[3:]
86a7dbe6
YCH
136
137 if 'video/' not in path:
138 webpage = self._download_webpage(url, video_id)
139 path = remove_start(self._search_regex(r'data-videoid="([^"]+)"', webpage, 'video id'), '/')
3cfeb162
YCH
140
141 if path == '{{id}}':
142 return self._extract_playlist(orig_path, video_id, webpage)
143
86a7dbe6
YCH
144 # See prepareContentId() of pkgCvp.js
145 if path.startswith('video/teams'):
146 path = 'video/channels/proxy/' + path[6:]
147
6a11bb77 148 video_info = self._download_xml('http://www.nba.com/%s.xml' % path, video_id)
86a7dbe6 149 video_id = os.path.splitext(xpath_text(video_info, 'slug'))[0]
46cc1c65 150 title = xpath_text(video_info, 'headline')
151 description = xpath_text(video_info, 'description')
152 duration = parse_duration(xpath_text(video_info, 'length'))
153 timestamp = int_or_none(xpath_attr(video_info, 'dateCreated', 'uts'))
c233e6bc 154
155 thumbnails = []
156 for image in video_info.find('images'):
157 thumbnails.append({
158 'id': image.attrib.get('cut'),
159 'url': image.text,
160 'width': int_or_none(image.attrib.get('width')),
161 'height': int_or_none(image.attrib.get('height')),
162 })
163
164 formats = []
b80d4beb 165 for video_file in video_info.findall('.//file'):
c233e6bc 166 video_url = video_file.text
139f2782 167 if video_url.startswith('/'):
168 continue
c233e6bc 169 if video_url.endswith('.m3u8'):
db9b1dbc 170 formats.extend(self._extract_m3u8_formats(video_url, video_id, ext='mp4', m3u8_id='hls', fatal=False))
c233e6bc 171 elif video_url.endswith('.f4m'):
7e5edcfd 172 formats.extend(self._extract_f4m_formats(video_url + '?hdcore=3.4.1.1', video_id, f4m_id='hds', fatal=False))
c233e6bc 173 else:
174 key = video_file.attrib.get('bitrate')
cb160dd5 175 format_info = {
c233e6bc 176 'format_id': key,
177 'url': video_url,
cb160dd5 178 }
179 mobj = re.search(r'(\d+)x(\d+)(?:_(\d+))?', key)
180 if mobj:
181 format_info.update({
0017486d 182 'width': int(mobj.group(1)),
183 'height': int(mobj.group(2)),
51c4fec0 184 'tbr': int_or_none(mobj.group(3)),
cb160dd5 185 })
186 formats.append(format_info)
c233e6bc 187 self._sort_formats(formats)
188
26a78d4b 189 return {
c233e6bc 190 'id': video_id,
191 'title': title,
192 'description': description,
193 'duration': duration,
194 'timestamp': timestamp,
195 'thumbnails': thumbnails,
196 'formats': formats,
5b286728 197 }