]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/nba.py
Start new release and annotate with bugs being fixed.
[youtubedl] / youtube_dl / extractor / nba.py
1 from __future__ import unicode_literals
2
3 import functools
4 import re
5
6 from .turner import TurnerBaseIE
7 from ..compat import (
8 compat_urllib_parse_urlencode,
9 compat_urlparse,
10 )
11 from ..utils import (
12 OnDemandPagedList,
13 remove_start,
14 )
15
16
17 class NBAIE(TurnerBaseIE):
18 _VALID_URL = r'https?://(?:watch\.|www\.)?nba\.com/(?P<path>(?:[^/]+/)+(?P<id>[^?]*?))/?(?:/index\.html)?(?:\?.*)?$'
19 _TESTS = [{
20 'url': 'http://www.nba.com/video/games/nets/2012/12/04/0021200253-okc-bkn-recap.nba/index.html',
21 'md5': '9e7729d3010a9c71506fd1248f74e4f4',
22 'info_dict': {
23 'id': '0021200253-okc-bkn-recap',
24 'ext': 'mp4',
25 'title': 'Thunder vs. Nets',
26 'description': 'Kevin Durant scores 32 points and dishes out six assists as the Thunder beat the Nets in Brooklyn.',
27 'duration': 181,
28 'timestamp': 1354638466,
29 'upload_date': '20121204',
30 },
31 'params': {
32 # m3u8 download
33 'skip_download': True,
34 },
35 }, {
36 'url': 'http://www.nba.com/video/games/hornets/2014/12/05/0021400276-nyk-cha-play5.nba/',
37 'only_matching': True,
38 }, {
39 'url': 'http://watch.nba.com/video/channels/playoffs/2015/05/20/0041400301-cle-atl-recap.nba',
40 'md5': 'b2b39b81cf28615ae0c3360a3f9668c4',
41 'info_dict': {
42 'id': 'channels/playoffs/2015/05/20/0041400301-cle-atl-recap.nba',
43 'ext': 'mp4',
44 'title': 'Hawks vs. Cavaliers Game 1',
45 'description': 'md5:8094c3498d35a9bd6b1a8c396a071b4d',
46 'duration': 228,
47 'timestamp': 1432134543,
48 'upload_date': '20150520',
49 },
50 'expected_warnings': ['Unable to download f4m manifest'],
51 }, {
52 'url': 'http://www.nba.com/clippers/news/doc-rivers-were-not-trading-blake',
53 'info_dict': {
54 'id': 'teams/clippers/2016/02/17/1455672027478-Doc_Feb16_720.mov-297324',
55 'ext': 'mp4',
56 'title': 'Practice: Doc Rivers - 2/16/16',
57 'description': 'Head Coach Doc Rivers addresses the media following practice.',
58 'upload_date': '20160216',
59 'timestamp': 1455672000,
60 },
61 'params': {
62 # m3u8 download
63 'skip_download': True,
64 },
65 'expected_warnings': ['Unable to download f4m manifest'],
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': {
80 'id': 'teams/timberwolves/2014/12/12/Wigginsmp4-3462601',
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 },
92 'expected_warnings': ['Unable to download f4m manifest'],
93 }]
94
95 _PAGE_SIZE = 30
96
97 def _fetch_page(self, team, video_id, page):
98 search_url = 'http://searchapp2.nba.com/nba-search/query.jsp?' + compat_urllib_parse_urlencode({
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),
125 self._PAGE_SIZE)
126
127 return self.playlist_result(entries, team, playlist_title)
128
129 def _real_extract(self, url):
130 path, video_id = re.match(self._VALID_URL, url).groups()
131 orig_path = path
132 if path.startswith('nba/'):
133 path = path[3:]
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'), '/')
138
139 if path == '{{id}}':
140 return self._extract_playlist(orig_path, video_id, webpage)
141
142 # See prepareContentId() of pkgCvp.js
143 if path.startswith('video/teams'):
144 path = 'video/channels/proxy/' + path[6:]
145
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 },
154 })