[turner,nba,cnn,adultswim] add base extractor to parse cvp feeds
[youtube-dl] / 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': '0041400301-cle-atl-recap',
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     }, {
51         'url': 'http://www.nba.com/clippers/news/doc-rivers-were-not-trading-blake',
52         'info_dict': {
53             'id': '1455672027478-Doc_Feb16_720',
54             'ext': 'mp4',
55             'title': 'Practice: Doc Rivers - 2/16/16',
56             'description': 'Head Coach Doc Rivers addresses the media following practice.',
57             'upload_date': '20160216',
58             'timestamp': 1455672000,
59         },
60         'params': {
61             # m3u8 download
62             'skip_download': True,
63         },
64     }, {
65         'url': 'http://www.nba.com/timberwolves/wiggins-shootaround#',
66         'info_dict': {
67             'id': 'timberwolves',
68             'title': 'Shootaround Access - Dec. 12 | Andrew Wiggins',
69         },
70         'playlist_count': 30,
71         'params': {
72             # Download the whole playlist takes too long time
73             'playlist_items': '1-30',
74         },
75     }, {
76         'url': 'http://www.nba.com/timberwolves/wiggins-shootaround#',
77         'info_dict': {
78             'id': 'Wigginsmp4-3462601',
79             'ext': 'mp4',
80             'title': 'Shootaround Access - Dec. 12 | Andrew Wiggins',
81             'description': 'Wolves rookie Andrew Wiggins addresses the media after Friday\'s shootaround.',
82             'upload_date': '20141212',
83             'timestamp': 1418418600,
84         },
85         'params': {
86             'noplaylist': True,
87             # m3u8 download
88             'skip_download': True,
89         },
90     }]
91
92     _PAGE_SIZE = 30
93
94     def _fetch_page(self, team, video_id, page):
95         search_url = 'http://searchapp2.nba.com/nba-search/query.jsp?' + compat_urllib_parse_urlencode({
96             'type': 'teamvideo',
97             'start': page * self._PAGE_SIZE + 1,
98             'npp': (page + 1) * self._PAGE_SIZE + 1,
99             'sort': 'recent',
100             'output': 'json',
101             'site': team,
102         })
103         results = self._download_json(
104             search_url, video_id, note='Download page %d of playlist data' % page)['results'][0]
105         for item in results:
106             yield self.url_result(compat_urlparse.urljoin('http://www.nba.com/', item['url']))
107
108     def _extract_playlist(self, orig_path, video_id, webpage):
109         team = orig_path.split('/')[0]
110
111         if self._downloader.params.get('noplaylist'):
112             self.to_screen('Downloading just video because of --no-playlist')
113             video_path = self._search_regex(
114                 r'nbaVideoCore\.firstVideo\s*=\s*\'([^\']+)\';', webpage, 'video path')
115             video_url = 'http://www.nba.com/%s/video/%s' % (team, video_path)
116             return self.url_result(video_url)
117
118         self.to_screen('Downloading playlist - add --no-playlist to just download video')
119         playlist_title = self._og_search_title(webpage, fatal=False)
120         entries = OnDemandPagedList(
121             functools.partial(self._fetch_page, team, video_id),
122             self._PAGE_SIZE, use_cache=True)
123
124         return self.playlist_result(entries, team, playlist_title)
125
126     def _real_extract(self, url):
127         path, video_id = re.match(self._VALID_URL, url).groups()
128         orig_path = path
129         if path.startswith('nba/'):
130             path = path[3:]
131
132         if 'video/' not in path:
133             webpage = self._download_webpage(url, video_id)
134             path = remove_start(self._search_regex(r'data-videoid="([^"]+)"', webpage, 'video id'), '/')
135
136             if path == '{{id}}':
137                 return self._extract_playlist(orig_path, video_id, webpage)
138
139             # See prepareContentId() of pkgCvp.js
140             if path.startswith('video/teams'):
141                 path = 'video/channels/proxy/' + path[6:]
142
143         return self._extract_cvp_info(
144             'http://www.nba.com/%s.xml' % path, video_id, {
145                 'default': {
146                     'media_src': 'http://nba.cdn.turner.com/nba/big',
147                 },
148                 'm3u8': {
149                     'media_src': 'http://nbavod-f.akamaihd.net',
150                 },
151             })