]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/vevo.py
Update changelog.
[youtubedl] / youtube_dl / extractor / vevo.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..compat import compat_etree_fromstring
7 from ..utils import (
8 ExtractorError,
9 int_or_none,
10 sanitized_Request,
11 parse_iso8601,
12 )
13
14
15 class VevoIE(InfoExtractor):
16 '''
17 Accepts urls from vevo.com or in the format 'vevo:{id}'
18 (currently used by MTVIE and MySpaceIE)
19 '''
20 _VALID_URL = r'''(?x)
21 (?:https?://www\.vevo\.com/watch/(?:[^/]+/(?:[^/]+/)?)?|
22 https?://cache\.vevo\.com/m/html/embed\.html\?video=|
23 https?://videoplayer\.vevo\.com/embed/embedded\?videoId=|
24 vevo:)
25 (?P<id>[^&?#]+)'''
26
27 _TESTS = [{
28 'url': 'http://www.vevo.com/watch/hurts/somebody-to-die-for/GB1101300280',
29 'md5': '95ee28ee45e70130e3ab02b0f579ae23',
30 'info_dict': {
31 'id': 'GB1101300280',
32 'ext': 'mp4',
33 'title': 'Somebody to Die For',
34 'upload_date': '20130624',
35 'uploader': 'Hurts',
36 'timestamp': 1372057200,
37 },
38 }, {
39 'note': 'v3 SMIL format',
40 'url': 'http://www.vevo.com/watch/cassadee-pope/i-wish-i-could-break-your-heart/USUV71302923',
41 'md5': 'f6ab09b034f8c22969020b042e5ac7fc',
42 'info_dict': {
43 'id': 'USUV71302923',
44 'ext': 'mp4',
45 'title': 'I Wish I Could Break Your Heart',
46 'upload_date': '20140219',
47 'uploader': 'Cassadee Pope',
48 'timestamp': 1392796919,
49 },
50 }, {
51 'note': 'Age-limited video',
52 'url': 'https://www.vevo.com/watch/justin-timberlake/tunnel-vision-explicit/USRV81300282',
53 'info_dict': {
54 'id': 'USRV81300282',
55 'ext': 'mp4',
56 'title': 'Tunnel Vision (Explicit)',
57 'upload_date': '20130703',
58 'age_limit': 18,
59 'uploader': 'Justin Timberlake',
60 'timestamp': 1372888800,
61 },
62 }, {
63 'note': 'No video_info',
64 'url': 'http://www.vevo.com/watch/k-camp-1/Till-I-Die/USUV71503000',
65 'md5': '8b83cc492d72fc9cf74a02acee7dc1b0',
66 'info_dict': {
67 'id': 'USUV71503000',
68 'ext': 'mp4',
69 'title': 'Till I Die',
70 'upload_date': '20151207',
71 'age_limit': 18,
72 'uploader': 'K Camp',
73 'timestamp': 1449468000,
74 },
75 }]
76 _SMIL_BASE_URL = 'http://smil.lvl3.vevo.com'
77 _SOURCE_TYPES = {
78 0: 'youtube',
79 1: 'brightcove',
80 2: 'http',
81 3: 'hls_ios',
82 4: 'hls',
83 5: 'smil', # http
84 7: 'f4m_cc',
85 8: 'f4m_ak',
86 9: 'f4m_l3',
87 10: 'ism',
88 13: 'smil', # rtmp
89 18: 'dash',
90 }
91 _VERSIONS = {
92 0: 'youtube', # only in AuthenticateVideo videoVersions
93 1: 'level3',
94 2: 'akamai',
95 3: 'level3',
96 4: 'amazon',
97 }
98
99 def _parse_smil_formats(self, smil, smil_url, video_id, namespace=None, f4m_params=None, transform_rtmp_url=None):
100 formats = []
101 els = smil.findall('.//{http://www.w3.org/2001/SMIL20/Language}video')
102 for el in els:
103 src = el.attrib['src']
104 m = re.match(r'''(?xi)
105 (?P<ext>[a-z0-9]+):
106 (?P<path>
107 [/a-z0-9]+ # The directory and main part of the URL
108 _(?P<tbr>[0-9]+)k
109 _(?P<width>[0-9]+)x(?P<height>[0-9]+)
110 _(?P<vcodec>[a-z0-9]+)
111 _(?P<vbr>[0-9]+)
112 _(?P<acodec>[a-z0-9]+)
113 _(?P<abr>[0-9]+)
114 \.[a-z0-9]+ # File extension
115 )''', src)
116 if not m:
117 continue
118
119 format_url = self._SMIL_BASE_URL + m.group('path')
120 formats.append({
121 'url': format_url,
122 'format_id': 'smil_' + m.group('tbr'),
123 'vcodec': m.group('vcodec'),
124 'acodec': m.group('acodec'),
125 'tbr': int(m.group('tbr')),
126 'vbr': int(m.group('vbr')),
127 'abr': int(m.group('abr')),
128 'ext': m.group('ext'),
129 'width': int(m.group('width')),
130 'height': int(m.group('height')),
131 })
132 return formats
133
134 def _initialize_api(self, video_id):
135 req = sanitized_Request(
136 'http://www.vevo.com/auth', data=b'')
137 webpage = self._download_webpage(
138 req, None,
139 note='Retrieving oauth token',
140 errnote='Unable to retrieve oauth token')
141
142 if 'THIS PAGE IS CURRENTLY UNAVAILABLE IN YOUR REGION' in webpage:
143 raise ExtractorError(
144 '%s said: This page is currently unavailable in your region.' % self.IE_NAME, expected=True)
145
146 auth_info = self._parse_json(webpage, video_id)
147 self._api_url_template = self.http_scheme() + '//apiv2.vevo.com/%s?token=' + auth_info['access_token']
148
149 def _call_api(self, path, video_id, note, errnote, fatal=True):
150 return self._download_json(self._api_url_template % path, video_id, note, errnote)
151
152 def _real_extract(self, url):
153 video_id = self._match_id(url)
154
155 json_url = 'http://videoplayer.vevo.com/VideoService/AuthenticateVideo?isrc=%s' % video_id
156 response = self._download_json(
157 json_url, video_id, 'Downloading video info', 'Unable to download info')
158 video_info = response.get('video') or {}
159 video_versions = video_info.get('videoVersions')
160 uploader = None
161 timestamp = None
162 view_count = None
163 formats = []
164
165 if not video_info:
166 if response.get('statusCode') != 909:
167 ytid = response.get('errorInfo', {}).get('ytid')
168 if ytid:
169 self.report_warning(
170 'Video is geoblocked, trying with the YouTube video %s' % ytid)
171 return self.url_result(ytid, 'Youtube', ytid)
172
173 if 'statusMessage' in response:
174 raise ExtractorError('%s said: %s' % (
175 self.IE_NAME, response['statusMessage']), expected=True)
176 raise ExtractorError('Unable to extract videos')
177
178 self._initialize_api(video_id)
179 video_info = self._call_api(
180 'video/%s' % video_id, video_id, 'Downloading api video info',
181 'Failed to download video info')
182
183 video_versions = self._call_api(
184 'video/%s/streams' % video_id, video_id,
185 'Downloading video versions info',
186 'Failed to download video versions info')
187
188 timestamp = parse_iso8601(video_info.get('releaseDate'))
189 artists = video_info.get('artists')
190 if artists:
191 uploader = artists[0]['name']
192 view_count = int_or_none(video_info.get('views', {}).get('total'))
193
194 for video_version in video_versions:
195 version = self._VERSIONS.get(video_version['version'])
196 version_url = video_version.get('url')
197 if not version_url:
198 continue
199
200 if '.ism' in version_url:
201 continue
202 elif '.mpd' in version_url:
203 formats.extend(self._extract_mpd_formats(
204 version_url, video_id, mpd_id='dash-%s' % version,
205 note='Downloading %s MPD information' % version,
206 errnote='Failed to download %s MPD information' % version,
207 fatal=False))
208 elif '.m3u8' in version_url:
209 formats.extend(self._extract_m3u8_formats(
210 version_url, video_id, 'mp4', 'm3u8_native',
211 m3u8_id='hls-%s' % version,
212 note='Downloading %s m3u8 information' % version,
213 errnote='Failed to download %s m3u8 information' % version,
214 fatal=False))
215 else:
216 m = re.search(r'''(?xi)
217 _(?P<width>[0-9]+)x(?P<height>[0-9]+)
218 _(?P<vcodec>[a-z0-9]+)
219 _(?P<vbr>[0-9]+)
220 _(?P<acodec>[a-z0-9]+)
221 _(?P<abr>[0-9]+)
222 \.(?P<ext>[a-z0-9]+)''', version_url)
223 if not m:
224 continue
225
226 formats.append({
227 'url': version_url,
228 'format_id': 'http-%s-%s' % (version, video_version['quality']),
229 'vcodec': m.group('vcodec'),
230 'acodec': m.group('acodec'),
231 'vbr': int(m.group('vbr')),
232 'abr': int(m.group('abr')),
233 'ext': m.group('ext'),
234 'width': int(m.group('width')),
235 'height': int(m.group('height')),
236 })
237 else:
238 timestamp = int_or_none(self._search_regex(
239 r'/Date\((\d+)\)/',
240 video_info['releaseDate'], 'release date', fatal=False),
241 scale=1000)
242 artists = video_info.get('mainArtists')
243 if artists:
244 uploader = artists[0]['artistName']
245
246 smil_parsed = False
247 for video_version in video_info['videoVersions']:
248 version = self._VERSIONS.get(video_version['version'])
249 if version == 'youtube':
250 continue
251 else:
252 source_type = self._SOURCE_TYPES.get(video_version['sourceType'])
253 renditions = compat_etree_fromstring(video_version['data'])
254 if source_type == 'http':
255 for rend in renditions.findall('rendition'):
256 attr = rend.attrib
257 formats.append({
258 'url': attr['url'],
259 'format_id': 'http-%s-%s' % (version, attr['name']),
260 'height': int_or_none(attr.get('frameheight')),
261 'width': int_or_none(attr.get('frameWidth')),
262 'tbr': int_or_none(attr.get('totalBitrate')),
263 'vbr': int_or_none(attr.get('videoBitrate')),
264 'abr': int_or_none(attr.get('audioBitrate')),
265 'vcodec': attr.get('videoCodec'),
266 'acodec': attr.get('audioCodec'),
267 })
268 elif source_type == 'hls':
269 formats.extend(self._extract_m3u8_formats(
270 renditions.find('rendition').attrib['url'], video_id,
271 'mp4', 'm3u8_native', m3u8_id='hls-%s' % version,
272 note='Downloading %s m3u8 information' % version,
273 errnote='Failed to download %s m3u8 information' % version,
274 fatal=False))
275 elif source_type == 'smil' and version == 'level3' and not smil_parsed:
276 formats.extend(self._extract_smil_formats(
277 renditions.find('rendition').attrib['url'], video_id, False))
278 smil_parsed = True
279 self._sort_formats(formats)
280
281 title = video_info['title']
282
283 is_explicit = video_info.get('isExplicit')
284 if is_explicit is True:
285 age_limit = 18
286 elif is_explicit is False:
287 age_limit = 0
288 else:
289 age_limit = None
290
291 duration = video_info.get('duration')
292
293 return {
294 'id': video_id,
295 'title': title,
296 'formats': formats,
297 'thumbnail': video_info.get('imageUrl') or video_info.get('thumbnailUrl'),
298 'timestamp': timestamp,
299 'uploader': uploader,
300 'duration': duration,
301 'view_count': view_count,
302 'age_limit': age_limit,
303 }