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