]>
Commit | Line | Data |
---|---|---|
45d7bc2f JMF |
1 | from __future__ import unicode_literals |
2 | ||
70d1924f | 3 | import re |
70d1924f JMF |
4 | |
5 | from .common import InfoExtractor | |
2975fe1a | 6 | from ..compat import compat_etree_fromstring |
1cc79574 | 7 | from ..utils import ( |
70d1924f | 8 | ExtractorError, |
7d3d06a1 | 9 | int_or_none, |
9165d6ba | 10 | sanitized_Request, |
11 | parse_iso8601, | |
70d1924f JMF |
12 | ) |
13 | ||
88bd97e3 | 14 | |
70d1924f | 15 | class VevoIE(InfoExtractor): |
2975fe1a | 16 | ''' |
0577177e | 17 | Accepts urls from vevo.com or in the format 'vevo:{id}' |
3266f0c6 | 18 | (currently used by MTVIE and MySpaceIE) |
2975fe1a | 19 | ''' |
f25571ff | 20 | _VALID_URL = r'''(?x) |
91683085 | 21 | (?:https?://www\.vevo\.com/watch/(?:[^/]+/(?:[^/]+/)?)?| |
f25571ff | 22 | https?://cache\.vevo\.com/m/html/embed\.html\?video=| |
ebce53b3 | 23 | https?://videoplayer\.vevo\.com/embed/embedded\?videoId=| |
f25571ff PH |
24 | vevo:) |
25 | (?P<id>[^&?#]+)''' | |
fd5e6f7e | 26 | |
72321ead | 27 | _TESTS = [{ |
45d7bc2f | 28 | 'url': 'http://www.vevo.com/watch/hurts/somebody-to-die-for/GB1101300280', |
682f8c43 | 29 | 'md5': '95ee28ee45e70130e3ab02b0f579ae23', |
45d7bc2f | 30 | 'info_dict': { |
682f8c43 | 31 | 'id': 'GB1101300280', |
27579b9e | 32 | 'ext': 'mp4', |
682f8c43 | 33 | 'title': 'Somebody to Die For', |
2975fe1a | 34 | 'upload_date': '20130624', |
682f8c43 | 35 | 'uploader': 'Hurts', |
36 | 'timestamp': 1372057200, | |
2975fe1a | 37 | }, |
27579b9e PH |
38 | }, { |
39 | 'note': 'v3 SMIL format', | |
40 | 'url': 'http://www.vevo.com/watch/cassadee-pope/i-wish-i-could-break-your-heart/USUV71302923', | |
682f8c43 | 41 | 'md5': 'f6ab09b034f8c22969020b042e5ac7fc', |
27579b9e | 42 | 'info_dict': { |
682f8c43 | 43 | 'id': 'USUV71302923', |
27579b9e | 44 | 'ext': 'mp4', |
682f8c43 | 45 | 'title': 'I Wish I Could Break Your Heart', |
27579b9e | 46 | 'upload_date': '20140219', |
682f8c43 | 47 | 'uploader': 'Cassadee Pope', |
48 | 'timestamp': 1392796919, | |
2975fe1a | 49 | }, |
6cadf8c8 PH |
50 | }, { |
51 | 'note': 'Age-limited video', | |
52 | 'url': 'https://www.vevo.com/watch/justin-timberlake/tunnel-vision-explicit/USRV81300282', | |
53 | 'info_dict': { | |
682f8c43 | 54 | 'id': 'USRV81300282', |
6cadf8c8 | 55 | 'ext': 'mp4', |
682f8c43 | 56 | 'title': 'Tunnel Vision (Explicit)', |
2975fe1a | 57 | 'upload_date': '20130703', |
682f8c43 | 58 | 'age_limit': 18, |
59 | 'uploader': 'Justin Timberlake', | |
60 | 'timestamp': 1372888800, | |
2975fe1a | 61 | }, |
ff51983e YCH |
62 | }, { |
63 | 'note': 'No video_info', | |
64 | 'url': 'http://www.vevo.com/watch/k-camp-1/Till-I-Die/USUV71503000', | |
682f8c43 | 65 | 'md5': '8b83cc492d72fc9cf74a02acee7dc1b0', |
ff51983e | 66 | 'info_dict': { |
682f8c43 | 67 | 'id': 'USUV71503000', |
ff51983e | 68 | 'ext': 'mp4', |
682f8c43 | 69 | 'title': 'Till I Die', |
9165d6ba | 70 | 'upload_date': '20151207', |
682f8c43 | 71 | 'age_limit': 18, |
72 | 'uploader': 'K Camp', | |
73 | 'timestamp': 1449468000, | |
ff51983e | 74 | }, |
72321ead | 75 | }] |
2975fe1a | 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 = { | |
9165d6ba | 92 | 0: 'youtube', # only in AuthenticateVideo videoVersions |
2975fe1a | 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): | |
72321ead | 100 | formats = [] |
2975fe1a | 101 | els = smil.findall('.//{http://www.w3.org/2001/SMIL20/Language}video') |
72321ead PH |
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 | |
2975fe1a | 108 | _(?P<tbr>[0-9]+)k |
72321ead PH |
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') | |
72321ead PH |
120 | formats.append({ |
121 | 'url': format_url, | |
2975fe1a | 122 | 'format_id': 'smil_' + m.group('tbr'), |
91c7271a PH |
123 | 'vcodec': m.group('vcodec'), |
124 | 'acodec': m.group('acodec'), | |
2975fe1a | 125 | 'tbr': int(m.group('tbr')), |
91c7271a PH |
126 | 'vbr': int(m.group('vbr')), |
127 | 'abr': int(m.group('abr')), | |
72321ead PH |
128 | 'ext': m.group('ext'), |
129 | 'width': int(m.group('width')), | |
130 | 'height': int(m.group('height')), | |
131 | }) | |
132 | return formats | |
133 | ||
682f8c43 | 134 | def _initialize_api(self, video_id): |
9165d6ba | 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: | |
682f8c43 | 143 | raise ExtractorError( |
144 | '%s said: This page is currently unavailable in your region.' % self.IE_NAME, expected=True) | |
9165d6ba | 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 | ||
72321ead | 152 | def _real_extract(self, url): |
4b942883 | 153 | video_id = self._match_id(url) |
72321ead PH |
154 | |
155 | json_url = 'http://videoplayer.vevo.com/VideoService/AuthenticateVideo?isrc=%s' % video_id | |
682f8c43 | 156 | response = self._download_json( |
157 | json_url, video_id, 'Downloading video info', 'Unable to download info') | |
9165d6ba | 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 = [] | |
e97e53ee | 164 | |
ff51983e | 165 | if not video_info: |
2975fe1a | 166 | if response.get('statusCode') != 909: |
682f8c43 | 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 | ||
2975fe1a | 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 | ||
682f8c43 | 178 | self._initialize_api(video_id) |
9165d6ba | 179 | video_info = self._call_api( |
180 | 'video/%s' % video_id, video_id, 'Downloading api video info', | |
181 | 'Failed to download video info') | |
ff51983e | 182 | |
9165d6ba | 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: | |
682f8c43 | 198 | continue |
9165d6ba | 199 | |
5976e7ab | 200 | if '.ism' in version_url: |
9165d6ba | 201 | continue |
5976e7ab | 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)) | |
9165d6ba | 208 | elif '.m3u8' in version_url: |
2975fe1a | 209 | formats.extend(self._extract_m3u8_formats( |
9165d6ba | 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': | |
682f8c43 | 250 | continue |
9165d6ba | 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)) | |
682f8c43 | 275 | elif source_type == 'smil' and version == 'level3' and not smil_parsed: |
9165d6ba | 276 | formats.extend(self._extract_smil_formats( |
277 | renditions.find('rendition').attrib['url'], video_id, False)) | |
278 | smil_parsed = True | |
2975fe1a | 279 | self._sort_formats(formats) |
27579b9e | 280 | |
9165d6ba | 281 | title = video_info['title'] |
282 | ||
6cadf8c8 PH |
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 | ||
9165d6ba | 291 | duration = video_info.get('duration') |
9d2ecdbc | 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'), |
ff51983e | 298 | 'timestamp': timestamp, |
9165d6ba | 299 | 'uploader': uploader, |
ff51983e | 300 | 'duration': duration, |
9165d6ba | 301 | 'view_count': view_count, |
6cadf8c8 | 302 | 'age_limit': age_limit, |
88bd97e3 | 303 | } |