[nba] extract all video formats and extract more info
[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         base_url = 'http://nba.cdn.turner.com/nba/big%s' % video_id
14         return [{
15             'url': base_url + '_nba_android_high.mp4',
16             'width': 480,
17             'height': 320,
18             'format_id': '320p',
19         },{
20             'url': base_url + '_640x360_664b.mp4',
21             'width': 640,
22             'height': 360,
23             'format_id': '360p',
24         },{
25             'url': base_url + '_768x432_1404.mp4',
26             'width': 768,
27             'height': 432,
28             'format_id': '432p',
29         },{
30             'url': base_url + '_1280x720.mp4',
31             'width': 1280,
32             'height': 720,
33             'format_id': '720p',
34         }]
35
36     def _real_extract(self, url):
37         video_id = self._match_id(url)
38         webpage = self._download_webpage(url, video_id)
39         ret = self._extract_metadata(webpage, video_id)
40         ret['id'] = video_id.rpartition('/')[2]
41         ret['formats'] = self._get_formats(video_id)
42         return ret
43
44
45 class NBAIE(NBABaseIE):
46     IE_NAME = 'nba'
47     _VALID_URL = r'https?://(?:www\.)?nba\.com/(?:nba/)?video(?P<id>/[^?]*?)/?(?:/index\.html)?(?:\?.*)?$'
48     _TESTS = [{
49         'url': 'http://www.nba.com/video/games/nets/2012/12/04/0021200253-okc-bkn-recap.nba/index.html',
50         'md5': '9d902940d2a127af3f7f9d2f3dc79c96',
51         'info_dict': {
52             'id': '0021200253-okc-bkn-recap.nba',
53             'ext': 'mp4',
54             'title': 'Thunder vs. Nets',
55             'description': 'Kevin Durant scores 32 points and dishes out six assists as the Thunder beat the Nets in Brooklyn.',
56             'duration': 181,
57             'timestamp': 1354680189,
58             'upload_date': '20121205',
59         },
60     }, {
61         'url': 'http://www.nba.com/video/games/hornets/2014/12/05/0021400276-nyk-cha-play5.nba/',
62         'only_matching': True,
63     }]
64
65     def _extract_metadata(self, webpage, video_id):
66         return {
67             'title': self._html_search_meta('name', webpage),
68             'description': self._html_search_meta('description', webpage),
69             'duration': parse_duration(self._html_search_meta('duration', webpage)),
70             'thumbnail': self._html_search_meta('thumbnailUrl', webpage),
71             'timestamp': parse_iso8601(self._html_search_meta('uploadDate', webpage))
72         }
73
74 class NBAWatchIE(NBABaseIE):
75     IE_NAME = 'nba:watch'
76     _VALID_URL = r'https?://watch.nba\.com/(?:nba/)?video(?P<id>/[^?]*?)/?(?:/index\.html)?(?:\?.*)?$'
77     _TESTS = [{
78         'url': 'http://watch.nba.com/nba/video/channels/playoffs/2015/05/20/0041400301-cle-atl-recap.nba',
79         'md5': 'b2b39b81cf28615ae0c3360a3f9668c4',
80         'info_dict': {
81             'id': '0041400301-cle-atl-recap.nba',
82             'ext': 'mp4',
83             'title': 'Hawks vs. Cavaliers Game 1',
84             'description': 'md5:8094c3498d35a9bd6b1a8c396a071b4d',
85             'duration': 228,
86             'timestamp': 1432094400,
87             'upload_date': '20150520',
88         }
89     }]
90
91     def _extract_metadata(self, webpage, video_id):
92         program_id = self._search_regex(r'var\s+programId\s*=\s*(\d+);', webpage, 'program id')
93         metadata = self._download_json(
94             '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]
95         return {
96             'title': metadata['name'],
97             'description': metadata.get('description'),
98             'duration': int_or_none(metadata.get('runtime')),
99             'thumbnail': metadata.get('image'),
100             'timestamp': parse_iso8601(metadata.get('releaseDate'))
101         }