]> jfr.im git - yt-dlp.git/blame - youtube_dlc/extractor/nba.py
[skip travis] renaming
[yt-dlp.git] / youtube_dlc / extractor / nba.py
CommitLineData
26a78d4b
JMF
1from __future__ import unicode_literals
2
3cfeb162 3import functools
ecf6de5b 4import re
5
29825140 6from .turner import TurnerBaseIE
3cfeb162 7from ..compat import (
15707c7e 8 compat_urllib_parse_urlencode,
3cfeb162
YCH
9 compat_urlparse,
10)
7bbc6428 11from ..utils import (
3cfeb162 12 OnDemandPagedList,
86a7dbe6 13 remove_start,
7bbc6428 14)
5b286728
PH
15
16
29825140 17class NBAIE(TurnerBaseIE):
86a7dbe6 18 _VALID_URL = r'https?://(?:watch\.|www\.)?nba\.com/(?P<path>(?:[^/]+/)+(?P<id>[^?]*?))/?(?:/index\.html)?(?:\?.*)?$'
6a3e0103 19 _TESTS = [{
26a78d4b 20 'url': 'http://www.nba.com/video/games/nets/2012/12/04/0021200253-okc-bkn-recap.nba/index.html',
6a11bb77 21 'md5': '9e7729d3010a9c71506fd1248f74e4f4',
26a78d4b 22 'info_dict': {
c233e6bc 23 'id': '0021200253-okc-bkn-recap',
db9b1dbc 24 'ext': 'mp4',
26a78d4b 25 'title': 'Thunder vs. Nets',
7bbc6428
S
26 'description': 'Kevin Durant scores 32 points and dishes out six assists as the Thunder beat the Nets in Brooklyn.',
27 'duration': 181,
c233e6bc 28 'timestamp': 1354638466,
29 'upload_date': '20121204',
26a78d4b 30 },
db9b1dbc
YCH
31 'params': {
32 # m3u8 download
33 'skip_download': True,
34 },
6a3e0103
PH
35 }, {
36 'url': 'http://www.nba.com/video/games/hornets/2014/12/05/0021400276-nyk-cha-play5.nba/',
37 'only_matching': True,
46cc1c65 38 }, {
6a11bb77 39 'url': 'http://watch.nba.com/video/channels/playoffs/2015/05/20/0041400301-cle-atl-recap.nba',
8fc226ef 40 'md5': 'b2b39b81cf28615ae0c3360a3f9668c4',
8a278a1d 41 'info_dict': {
3c77a54d 42 'id': 'channels/playoffs/2015/05/20/0041400301-cle-atl-recap.nba',
8a278a1d 43 'ext': 'mp4',
8fc226ef 44 'title': 'Hawks vs. Cavaliers Game 1',
8a278a1d
YCH
45 'description': 'md5:8094c3498d35a9bd6b1a8c396a071b4d',
46 'duration': 228,
c233e6bc 47 'timestamp': 1432134543,
8fc226ef 48 'upload_date': '20150520',
3c77a54d
RA
49 },
50 'expected_warnings': ['Unable to download f4m manifest'],
86a7dbe6
YCH
51 }, {
52 'url': 'http://www.nba.com/clippers/news/doc-rivers-were-not-trading-blake',
53 'info_dict': {
3c77a54d 54 'id': 'teams/clippers/2016/02/17/1455672027478-Doc_Feb16_720.mov-297324',
86a7dbe6
YCH
55 'ext': 'mp4',
56 'title': 'Practice: Doc Rivers - 2/16/16',
57 'description': 'Head Coach Doc Rivers addresses the media following practice.',
29825140 58 'upload_date': '20160216',
86a7dbe6
YCH
59 'timestamp': 1455672000,
60 },
61 'params': {
62 # m3u8 download
63 'skip_download': True,
64 },
3c77a54d 65 'expected_warnings': ['Unable to download f4m manifest'],
3cfeb162
YCH
66 }, {
67 'url': 'http://www.nba.com/timberwolves/wiggins-shootaround#',
68 'info_dict': {
69 'id': 'timberwolves',
70 'title': 'Shootaround Access - Dec. 12 | Andrew Wiggins',
71 },
72 'playlist_count': 30,
73 'params': {
74 # Download the whole playlist takes too long time
75 'playlist_items': '1-30',
76 },
77 }, {
78 'url': 'http://www.nba.com/timberwolves/wiggins-shootaround#',
79 'info_dict': {
3c77a54d 80 'id': 'teams/timberwolves/2014/12/12/Wigginsmp4-3462601',
3cfeb162
YCH
81 'ext': 'mp4',
82 'title': 'Shootaround Access - Dec. 12 | Andrew Wiggins',
83 'description': 'Wolves rookie Andrew Wiggins addresses the media after Friday\'s shootaround.',
84 'upload_date': '20141212',
85 'timestamp': 1418418600,
86 },
87 'params': {
88 'noplaylist': True,
89 # m3u8 download
90 'skip_download': True,
91 },
3c77a54d 92 'expected_warnings': ['Unable to download f4m manifest'],
6a3e0103 93 }]
5b286728 94
3cfeb162
YCH
95 _PAGE_SIZE = 30
96
97 def _fetch_page(self, team, video_id, page):
15707c7e 98 search_url = 'http://searchapp2.nba.com/nba-search/query.jsp?' + compat_urllib_parse_urlencode({
3cfeb162
YCH
99 'type': 'teamvideo',
100 'start': page * self._PAGE_SIZE + 1,
101 'npp': (page + 1) * self._PAGE_SIZE + 1,
102 'sort': 'recent',
103 'output': 'json',
104 'site': team,
105 })
106 results = self._download_json(
107 search_url, video_id, note='Download page %d of playlist data' % page)['results'][0]
108 for item in results:
109 yield self.url_result(compat_urlparse.urljoin('http://www.nba.com/', item['url']))
110
111 def _extract_playlist(self, orig_path, video_id, webpage):
112 team = orig_path.split('/')[0]
113
114 if self._downloader.params.get('noplaylist'):
115 self.to_screen('Downloading just video because of --no-playlist')
116 video_path = self._search_regex(
117 r'nbaVideoCore\.firstVideo\s*=\s*\'([^\']+)\';', webpage, 'video path')
118 video_url = 'http://www.nba.com/%s/video/%s' % (team, video_path)
119 return self.url_result(video_url)
120
121 self.to_screen('Downloading playlist - add --no-playlist to just download video')
122 playlist_title = self._og_search_title(webpage, fatal=False)
123 entries = OnDemandPagedList(
124 functools.partial(self._fetch_page, team, video_id),
6be08ce6 125 self._PAGE_SIZE)
3cfeb162
YCH
126
127 return self.playlist_result(entries, team, playlist_title)
128
c233e6bc 129 def _real_extract(self, url):
6a11bb77 130 path, video_id = re.match(self._VALID_URL, url).groups()
3cfeb162 131 orig_path = path
b80d4beb 132 if path.startswith('nba/'):
133 path = path[3:]
86a7dbe6
YCH
134
135 if 'video/' not in path:
136 webpage = self._download_webpage(url, video_id)
137 path = remove_start(self._search_regex(r'data-videoid="([^"]+)"', webpage, 'video id'), '/')
3cfeb162
YCH
138
139 if path == '{{id}}':
140 return self._extract_playlist(orig_path, video_id, webpage)
141
86a7dbe6
YCH
142 # See prepareContentId() of pkgCvp.js
143 if path.startswith('video/teams'):
144 path = 'video/channels/proxy/' + path[6:]
145
29825140
RA
146 return self._extract_cvp_info(
147 'http://www.nba.com/%s.xml' % path, video_id, {
148 'default': {
149 'media_src': 'http://nba.cdn.turner.com/nba/big',
150 },
151 'm3u8': {
152 'media_src': 'http://nbavod-f.akamaihd.net',
153 },
c233e6bc 154 })