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