[nba] extract video info from xml feed
[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     int_or_none,
7 )
8
9
10 class NBAIE(InfoExtractor):
11     _VALID_URL = r'https?://(?:watch\.|www\.)?nba\.com/(?:nba/)?video/(?P<id>[^?]*?)/?(?:/index\.html)?(?:\?.*)?$'
12     _TESTS = [{
13         'url': 'http://www.nba.com/video/games/nets/2012/12/04/0021200253-okc-bkn-recap.nba/index.html',
14         'md5': '9d902940d2a127af3f7f9d2f3dc79c96',
15         'info_dict': {
16             'id': '0021200253-okc-bkn-recap',
17             'ext': 'mp4',
18             'title': 'Thunder vs. Nets',
19             'description': 'Kevin Durant scores 32 points and dishes out six assists as the Thunder beat the Nets in Brooklyn.',
20             'duration': 181,
21             'timestamp': 1354638466,
22             'upload_date': '20121204',
23         },
24     }, {
25         'url': 'http://www.nba.com/video/games/hornets/2014/12/05/0021400276-nyk-cha-play5.nba/',
26         'only_matching': True,
27     },{
28         'url': 'http://watch.nba.com/nba/video/channels/playoffs/2015/05/20/0041400301-cle-atl-recap.nba',
29         'md5': 'b2b39b81cf28615ae0c3360a3f9668c4',
30         'info_dict': {
31             'id': '0041400301-cle-atl-recap',
32             'ext': 'mp4',
33             'title': 'Hawks vs. Cavaliers Game 1',
34             'description': 'md5:8094c3498d35a9bd6b1a8c396a071b4d',
35             'duration': 228,
36             'timestamp': 1432134543,
37             'upload_date': '20150520',
38         }
39     }]
40
41     _BASE_PATHS = {
42         'turner': 'http://nba.cdn.turner.com/nba/big',
43         'akamai': 'http://nbavod-f.akamaihd.net',
44     }
45
46     _QUALITIES = {
47         '420mp4': {
48             'width': 400,
49             'height': 224,
50             'preference': 1,
51         },
52         '416x234': {
53             'width': 416,
54             'height': 234,
55             'preference': 2,
56         },
57         '556': {
58             'width': 416,
59             'height': 234,
60             'preference': 3,
61         },
62         '480x320_910': {
63             'width': 480,
64             'height': 320,
65             'preference': 4,
66         },
67         'nba_576x324': {
68             'width': 576,
69             'height': 324,
70             'preference': 5,
71         },
72         'nba_640x360': {
73             'width': 640,
74             'height': 360,
75             'preference': 6,
76         },
77         '640x360_664b': {
78             'width': 640,
79             'height': 360,
80             'preference': 7,
81         },
82         '640x360_664m': {
83             'width': 640,
84             'height': 360,
85             'preference': 8,
86         },
87         '768x432_996': {
88             'width': 768,
89             'height': 432,
90             'preference': 9,
91         },
92         '768x432_1404': {
93             'width': 768,
94             'height': 432,
95             'preference': 10,
96         },
97         '960x540_2104': {
98             'width': 960,
99             'height': 540,
100             'preference': 11,
101         },
102         '1280x720_3072': {
103             'width': 1280,
104             'height': 720,
105             'preference': 12,
106         },
107     }
108
109     def _real_extract(self, url):
110         video_id = self._match_id(url)
111         video_info = self._download_xml('http://www.nba.com/video/%s.xml' % video_id, video_id)
112         video_id = video_info.find('slug').text
113         title = video_info.find('headline').text
114         description = video_info.find('description').text
115         duration = parse_duration(video_info.find('length').text)
116         timestamp = int_or_none(video_info.find('dateCreated').attrib.get('uts'))
117
118         thumbnails = []
119         for image in video_info.find('images'):
120             thumbnails.append({
121                 'id': image.attrib.get('cut'),
122                 'url': image.text,
123                 'width': int_or_none(image.attrib.get('width')),
124                 'height': int_or_none(image.attrib.get('height')),
125             })
126
127         formats = []
128         for video_file in video_info.find('files').iter('file'):
129             video_url = video_file.text
130             if not video_url.startswith('http://'):
131                 if video_url.endswith('.m3u8') or video_url.endswith('.f4m'):
132                     video_url = self._BASE_PATHS['akamai'] + video_url
133                 else:
134                     video_url = self._BASE_PATHS['turner'] + video_url
135             if video_url.endswith('.m3u8'):
136                 formats.extend(self._extract_m3u8_formats(video_url, video_id))
137             elif video_url.endswith('.f4m'):
138                 formats.extend(self._extract_f4m_formats(video_url + '?hdcore=3.4.1.1', video_id))
139             else:
140                 key = video_file.attrib.get('bitrate')
141                 quality = self._QUALITIES[key]
142                 formats.append({
143                     'format_id': key,
144                     'url': video_url,
145                     'width': quality['width'],
146                     'height': quality['height'],
147                     'preference': quality['preference'],
148                 })
149         self._sort_formats(formats)
150
151         return {
152             'id': video_id,
153             'title': title,
154             'description': description,
155             'duration': duration,
156             'timestamp': timestamp,
157             'thumbnails': thumbnails,
158             'formats': formats,
159         }