[nba] extract more formats
[youtube-dl] / youtube_dl / extractor / nba.py
1 from __future__ import unicode_literals
2
3 from .common import InfoExtractor
4 from ..utils import (
5     parse_duration,
6     parse_iso8601,
7     int_or_none,
8 )
9
10
11 class NBABaseIE(InfoExtractor):
12     def _get_formats(self, video_id):
13         formats = self._extract_m3u8_formats(
14             'http://nbavod-f.akamaihd.net/i/nba/big%s_,640x360_664m,768x432_996,768x432_1404,960x540_2104,1280x720,.mp4.csmil/master.m3u8' % video_id,
15             video_id,
16             m3u8_id='hls')
17         formats.extend(self._extract_f4m_formats(
18             'http://nbavod-f.akamaihd.net/z/nba/big%s_,640x360_664m,768x432_996,768x432_1404,960x540_2104,1280x720,.mp4.csmil/manifest.f4m?hdcore=3.4.1.1' % video_id,
19             video_id,
20             f4m_id='hds'))
21         base_url = 'http://nba.cdn.turner.com/nba/big%s' % video_id
22         formats.extend([{
23             'url': base_url + '_nba_ipad.mp4',
24             'width': 400,
25             'height': 224,
26             'format_id': '224p',
27             'preference': 1,
28         },{
29             'url': base_url + '_nba_android_high.mp4',
30             'width': 480,
31             'height': 320,
32             'format_id': '320p',
33             'preference': 2,
34         },{
35             'url': base_url + '_nba_576x324.mp4',
36             'width': 576,
37             'height': 324,
38             'format_id': '324p',
39             'preference': 3,
40         },{
41             'url': base_url + '_640x360_664b.mp4',
42             'width': 640,
43             'height': 360,
44             'format_id': '360p',
45             'preference': 4,
46         },{
47             'url': base_url + '_768x432_1404.mp4',
48             'width': 768,
49             'height': 432,
50             'format_id': '432p',
51             'preference': 5,
52         },{
53             'url': base_url + '_960x540_2104.mp4',
54             'width': 960,
55             'height': 540,
56             'format_id': '540p',
57             'preference': 6,
58         },{
59             'url': base_url + '_1280x720.mp4',
60             'width': 1280,
61             'height': 720,
62             'format_id': '720p',
63             'preference': 7,
64         }])
65         self._sort_formats(formats)
66         return formats
67
68     def _real_extract(self, url):
69         video_id = self._match_id(url)
70         webpage = self._download_webpage(url, video_id)
71         ret = self._extract_metadata(webpage, video_id)
72         ret['id'] = video_id.rpartition('/')[2]
73         ret['formats'] = self._get_formats(video_id)
74         return ret
75
76
77 class NBAIE(NBABaseIE):
78     IE_NAME = 'nba'
79     _VALID_URL = r'https?://(?:www\.)?nba\.com/(?:nba/)?video(?P<id>/[^?]*?)/?(?:/index\.html)?(?:\?.*)?$'
80     _TESTS = [{
81         'url': 'http://www.nba.com/video/games/nets/2012/12/04/0021200253-okc-bkn-recap.nba/index.html',
82         'md5': '9d902940d2a127af3f7f9d2f3dc79c96',
83         'info_dict': {
84             'id': '0021200253-okc-bkn-recap.nba',
85             'ext': 'mp4',
86             'title': 'Thunder vs. Nets',
87             'description': 'Kevin Durant scores 32 points and dishes out six assists as the Thunder beat the Nets in Brooklyn.',
88             'duration': 181,
89             'timestamp': 1354680189,
90             'upload_date': '20121205',
91         },
92     }, {
93         'url': 'http://www.nba.com/video/games/hornets/2014/12/05/0021400276-nyk-cha-play5.nba/',
94         'only_matching': True,
95     }]
96
97     def _extract_metadata(self, webpage, video_id):
98         return {
99             'title': self._html_search_meta('name', webpage),
100             'description': self._html_search_meta('description', webpage),
101             'duration': parse_duration(self._html_search_meta('duration', webpage)),
102             'thumbnail': self._html_search_meta('thumbnailUrl', webpage),
103             'timestamp': parse_iso8601(self._html_search_meta('uploadDate', webpage))
104         }
105
106 class NBAWatchIE(NBABaseIE):
107     IE_NAME = 'nba:watch'
108     _VALID_URL = r'https?://watch.nba\.com/(?:nba/)?video(?P<id>/[^?]*?)/?(?:/index\.html)?(?:\?.*)?$'
109     _TESTS = [{
110         'url': 'http://watch.nba.com/nba/video/channels/playoffs/2015/05/20/0041400301-cle-atl-recap.nba',
111         'md5': 'b2b39b81cf28615ae0c3360a3f9668c4',
112         'info_dict': {
113             'id': '0041400301-cle-atl-recap.nba',
114             'ext': 'mp4',
115             'title': 'Hawks vs. Cavaliers Game 1',
116             'description': 'md5:8094c3498d35a9bd6b1a8c396a071b4d',
117             'duration': 228,
118             'timestamp': 1432094400,
119             'upload_date': '20150520',
120         }
121     }]
122
123     def _extract_metadata(self, webpage, video_id):
124         program_id = self._search_regex(r'var\s+programId\s*=\s*(\d+);', webpage, 'program id')
125         metadata = self._download_json(
126             'http://smbsolr.cdnak.neulion.com/solr_nbav6/nba/nba/mlt/?wt=json&fl=name,description,image,runtime,releaseDate&q=sequence%3A' + program_id, video_id)['match']['docs'][0]
127         return {
128             'title': metadata['name'],
129             'description': metadata.get('description'),
130             'duration': int_or_none(metadata.get('runtime')),
131             'thumbnail': metadata.get('image'),
132             'timestamp': parse_iso8601(metadata.get('releaseDate'))
133         }