]> jfr.im git - yt-dlp.git/blame - youtube_dlc/extractor/vevo.py
[skip travis] renaming
[yt-dlp.git] / youtube_dlc / extractor / vevo.py
CommitLineData
45d7bc2f
JMF
1from __future__ import unicode_literals
2
70d1924f 3import re
55992530 4import json
70d1924f
JMF
5
6from .common import InfoExtractor
e0da32df 7from ..compat import (
5c9ced95 8 compat_str,
e0da32df 9 compat_urlparse,
9bccdc70 10 compat_HTTPError,
e0da32df 11)
1cc79574 12from ..utils import (
70d1924f 13 ExtractorError,
7d3d06a1 14 int_or_none,
9165d6ba 15 parse_iso8601,
70d1924f
JMF
16)
17
88bd97e3 18
9618c448 19class VevoBaseIE(InfoExtractor):
9d0c08a0 20 def _extract_json(self, webpage, video_id):
9618c448
S
21 return self._parse_json(
22 self._search_regex(
23 r'window\.__INITIAL_STORE__\s*=\s*({.+?});\s*</script>',
24 webpage, 'initial store'),
9d0c08a0 25 video_id)
9618c448
S
26
27
28class VevoIE(VevoBaseIE):
2975fe1a 29 '''
0577177e 30 Accepts urls from vevo.com or in the format 'vevo:{id}'
3266f0c6 31 (currently used by MTVIE and MySpaceIE)
2975fe1a 32 '''
f25571ff 33 _VALID_URL = r'''(?x)
92519402 34 (?:https?://(?:www\.)?vevo\.com/watch/(?!playlist|genre)(?:[^/]+/(?:[^/]+/)?)?|
f25571ff 35 https?://cache\.vevo\.com/m/html/embed\.html\?video=|
ebce53b3 36 https?://videoplayer\.vevo\.com/embed/embedded\?videoId=|
d1e41164 37 https?://embed\.vevo\.com/.*?[?&]isrc=|
f25571ff
PH
38 vevo:)
39 (?P<id>[^&?#]+)'''
fd5e6f7e 40
72321ead 41 _TESTS = [{
45d7bc2f 42 'url': 'http://www.vevo.com/watch/hurts/somebody-to-die-for/GB1101300280',
682f8c43 43 'md5': '95ee28ee45e70130e3ab02b0f579ae23',
45d7bc2f 44 'info_dict': {
682f8c43 45 'id': 'GB1101300280',
27579b9e 46 'ext': 'mp4',
881dbc86
S
47 'title': 'Hurts - Somebody to Die For',
48 'timestamp': 1372057200,
2975fe1a 49 'upload_date': '20130624',
682f8c43 50 'uploader': 'Hurts',
881dbc86
S
51 'track': 'Somebody to Die For',
52 'artist': 'Hurts',
53 'genre': 'Pop',
2975fe1a 54 },
1db05846 55 'expected_warnings': ['Unable to download SMIL file', 'Unable to download info'],
27579b9e
PH
56 }, {
57 'note': 'v3 SMIL format',
58 'url': 'http://www.vevo.com/watch/cassadee-pope/i-wish-i-could-break-your-heart/USUV71302923',
682f8c43 59 'md5': 'f6ab09b034f8c22969020b042e5ac7fc',
27579b9e 60 'info_dict': {
682f8c43 61 'id': 'USUV71302923',
27579b9e 62 'ext': 'mp4',
881dbc86
S
63 'title': 'Cassadee Pope - I Wish I Could Break Your Heart',
64 'timestamp': 1392796919,
27579b9e 65 'upload_date': '20140219',
682f8c43 66 'uploader': 'Cassadee Pope',
881dbc86
S
67 'track': 'I Wish I Could Break Your Heart',
68 'artist': 'Cassadee Pope',
69 'genre': 'Country',
2975fe1a 70 },
1db05846 71 'expected_warnings': ['Unable to download SMIL file', 'Unable to download info'],
6cadf8c8
PH
72 }, {
73 'note': 'Age-limited video',
74 'url': 'https://www.vevo.com/watch/justin-timberlake/tunnel-vision-explicit/USRV81300282',
75 'info_dict': {
682f8c43 76 'id': 'USRV81300282',
6cadf8c8 77 'ext': 'mp4',
881dbc86 78 'title': 'Justin Timberlake - Tunnel Vision (Explicit)',
682f8c43 79 'age_limit': 18,
682f8c43 80 'timestamp': 1372888800,
881dbc86
S
81 'upload_date': '20130703',
82 'uploader': 'Justin Timberlake',
83 'track': 'Tunnel Vision (Explicit)',
84 'artist': 'Justin Timberlake',
85 'genre': 'Pop',
2975fe1a 86 },
1db05846 87 'expected_warnings': ['Unable to download SMIL file', 'Unable to download info'],
ff51983e
YCH
88 }, {
89 'note': 'No video_info',
90 'url': 'http://www.vevo.com/watch/k-camp-1/Till-I-Die/USUV71503000',
682f8c43 91 'md5': '8b83cc492d72fc9cf74a02acee7dc1b0',
ff51983e 92 'info_dict': {
682f8c43 93 'id': 'USUV71503000',
ff51983e 94 'ext': 'mp4',
8b27d83e 95 'title': 'K Camp ft. T.I. - Till I Die',
682f8c43 96 'age_limit': 18,
682f8c43 97 'timestamp': 1449468000,
881dbc86
S
98 'upload_date': '20151207',
99 'uploader': 'K Camp',
100 'track': 'Till I Die',
101 'artist': 'K Camp',
8b27d83e
AE
102 'genre': 'Hip-Hop',
103 },
1db05846 104 'expected_warnings': ['Unable to download SMIL file', 'Unable to download info'],
8b27d83e
AE
105 }, {
106 'note': 'Featured test',
107 'url': 'https://www.vevo.com/watch/lemaitre/Wait/USUV71402190',
108 'md5': 'd28675e5e8805035d949dc5cf161071d',
109 'info_dict': {
110 'id': 'USUV71402190',
111 'ext': 'mp4',
112 'title': 'Lemaitre ft. LoLo - Wait',
113 'age_limit': 0,
114 'timestamp': 1413432000,
115 'upload_date': '20141016',
116 'uploader': 'Lemaitre',
117 'track': 'Wait',
118 'artist': 'Lemaitre',
119 'genre': 'Electronic',
ff51983e 120 },
1db05846 121 'expected_warnings': ['Unable to download SMIL file', 'Unable to download info'],
8e7d0048
S
122 }, {
123 'note': 'Only available via webpage',
124 'url': 'http://www.vevo.com/watch/GBUV71600656',
125 'md5': '67e79210613865b66a47c33baa5e37fe',
126 'info_dict': {
127 'id': 'GBUV71600656',
128 'ext': 'mp4',
881dbc86 129 'title': 'ABC - Viva Love',
8e7d0048 130 'age_limit': 0,
8e7d0048 131 'timestamp': 1461830400,
881dbc86
S
132 'upload_date': '20160428',
133 'uploader': 'ABC',
134 'track': 'Viva Love',
135 'artist': 'ABC',
136 'genre': 'Pop',
8e7d0048
S
137 },
138 'expected_warnings': ['Failed to download video versions info'],
5c9ced95
S
139 }, {
140 # no genres available
141 'url': 'http://www.vevo.com/watch/INS171400764',
142 'only_matching': True,
9d0c08a0
YCH
143 }, {
144 # Another case available only via the webpage; using streams/streamsV3 formats
145 # Geo-restricted to Netherlands/Germany
146 'url': 'http://www.vevo.com/watch/boostee/pop-corn-clip-officiel/FR1A91600909',
147 'only_matching': True,
d1e41164 148 }, {
149 'url': 'https://embed.vevo.com/?isrc=USH5V1923499&partnerId=4d61b777-8023-4191-9ede-497ed6c24647&partnerAdCode=',
150 'only_matching': True,
72321ead 151 }]
2975fe1a 152 _VERSIONS = {
9165d6ba 153 0: 'youtube', # only in AuthenticateVideo videoVersions
2975fe1a 154 1: 'level3',
155 2: 'akamai',
156 3: 'level3',
157 4: 'amazon',
158 }
159
682f8c43 160 def _initialize_api(self, video_id):
9165d6ba 161 webpage = self._download_webpage(
b07ea5ea 162 'https://accounts.vevo.com/token', None,
9165d6ba 163 note='Retrieving oauth token',
b07ea5ea
S
164 errnote='Unable to retrieve oauth token',
165 data=json.dumps({
166 'client_id': 'SPupX1tvqFEopQ1YS6SS',
167 'grant_type': 'urn:vevo:params:oauth:grant-type:anonymous',
168 }).encode('utf-8'),
169 headers={
170 'Content-Type': 'application/json',
171 })
9165d6ba 172
621a2800 173 if re.search(r'(?i)THIS PAGE IS CURRENTLY UNAVAILABLE IN YOUR REGION', webpage):
bc7e77a0
S
174 self.raise_geo_restricted(
175 '%s said: This page is currently unavailable in your region' % self.IE_NAME)
9165d6ba 176
177 auth_info = self._parse_json(webpage, video_id)
55992530 178 self._api_url_template = self.http_scheme() + '//apiv2.vevo.com/%s?token=' + auth_info['legacy_token']
9165d6ba 179
516ea41a 180 def _call_api(self, path, *args, **kwargs):
9bccdc70
RA
181 try:
182 data = self._download_json(self._api_url_template % path, *args, **kwargs)
183 except ExtractorError as e:
184 if isinstance(e.cause, compat_HTTPError):
185 errors = self._parse_json(e.cause.read().decode(), None)['errors']
186 error_message = ', '.join([error['message'] for error in errors])
187 raise ExtractorError('%s said: %s' % (self.IE_NAME, error_message), expected=True)
188 raise
189 return data
9165d6ba 190
72321ead 191 def _real_extract(self, url):
4b942883 192 video_id = self._match_id(url)
72321ead 193
9bccdc70 194 self._initialize_api(video_id)
682f8c43 195
9bccdc70
RA
196 video_info = self._call_api(
197 'video/%s' % video_id, video_id, 'Downloading api video info',
198 'Failed to download video info')
2975fe1a 199
9bccdc70
RA
200 video_versions = self._call_api(
201 'video/%s/streams' % video_id, video_id,
202 'Downloading video versions info',
203 'Failed to download video versions info',
204 fatal=False)
ff51983e 205
9bccdc70 206 # Some videos are only available via webpage (e.g.
067aa17e 207 # https://github.com/ytdl-org/youtube-dl/issues/9366)
9bccdc70
RA
208 if not video_versions:
209 webpage = self._download_webpage(url, video_id)
9d0c08a0
YCH
210 json_data = self._extract_json(webpage, video_id)
211 if 'streams' in json_data.get('default', {}):
212 video_versions = json_data['default']['streams'][video_id][0]
213 else:
214 video_versions = [
215 value
216 for key, value in json_data['apollo']['data'].items()
217 if key.startswith('%s.streams' % video_id)]
9618c448 218
9bccdc70
RA
219 uploader = None
220 artist = None
221 featured_artist = None
222 artists = video_info.get('artists')
223 for curr_artist in artists:
224 if curr_artist.get('role') == 'Featured':
225 featured_artist = curr_artist['name']
226 else:
227 artist = uploader = curr_artist['name']
9165d6ba 228
9bccdc70
RA
229 formats = []
230 for video_version in video_versions:
9d0c08a0 231 version = self._VERSIONS.get(video_version.get('version'), 'generic')
9bccdc70
RA
232 version_url = video_version.get('url')
233 if not version_url:
234 continue
9165d6ba 235
9bccdc70
RA
236 if '.ism' in version_url:
237 continue
238 elif '.mpd' in version_url:
239 formats.extend(self._extract_mpd_formats(
240 version_url, video_id, mpd_id='dash-%s' % version,
241 note='Downloading %s MPD information' % version,
242 errnote='Failed to download %s MPD information' % version,
243 fatal=False))
244 elif '.m3u8' in version_url:
245 formats.extend(self._extract_m3u8_formats(
246 version_url, video_id, 'mp4', 'm3u8_native',
247 m3u8_id='hls-%s' % version,
248 note='Downloading %s m3u8 information' % version,
249 errnote='Failed to download %s m3u8 information' % version,
250 fatal=False))
251 else:
252 m = re.search(r'''(?xi)
253 _(?P<width>[0-9]+)x(?P<height>[0-9]+)
254 _(?P<vcodec>[a-z0-9]+)
255 _(?P<vbr>[0-9]+)
256 _(?P<acodec>[a-z0-9]+)
257 _(?P<abr>[0-9]+)
258 \.(?P<ext>[a-z0-9]+)''', version_url)
259 if not m:
9165d6ba 260 continue
9165d6ba 261
9bccdc70
RA
262 formats.append({
263 'url': version_url,
264 'format_id': 'http-%s-%s' % (version, video_version['quality']),
265 'vcodec': m.group('vcodec'),
266 'acodec': m.group('acodec'),
267 'vbr': int(m.group('vbr')),
268 'abr': int(m.group('abr')),
269 'ext': m.group('ext'),
270 'width': int(m.group('width')),
271 'height': int(m.group('height')),
272 })
2975fe1a 273 self._sort_formats(formats)
27579b9e 274
881dbc86 275 track = video_info['title']
9508738f
S
276 if featured_artist:
277 artist = '%s ft. %s' % (artist, featured_artist)
278 title = '%s - %s' % (artist, track) if artist else track
5c9ced95
S
279
280 genres = video_info.get('genres')
281 genre = (
3089bc74
S
282 genres[0] if genres and isinstance(genres, list)
283 and isinstance(genres[0], compat_str) else None)
9165d6ba 284
6cadf8c8
PH
285 is_explicit = video_info.get('isExplicit')
286 if is_explicit is True:
287 age_limit = 18
288 elif is_explicit is False:
289 age_limit = 0
290 else:
291 age_limit = None
292
45d7bc2f 293 return {
88bd97e3 294 'id': video_id,
ff51983e 295 'title': title,
88bd97e3 296 'formats': formats,
9165d6ba 297 'thumbnail': video_info.get('imageUrl') or video_info.get('thumbnailUrl'),
9bccdc70 298 'timestamp': parse_iso8601(video_info.get('releaseDate')),
9165d6ba 299 'uploader': uploader,
9bccdc70
RA
300 'duration': int_or_none(video_info.get('duration')),
301 'view_count': int_or_none(video_info.get('views', {}).get('total')),
6cadf8c8 302 'age_limit': age_limit,
881dbc86
S
303 'track': track,
304 'artist': uploader,
305 'genre': genre,
88bd97e3 306 }
e0da32df
S
307
308
9618c448 309class VevoPlaylistIE(VevoBaseIE):
92519402 310 _VALID_URL = r'https?://(?:www\.)?vevo\.com/watch/(?P<kind>playlist|genre)/(?P<id>[^/?#&]+)'
e0da32df
S
311
312 _TESTS = [{
313 'url': 'http://www.vevo.com/watch/playlist/dadbf4e7-b99f-4184-9670-6f0e547b6a29',
314 'info_dict': {
315 'id': 'dadbf4e7-b99f-4184-9670-6f0e547b6a29',
316 'title': 'Best-Of: Birdman',
317 },
318 'playlist_count': 10,
e2bd301c
S
319 }, {
320 'url': 'http://www.vevo.com/watch/genre/rock',
321 'info_dict': {
322 'id': 'rock',
323 'title': 'Rock',
324 },
325 'playlist_count': 20,
e0da32df
S
326 }, {
327 'url': 'http://www.vevo.com/watch/playlist/dadbf4e7-b99f-4184-9670-6f0e547b6a29?index=0',
328 'md5': '32dcdfddddf9ec6917fc88ca26d36282',
329 'info_dict': {
330 'id': 'USCMV1100073',
331 'ext': 'mp4',
881dbc86 332 'title': 'Birdman - Y.U. MAD',
e0da32df
S
333 'timestamp': 1323417600,
334 'upload_date': '20111209',
335 'uploader': 'Birdman',
881dbc86
S
336 'track': 'Y.U. MAD',
337 'artist': 'Birdman',
338 'genre': 'Rap/Hip-Hop',
e0da32df
S
339 },
340 'expected_warnings': ['Unable to download SMIL file'],
e0da32df
S
341 }, {
342 'url': 'http://www.vevo.com/watch/genre/rock?index=0',
343 'only_matching': True,
344 }]
345
346 def _real_extract(self, url):
e2bd301c
S
347 mobj = re.match(self._VALID_URL, url)
348 playlist_id = mobj.group('id')
349 playlist_kind = mobj.group('kind')
e0da32df
S
350
351 webpage = self._download_webpage(url, playlist_id)
352
353 qs = compat_urlparse.parse_qs(compat_urlparse.urlparse(url).query)
354 index = qs.get('index', [None])[0]
355
356 if index:
357 video_id = self._search_regex(
358 r'<meta[^>]+content=(["\'])vevo://video/(?P<id>.+?)\1[^>]*>',
359 webpage, 'video id', default=None, group='id')
360 if video_id:
361 return self.url_result('vevo:%s' % video_id, VevoIE.ie_key())
362
9d0c08a0 363 playlists = self._extract_json(webpage, playlist_id)['default']['%ss' % playlist_kind]
e0da32df 364
e2bd301c
S
365 playlist = (list(playlists.values())[0]
366 if playlist_kind == 'playlist' else playlists[playlist_id])
e0da32df
S
367
368 entries = [
369 self.url_result('vevo:%s' % src, VevoIE.ie_key())
370 for src in playlist['isrcs']]
371
372 return self.playlist_result(
78a3ff33 373 entries, playlist.get('playlistId') or playlist_id,
e0da32df 374 playlist.get('name'), playlist.get('description'))