[nba] skip Legacy Video Files
[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     _QUALITIES = {
42         '420mp4': {
43             'width': 400,
44             'height': 224,
45             'preference': 1,
46         },
47         '416x234': {
48             'width': 416,
49             'height': 234,
50             'preference': 2,
51         },
52         '480x320_910': {
53             'width': 480,
54             'height': 320,
55             'preference': 3,
56         },
57         'nba_576x324': {
58             'width': 576,
59             'height': 324,
60             'preference': 4,
61         },
62         'nba_640x360': {
63             'width': 640,
64             'height': 360,
65             'preference': 5,
66         },
67         '640x360_664b': {
68             'width': 640,
69             'height': 360,
70             'preference': 6,
71         },
72         '640x360_664m': {
73             'width': 640,
74             'height': 360,
75             'preference': 7,
76         },
77         '768x432_996': {
78             'width': 768,
79             'height': 432,
80             'preference': 8,
81         },
82         '768x432_1404': {
83             'width': 768,
84             'height': 432,
85             'preference': 9,
86         },
87         '960x540_2104': {
88             'width': 960,
89             'height': 540,
90             'preference': 10,
91         },
92         '1280x720_3072': {
93             'width': 1280,
94             'height': 720,
95             'preference': 11,
96         },
97     }
98
99     def _real_extract(self, url):
100         video_id = self._match_id(url)
101         video_info = self._download_xml('http://www.nba.com/video/%s.xml' % video_id, video_id)
102         video_id = video_info.find('slug').text
103         title = video_info.find('headline').text
104         description = video_info.find('description').text
105         duration = parse_duration(video_info.find('length').text)
106         timestamp = int_or_none(video_info.find('dateCreated').attrib.get('uts'))
107
108         thumbnails = []
109         for image in video_info.find('images'):
110             thumbnails.append({
111                 'id': image.attrib.get('cut'),
112                 'url': image.text,
113                 'width': int_or_none(image.attrib.get('width')),
114                 'height': int_or_none(image.attrib.get('height')),
115             })
116
117         formats = []
118         for video_file in video_info.find('files').iter('file'):
119             video_url = video_file.text
120             if video_url.startswith('/'):
121                 continue
122             if video_url.endswith('.m3u8'):
123                 formats.extend(self._extract_m3u8_formats(video_url, video_id))
124             elif video_url.endswith('.f4m'):
125                 formats.extend(self._extract_f4m_formats(video_url + '?hdcore=3.4.1.1', video_id))
126             else:
127                 key = video_file.attrib.get('bitrate')
128                 quality = self._QUALITIES[key]
129                 formats.append({
130                     'format_id': key,
131                     'url': video_url,
132                     'width': quality['width'],
133                     'height': quality['height'],
134                     'preference': quality['preference'],
135                 })
136         self._sort_formats(formats)
137
138         return {
139             'id': video_id,
140             'title': title,
141             'description': description,
142             'duration': duration,
143             'timestamp': timestamp,
144             'thumbnails': thumbnails,
145             'formats': formats,
146         }