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