[vevo] Allow video info to fail in tests
[youtube-dl] / 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 (
7     compat_etree_fromstring,
8     compat_str,
9     compat_urlparse,
10 )
11 from ..utils import (
12     ExtractorError,
13     int_or_none,
14     sanitized_Request,
15     parse_iso8601,
16 )
17
18
19 class 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
28 class VevoIE(VevoBaseIE):
29     '''
30     Accepts urls from vevo.com or in the format 'vevo:{id}'
31     (currently used by MTVIE and MySpaceIE)
32     '''
33     _VALID_URL = r'''(?x)
34         (?:https?://(?:www\.)?vevo\.com/watch/(?!playlist|genre)(?:[^/]+/(?:[^/]+/)?)?|
35            https?://cache\.vevo\.com/m/html/embed\.html\?video=|
36            https?://videoplayer\.vevo\.com/embed/embedded\?videoId=|
37            vevo:)
38         (?P<id>[^&?#]+)'''
39
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     _SMIL_BASE_URL = 'http://smil.lvl3.vevo.com'
144     _SOURCE_TYPES = {
145         0: 'youtube',
146         1: 'brightcove',
147         2: 'http',
148         3: 'hls_ios',
149         4: 'hls',
150         5: 'smil',  # http
151         7: 'f4m_cc',
152         8: 'f4m_ak',
153         9: 'f4m_l3',
154         10: 'ism',
155         13: 'smil',  # rtmp
156         18: 'dash',
157     }
158     _VERSIONS = {
159         0: 'youtube',  # only in AuthenticateVideo videoVersions
160         1: 'level3',
161         2: 'akamai',
162         3: 'level3',
163         4: 'amazon',
164     }
165
166     def _parse_smil_formats(self, smil, smil_url, video_id, namespace=None, f4m_params=None, transform_rtmp_url=None):
167         formats = []
168         els = smil.findall('.//{http://www.w3.org/2001/SMIL20/Language}video')
169         for el in els:
170             src = el.attrib['src']
171             m = re.match(r'''(?xi)
172                 (?P<ext>[a-z0-9]+):
173                 (?P<path>
174                     [/a-z0-9]+     # The directory and main part of the URL
175                     _(?P<tbr>[0-9]+)k
176                     _(?P<width>[0-9]+)x(?P<height>[0-9]+)
177                     _(?P<vcodec>[a-z0-9]+)
178                     _(?P<vbr>[0-9]+)
179                     _(?P<acodec>[a-z0-9]+)
180                     _(?P<abr>[0-9]+)
181                     \.[a-z0-9]+  # File extension
182                 )''', src)
183             if not m:
184                 continue
185
186             format_url = self._SMIL_BASE_URL + m.group('path')
187             formats.append({
188                 'url': format_url,
189                 'format_id': 'smil_' + m.group('tbr'),
190                 'vcodec': m.group('vcodec'),
191                 'acodec': m.group('acodec'),
192                 'tbr': int(m.group('tbr')),
193                 'vbr': int(m.group('vbr')),
194                 'abr': int(m.group('abr')),
195                 'ext': m.group('ext'),
196                 'width': int(m.group('width')),
197                 'height': int(m.group('height')),
198             })
199         return formats
200
201     def _initialize_api(self, video_id):
202         req = sanitized_Request(
203             'http://www.vevo.com/auth', data=b'')
204         webpage = self._download_webpage(
205             req, None,
206             note='Retrieving oauth token',
207             errnote='Unable to retrieve oauth token')
208
209         if 'THIS PAGE IS CURRENTLY UNAVAILABLE IN YOUR REGION' in webpage:
210             self.raise_geo_restricted(
211                 '%s said: This page is currently unavailable in your region' % self.IE_NAME)
212
213         auth_info = self._parse_json(webpage, video_id)
214         self._api_url_template = self.http_scheme() + '//apiv2.vevo.com/%s?token=' + auth_info['access_token']
215
216     def _call_api(self, path, *args, **kwargs):
217         return self._download_json(self._api_url_template % path, *args, **kwargs)
218
219     def _real_extract(self, url):
220         video_id = self._match_id(url)
221
222         json_url = 'http://api.vevo.com/VideoService/AuthenticateVideo?isrc=%s' % video_id
223         response = self._download_json(
224             json_url, video_id, 'Downloading video info',
225             'Unable to download info', fatal=False) or {}
226         video_info = response.get('video') or {}
227         artist = None
228         featured_artist = None
229         uploader = None
230         view_count = None
231         formats = []
232
233         if not video_info:
234             try:
235                 self._initialize_api(video_id)
236             except ExtractorError:
237                 ytid = response.get('errorInfo', {}).get('ytid')
238                 if ytid:
239                     self.report_warning(
240                         'Video is geoblocked, trying with the YouTube video %s' % ytid)
241                     return self.url_result(ytid, 'Youtube', ytid)
242
243                 raise
244
245             video_info = self._call_api(
246                 'video/%s' % video_id, video_id, 'Downloading api video info',
247                 'Failed to download video info')
248
249             video_versions = self._call_api(
250                 'video/%s/streams' % video_id, video_id,
251                 'Downloading video versions info',
252                 'Failed to download video versions info',
253                 fatal=False)
254
255             # Some videos are only available via webpage (e.g.
256             # https://github.com/rg3/youtube-dl/issues/9366)
257             if not video_versions:
258                 webpage = self._download_webpage(url, video_id)
259                 video_versions = self._extract_json(webpage, video_id, 'streams')[video_id][0]
260
261             timestamp = parse_iso8601(video_info.get('releaseDate'))
262             artists = video_info.get('artists')
263             for curr_artist in artists:
264                 if curr_artist.get('role') == 'Featured':
265                     featured_artist = curr_artist['name']
266                 else:
267                     artist = uploader = curr_artist['name']
268             view_count = int_or_none(video_info.get('views', {}).get('total'))
269
270             for video_version in video_versions:
271                 version = self._VERSIONS.get(video_version['version'])
272                 version_url = video_version.get('url')
273                 if not version_url:
274                     continue
275
276                 if '.ism' in version_url:
277                     continue
278                 elif '.mpd' in version_url:
279                     formats.extend(self._extract_mpd_formats(
280                         version_url, video_id, mpd_id='dash-%s' % version,
281                         note='Downloading %s MPD information' % version,
282                         errnote='Failed to download %s MPD information' % version,
283                         fatal=False))
284                 elif '.m3u8' in version_url:
285                     formats.extend(self._extract_m3u8_formats(
286                         version_url, video_id, 'mp4', 'm3u8_native',
287                         m3u8_id='hls-%s' % version,
288                         note='Downloading %s m3u8 information' % version,
289                         errnote='Failed to download %s m3u8 information' % version,
290                         fatal=False))
291                 else:
292                     m = re.search(r'''(?xi)
293                         _(?P<width>[0-9]+)x(?P<height>[0-9]+)
294                         _(?P<vcodec>[a-z0-9]+)
295                         _(?P<vbr>[0-9]+)
296                         _(?P<acodec>[a-z0-9]+)
297                         _(?P<abr>[0-9]+)
298                         \.(?P<ext>[a-z0-9]+)''', version_url)
299                     if not m:
300                         continue
301
302                     formats.append({
303                         'url': version_url,
304                         'format_id': 'http-%s-%s' % (version, video_version['quality']),
305                         'vcodec': m.group('vcodec'),
306                         'acodec': m.group('acodec'),
307                         'vbr': int(m.group('vbr')),
308                         'abr': int(m.group('abr')),
309                         'ext': m.group('ext'),
310                         'width': int(m.group('width')),
311                         'height': int(m.group('height')),
312                     })
313         else:
314             timestamp = int_or_none(self._search_regex(
315                 r'/Date\((\d+)\)/',
316                 video_info['releaseDate'], 'release date', fatal=False),
317                 scale=1000)
318             artists = video_info.get('mainArtists')
319             if artists:
320                 artist = uploader = artists[0]['artistName']
321
322             featured_artists = video_info.get('featuredArtists')
323             if featured_artists:
324                 featured_artist = featured_artists[0]['artistName']
325
326             smil_parsed = False
327             for video_version in video_info['videoVersions']:
328                 version = self._VERSIONS.get(video_version['version'])
329                 if version == 'youtube':
330                     continue
331                 else:
332                     source_type = self._SOURCE_TYPES.get(video_version['sourceType'])
333                     renditions = compat_etree_fromstring(video_version['data'])
334                     if source_type == 'http':
335                         for rend in renditions.findall('rendition'):
336                             attr = rend.attrib
337                             formats.append({
338                                 'url': attr['url'],
339                                 'format_id': 'http-%s-%s' % (version, attr['name']),
340                                 'height': int_or_none(attr.get('frameheight')),
341                                 'width': int_or_none(attr.get('frameWidth')),
342                                 'tbr': int_or_none(attr.get('totalBitrate')),
343                                 'vbr': int_or_none(attr.get('videoBitrate')),
344                                 'abr': int_or_none(attr.get('audioBitrate')),
345                                 'vcodec': attr.get('videoCodec'),
346                                 'acodec': attr.get('audioCodec'),
347                             })
348                     elif source_type == 'hls':
349                         formats.extend(self._extract_m3u8_formats(
350                             renditions.find('rendition').attrib['url'], video_id,
351                             'mp4', 'm3u8_native', m3u8_id='hls-%s' % version,
352                             note='Downloading %s m3u8 information' % version,
353                             errnote='Failed to download %s m3u8 information' % version,
354                             fatal=False))
355                     elif source_type == 'smil' and version == 'level3' and not smil_parsed:
356                         formats.extend(self._extract_smil_formats(
357                             renditions.find('rendition').attrib['url'], video_id, False))
358                         smil_parsed = True
359         self._sort_formats(formats)
360
361         track = video_info['title']
362         if featured_artist:
363             artist = '%s ft. %s' % (artist, featured_artist)
364         title = '%s - %s' % (artist, track) if artist else track
365
366         genres = video_info.get('genres')
367         genre = (
368             genres[0] if genres and isinstance(genres, list) and
369             isinstance(genres[0], compat_str) else None)
370
371         is_explicit = video_info.get('isExplicit')
372         if is_explicit is True:
373             age_limit = 18
374         elif is_explicit is False:
375             age_limit = 0
376         else:
377             age_limit = None
378
379         duration = video_info.get('duration')
380
381         return {
382             'id': video_id,
383             'title': title,
384             'formats': formats,
385             'thumbnail': video_info.get('imageUrl') or video_info.get('thumbnailUrl'),
386             'timestamp': timestamp,
387             'uploader': uploader,
388             'duration': duration,
389             'view_count': view_count,
390             'age_limit': age_limit,
391             'track': track,
392             'artist': uploader,
393             'genre': genre,
394         }
395
396
397 class VevoPlaylistIE(VevoBaseIE):
398     _VALID_URL = r'https?://(?:www\.)?vevo\.com/watch/(?P<kind>playlist|genre)/(?P<id>[^/?#&]+)'
399
400     _TESTS = [{
401         'url': 'http://www.vevo.com/watch/playlist/dadbf4e7-b99f-4184-9670-6f0e547b6a29',
402         'info_dict': {
403             'id': 'dadbf4e7-b99f-4184-9670-6f0e547b6a29',
404             'title': 'Best-Of: Birdman',
405         },
406         'playlist_count': 10,
407     }, {
408         'url': 'http://www.vevo.com/watch/genre/rock',
409         'info_dict': {
410             'id': 'rock',
411             'title': 'Rock',
412         },
413         'playlist_count': 20,
414     }, {
415         'url': 'http://www.vevo.com/watch/playlist/dadbf4e7-b99f-4184-9670-6f0e547b6a29?index=0',
416         'md5': '32dcdfddddf9ec6917fc88ca26d36282',
417         'info_dict': {
418             'id': 'USCMV1100073',
419             'ext': 'mp4',
420             'title': 'Birdman - Y.U. MAD',
421             'timestamp': 1323417600,
422             'upload_date': '20111209',
423             'uploader': 'Birdman',
424             'track': 'Y.U. MAD',
425             'artist': 'Birdman',
426             'genre': 'Rap/Hip-Hop',
427         },
428         'expected_warnings': ['Unable to download SMIL file'],
429     }, {
430         'url': 'http://www.vevo.com/watch/genre/rock?index=0',
431         'only_matching': True,
432     }]
433
434     def _real_extract(self, url):
435         mobj = re.match(self._VALID_URL, url)
436         playlist_id = mobj.group('id')
437         playlist_kind = mobj.group('kind')
438
439         webpage = self._download_webpage(url, playlist_id)
440
441         qs = compat_urlparse.parse_qs(compat_urlparse.urlparse(url).query)
442         index = qs.get('index', [None])[0]
443
444         if index:
445             video_id = self._search_regex(
446                 r'<meta[^>]+content=(["\'])vevo://video/(?P<id>.+?)\1[^>]*>',
447                 webpage, 'video id', default=None, group='id')
448             if video_id:
449                 return self.url_result('vevo:%s' % video_id, VevoIE.ie_key())
450
451         playlists = self._extract_json(webpage, playlist_id, '%ss' % playlist_kind)
452
453         playlist = (list(playlists.values())[0]
454                     if playlist_kind == 'playlist' else playlists[playlist_id])
455
456         entries = [
457             self.url_result('vevo:%s' % src, VevoIE.ie_key())
458             for src in playlist['isrcs']]
459
460         return self.playlist_result(
461             entries, playlist.get('playlistId') or playlist_id,
462             playlist.get('name'), playlist.get('description'))