]>
Commit | Line | Data |
---|---|---|
45d7bc2f JMF |
1 | from __future__ import unicode_literals |
2 | ||
70d1924f | 3 | import re |
88bd97e3 | 4 | import xml.etree.ElementTree |
70d1924f JMF |
5 | |
6 | from .common import InfoExtractor | |
1cc79574 | 7 | from ..compat import ( |
f0b5d6af | 8 | compat_urllib_request, |
1cc79574 PH |
9 | ) |
10 | from ..utils import ( | |
70d1924f | 11 | ExtractorError, |
7d3d06a1 | 12 | int_or_none, |
70d1924f JMF |
13 | ) |
14 | ||
88bd97e3 | 15 | |
70d1924f | 16 | class VevoIE(InfoExtractor): |
1c251cd9 | 17 | """ |
0577177e | 18 | Accepts urls from vevo.com or in the format 'vevo:{id}' |
3266f0c6 | 19 | (currently used by MTVIE and MySpaceIE) |
1c251cd9 | 20 | """ |
f25571ff | 21 | _VALID_URL = r'''(?x) |
91683085 | 22 | (?:https?://www\.vevo\.com/watch/(?:[^/]+/(?:[^/]+/)?)?| |
f25571ff | 23 | https?://cache\.vevo\.com/m/html/embed\.html\?video=| |
ebce53b3 | 24 | https?://videoplayer\.vevo\.com/embed/embedded\?videoId=| |
f25571ff PH |
25 | vevo:) |
26 | (?P<id>[^&?#]+)''' | |
fd5e6f7e | 27 | |
72321ead | 28 | _TESTS = [{ |
45d7bc2f | 29 | 'url': 'http://www.vevo.com/watch/hurts/somebody-to-die-for/GB1101300280', |
b686fc18 | 30 | "md5": "95ee28ee45e70130e3ab02b0f579ae23", |
45d7bc2f | 31 | 'info_dict': { |
27579b9e PH |
32 | 'id': 'GB1101300280', |
33 | 'ext': 'mp4', | |
45d7bc2f JMF |
34 | "upload_date": "20130624", |
35 | "uploader": "Hurts", | |
36 | "title": "Somebody to Die For", | |
37 | "duration": 230.12, | |
38 | "width": 1920, | |
39 | "height": 1080, | |
fd5e6f7e PH |
40 | # timestamp and upload_date are often incorrect; seem to change randomly |
41 | 'timestamp': int, | |
6f5ac90c | 42 | } |
27579b9e PH |
43 | }, { |
44 | 'note': 'v3 SMIL format', | |
45 | 'url': 'http://www.vevo.com/watch/cassadee-pope/i-wish-i-could-break-your-heart/USUV71302923', | |
b686fc18 | 46 | 'md5': 'f6ab09b034f8c22969020b042e5ac7fc', |
27579b9e PH |
47 | 'info_dict': { |
48 | 'id': 'USUV71302923', | |
49 | 'ext': 'mp4', | |
50 | 'upload_date': '20140219', | |
51 | 'uploader': 'Cassadee Pope', | |
52 | 'title': 'I Wish I Could Break Your Heart', | |
53 | 'duration': 226.101, | |
6cadf8c8 | 54 | 'age_limit': 0, |
fd5e6f7e | 55 | 'timestamp': int, |
6cadf8c8 PH |
56 | } |
57 | }, { | |
58 | 'note': 'Age-limited video', | |
59 | 'url': 'https://www.vevo.com/watch/justin-timberlake/tunnel-vision-explicit/USRV81300282', | |
60 | 'info_dict': { | |
61 | 'id': 'USRV81300282', | |
62 | 'ext': 'mp4', | |
63 | 'age_limit': 18, | |
64 | 'title': 'Tunnel Vision (Explicit)', | |
65 | 'uploader': 'Justin Timberlake', | |
aba77bbf PH |
66 | 'upload_date': 're:2013070[34]', |
67 | 'timestamp': int, | |
6cadf8c8 PH |
68 | }, |
69 | 'params': { | |
70 | 'skip_download': 'true', | |
27579b9e | 71 | } |
72321ead PH |
72 | }] |
73 | _SMIL_BASE_URL = 'http://smil.lvl3.vevo.com/' | |
70d1924f | 74 | |
f0b5d6af PH |
75 | def _real_initialize(self): |
76 | req = compat_urllib_request.Request( | |
77 | 'http://www.vevo.com/auth', data=b'') | |
78 | webpage = self._download_webpage( | |
79 | req, None, | |
80 | note='Retrieving oauth token', | |
81 | errnote='Unable to retrieve oauth token', | |
82 | fatal=False) | |
83 | if webpage is False: | |
84 | self._oauth_token = None | |
85 | else: | |
86 | self._oauth_token = self._search_regex( | |
87 | r'access_token":\s*"([^"]+)"', | |
88 | webpage, 'access token', fatal=False) | |
89 | ||
72321ead | 90 | def _formats_from_json(self, video_info): |
88bd97e3 JMF |
91 | last_version = {'version': -1} |
92 | for version in video_info['videoVersions']: | |
93 | # These are the HTTP downloads, other types are for different manifests | |
94 | if version['sourceType'] == 2: | |
95 | if version['version'] > last_version['version']: | |
96 | last_version = version | |
97 | if last_version['version'] == -1: | |
45d7bc2f | 98 | raise ExtractorError('Unable to extract last version of the video') |
88bd97e3 JMF |
99 | |
100 | renditions = xml.etree.ElementTree.fromstring(last_version['data']) | |
101 | formats = [] | |
102 | # Already sorted from worst to best quality | |
103 | for rend in renditions.findall('rendition'): | |
104 | attr = rend.attrib | |
72321ead | 105 | format_note = '%(videoCodec)s@%(videoBitrate)4sk, %(audioCodec)s@%(audioBitrate)3sk' % attr |
88bd97e3 | 106 | formats.append({ |
e54fd4b2 PH |
107 | 'url': attr['url'], |
108 | 'format_id': attr['name'], | |
109 | 'format_note': format_note, | |
88bd97e3 JMF |
110 | 'height': int(attr['frameheight']), |
111 | 'width': int(attr['frameWidth']), | |
112 | }) | |
72321ead PH |
113 | return formats |
114 | ||
115 | def _formats_from_smil(self, smil_xml): | |
116 | formats = [] | |
117 | smil_doc = xml.etree.ElementTree.fromstring(smil_xml.encode('utf-8')) | |
118 | els = smil_doc.findall('.//{http://www.w3.org/2001/SMIL20/Language}video') | |
119 | for el in els: | |
120 | src = el.attrib['src'] | |
121 | m = re.match(r'''(?xi) | |
122 | (?P<ext>[a-z0-9]+): | |
123 | (?P<path> | |
124 | [/a-z0-9]+ # The directory and main part of the URL | |
125 | _(?P<cbr>[0-9]+)k | |
126 | _(?P<width>[0-9]+)x(?P<height>[0-9]+) | |
127 | _(?P<vcodec>[a-z0-9]+) | |
128 | _(?P<vbr>[0-9]+) | |
129 | _(?P<acodec>[a-z0-9]+) | |
130 | _(?P<abr>[0-9]+) | |
131 | \.[a-z0-9]+ # File extension | |
132 | )''', src) | |
133 | if not m: | |
134 | continue | |
135 | ||
136 | format_url = self._SMIL_BASE_URL + m.group('path') | |
72321ead PH |
137 | formats.append({ |
138 | 'url': format_url, | |
45d7bc2f | 139 | 'format_id': 'SMIL_' + m.group('cbr'), |
91c7271a PH |
140 | 'vcodec': m.group('vcodec'), |
141 | 'acodec': m.group('acodec'), | |
142 | 'vbr': int(m.group('vbr')), | |
143 | 'abr': int(m.group('abr')), | |
72321ead PH |
144 | 'ext': m.group('ext'), |
145 | 'width': int(m.group('width')), | |
146 | 'height': int(m.group('height')), | |
147 | }) | |
148 | return formats | |
149 | ||
f0b5d6af PH |
150 | def _download_api_formats(self, video_id): |
151 | if not self._oauth_token: | |
152 | self._downloader.report_warning( | |
153 | 'No oauth token available, skipping API HLS download') | |
154 | return [] | |
155 | ||
156 | api_url = 'https://apiv2.vevo.com/video/%s/streams/hls?token=%s' % ( | |
157 | video_id, self._oauth_token) | |
158 | api_data = self._download_json( | |
159 | api_url, video_id, | |
160 | note='Downloading HLS formats', | |
161 | errnote='Failed to download HLS format list', fatal=False) | |
162 | if api_data is None: | |
163 | return [] | |
164 | ||
165 | m3u8_url = api_data[0]['url'] | |
166 | return self._extract_m3u8_formats( | |
167 | m3u8_url, video_id, entry_protocol='m3u8_native', ext='mp4', | |
168 | preference=0) | |
169 | ||
72321ead PH |
170 | def _real_extract(self, url): |
171 | mobj = re.match(self._VALID_URL, url) | |
172 | video_id = mobj.group('id') | |
173 | ||
174 | json_url = 'http://videoplayer.vevo.com/VideoService/AuthenticateVideo?isrc=%s' % video_id | |
e97e53ee S |
175 | response = self._download_json(json_url, video_id) |
176 | video_info = response['video'] | |
177 | ||
178 | if not video_info: | |
179 | if 'statusMessage' in response: | |
180 | raise ExtractorError('%s said: %s' % (self.IE_NAME, response['statusMessage']), expected=True) | |
181 | raise ExtractorError('Unable to extract videos') | |
72321ead PH |
182 | |
183 | formats = self._formats_from_json(video_info) | |
27579b9e | 184 | |
6cadf8c8 PH |
185 | is_explicit = video_info.get('isExplicit') |
186 | if is_explicit is True: | |
187 | age_limit = 18 | |
188 | elif is_explicit is False: | |
189 | age_limit = 0 | |
190 | else: | |
191 | age_limit = None | |
192 | ||
f0b5d6af PH |
193 | # Download via HLS API |
194 | formats.extend(self._download_api_formats(video_id)) | |
195 | ||
7d3d06a1 PH |
196 | # Download SMIL |
197 | smil_blocks = sorted(( | |
198 | f for f in video_info['videoVersions'] | |
199 | if f['sourceType'] == 13), | |
200 | key=lambda f: f['version']) | |
201 | smil_url = '%s/Video/V2/VFILE/%s/%sr.smil' % ( | |
202 | self._SMIL_BASE_URL, video_id, video_id.lower()) | |
203 | if smil_blocks: | |
204 | smil_url_m = self._search_regex( | |
205 | r'url="([^"]+)"', smil_blocks[-1]['data'], 'SMIL URL', | |
206 | default=None) | |
207 | if smil_url_m is not None: | |
208 | smil_url = smil_url_m | |
209 | if smil_url: | |
210 | smil_xml = self._download_webpage( | |
211 | smil_url, video_id, 'Downloading SMIL info', fatal=False) | |
212 | if smil_xml: | |
213 | formats.extend(self._formats_from_smil(smil_xml)) | |
214 | ||
bff74bdd | 215 | self._sort_formats(formats) |
7d3d06a1 PH |
216 | timestamp_ms = int_or_none(self._search_regex( |
217 | r'/Date\((\d+)\)/', | |
218 | video_info['launchDate'], 'launch date', fatal=False)) | |
9d2ecdbc | 219 | |
45d7bc2f | 220 | return { |
88bd97e3 JMF |
221 | 'id': video_id, |
222 | 'title': video_info['title'], | |
223 | 'formats': formats, | |
224 | 'thumbnail': video_info['imageUrl'], | |
955c4514 | 225 | 'timestamp': timestamp_ms // 1000, |
88bd97e3 JMF |
226 | 'uploader': video_info['mainArtists'][0]['artistName'], |
227 | 'duration': video_info['duration'], | |
6cadf8c8 | 228 | 'age_limit': age_limit, |
88bd97e3 | 229 | } |