1 from __future__ 
import unicode_literals
 
   7 from .common 
import InfoExtractor
 
   9     compat_urllib_parse_urlencode
, 
  22 class NBAIE(InfoExtractor
): 
  23     _VALID_URL 
= r
'https?://(?:watch\.|www\.)?nba\.com/(?P<path>(?:[^/]+/)+(?P<id>[^?]*?))/?(?:/index\.html)?(?:\?.*)?$' 
  25         'url': 'http://www.nba.com/video/games/nets/2012/12/04/0021200253-okc-bkn-recap.nba/index.html', 
  26         'md5': '9e7729d3010a9c71506fd1248f74e4f4', 
  28             'id': '0021200253-okc-bkn-recap', 
  30             'title': 'Thunder vs. Nets', 
  31             'description': 'Kevin Durant scores 32 points and dishes out six assists as the Thunder beat the Nets in Brooklyn.', 
  33             'timestamp': 1354638466, 
  34             'upload_date': '20121204', 
  38             'skip_download': True, 
  41         'url': 'http://www.nba.com/video/games/hornets/2014/12/05/0021400276-nyk-cha-play5.nba/', 
  42         'only_matching': True, 
  44         'url': 'http://watch.nba.com/video/channels/playoffs/2015/05/20/0041400301-cle-atl-recap.nba', 
  45         'md5': 'b2b39b81cf28615ae0c3360a3f9668c4', 
  47             'id': '0041400301-cle-atl-recap', 
  49             'title': 'Hawks vs. Cavaliers Game 1', 
  50             'description': 'md5:8094c3498d35a9bd6b1a8c396a071b4d', 
  52             'timestamp': 1432134543, 
  53             'upload_date': '20150520', 
  56         'url': 'http://www.nba.com/clippers/news/doc-rivers-were-not-trading-blake', 
  58             'id': '1455672027478-Doc_Feb16_720', 
  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, 
  67             'skip_download': True, 
  70         'url': 'http://www.nba.com/timberwolves/wiggins-shootaround#', 
  73             'title': 'Shootaround Access - Dec. 12 | Andrew Wiggins', 
  77             # Download the whole playlist takes too long time 
  78             'playlist_items': '1-30', 
  81         'url': 'http://www.nba.com/timberwolves/wiggins-shootaround#', 
  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, 
  93             'skip_download': True, 
  99     def _fetch_page(self
, team
, video_id
, page
): 
 100         search_url 
= 'http://searchapp2.nba.com/nba-search/query.jsp?' + compat_urllib_parse_urlencode({ 
 102             'start': page 
* self
._PAGE
_SIZE 
+ 1, 
 103             'npp': (page 
+ 1) * self
._PAGE
_SIZE 
+ 1, 
 108         results 
= self
._download
_json
( 
 109             search_url
, video_id
, note
='Download page %d of playlist data' % page
)['results'][0] 
 111             yield self
.url_result(compat_urlparse
.urljoin('http://www.nba.com/', item
['url'])) 
 113     def _extract_playlist(self
, orig_path
, video_id
, webpage
): 
 114         team 
= orig_path
.split('/')[0] 
 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) 
 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) 
 129         return self.playlist_result(entries, team, playlist_title) 
 131     def _real_extract(self, url): 
 132         path, video_id = re.match(self._VALID_URL, url).groups() 
 134         if path.startswith('nba
/'): 
 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'), '/') 
 142                 return self._extract_playlist(orig_path, video_id, webpage) 
 144             # See prepareContentId() of pkgCvp.js 
 145             if path.startswith('video/teams'): 
 146                 path = 'video/channels/proxy/' + path[6:] 
 148         video_info = self._download_xml('http://www.nba.com/%s.xml' % path, video_id) 
 149         video_id = os.path.splitext(xpath_text(video_info, 'slug'))[0] 
 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')) 
 156         for image in video_info.find('images'): 
 158                 'id': image.attrib.get('cut'), 
 160                 'width': int_or_none(image.attrib.get('width')), 
 161                 'height': int_or_none(image.attrib.get('height')), 
 165         for video_file in video_info.findall('.//file'): 
 166             video_url = video_file.text 
 167             if video_url.startswith('/'): 
 169             if video_url.endswith('.m3u8'): 
 170                 formats.extend(self._extract_m3u8_formats(video_url, video_id, ext='mp4', m3u8_id='hls', fatal=False)) 
 171             elif video_url.endswith('.f4m'): 
 172                 formats.extend(self._extract_f4m_formats(video_url + '?hdcore=3.4.1.1', video_id, f4m_id='hds', fatal=False)) 
 174                 key = video_file.attrib.get('bitrate') 
 179                 mobj = re.search(r'(\d+)x(\d+)(?:_(\d+))?', key) 
 182                         'width': int(mobj.group(1)), 
 183                         'height': int(mobj.group(2)), 
 184                         'tbr': int_or_none(mobj.group(3)), 
 186                 formats.append(format_info) 
 187         self._sort_formats(formats) 
 192             'description': description, 
 193             'duration': duration, 
 194             'timestamp': timestamp, 
 195             'thumbnails': thumbnails,