]> jfr.im git - yt-dlp.git/blob - youtube_dl/extractor/vevo.py
Rename upload_timestamp to timestamp
[yt-dlp.git] / youtube_dl / extractor / vevo.py
1 from __future__ import unicode_literals
2
3 import re
4 import xml.etree.ElementTree
5
6 from .common import InfoExtractor
7 from ..utils import (
8 compat_HTTPError,
9 ExtractorError,
10 )
11
12
13 class VevoIE(InfoExtractor):
14 """
15 Accepts urls from vevo.com or in the format 'vevo:{id}'
16 (currently used by MTVIE)
17 """
18 _VALID_URL = r'''(?x)
19 (?:https?://www\.vevo\.com/watch/(?:[^/]+/[^/]+/)?|
20 https?://cache\.vevo\.com/m/html/embed\.html\?video=|
21 https?://videoplayer\.vevo\.com/embed/embedded\?videoId=|
22 vevo:)
23 (?P<id>[^&?#]+)'''
24 _TESTS = [{
25 'url': 'http://www.vevo.com/watch/hurts/somebody-to-die-for/GB1101300280',
26 "md5": "06bea460acb744eab74a9d7dcb4bfd61",
27 'info_dict': {
28 'id': 'GB1101300280',
29 'ext': 'mp4',
30 "upload_date": "20130624",
31 "uploader": "Hurts",
32 "title": "Somebody to Die For",
33 "duration": 230.12,
34 "width": 1920,
35 "height": 1080,
36 'timestamp': 1372057200,
37 }
38 }, {
39 'note': 'v3 SMIL format',
40 'url': 'http://www.vevo.com/watch/cassadee-pope/i-wish-i-could-break-your-heart/USUV71302923',
41 'md5': '893ec0e0d4426a1d96c01de8f2bdff58',
42 'info_dict': {
43 'id': 'USUV71302923',
44 'ext': 'mp4',
45 'upload_date': '20140219',
46 'uploader': 'Cassadee Pope',
47 'title': 'I Wish I Could Break Your Heart',
48 'duration': 226.101,
49 'age_limit': 0,
50 'timestamp': 1392796919,
51 }
52 }, {
53 'note': 'Age-limited video',
54 'url': 'https://www.vevo.com/watch/justin-timberlake/tunnel-vision-explicit/USRV81300282',
55 'info_dict': {
56 'id': 'USRV81300282',
57 'ext': 'mp4',
58 'age_limit': 18,
59 'title': 'Tunnel Vision (Explicit)',
60 'uploader': 'Justin Timberlake',
61 'upload_date': '20130704',
62 'timestamp': 1372906800,
63 },
64 'params': {
65 'skip_download': 'true',
66 }
67 }]
68 _SMIL_BASE_URL = 'http://smil.lvl3.vevo.com/'
69
70 def _formats_from_json(self, video_info):
71 last_version = {'version': -1}
72 for version in video_info['videoVersions']:
73 # These are the HTTP downloads, other types are for different manifests
74 if version['sourceType'] == 2:
75 if version['version'] > last_version['version']:
76 last_version = version
77 if last_version['version'] == -1:
78 raise ExtractorError('Unable to extract last version of the video')
79
80 renditions = xml.etree.ElementTree.fromstring(last_version['data'])
81 formats = []
82 # Already sorted from worst to best quality
83 for rend in renditions.findall('rendition'):
84 attr = rend.attrib
85 format_note = '%(videoCodec)s@%(videoBitrate)4sk, %(audioCodec)s@%(audioBitrate)3sk' % attr
86 formats.append({
87 'url': attr['url'],
88 'format_id': attr['name'],
89 'format_note': format_note,
90 'height': int(attr['frameheight']),
91 'width': int(attr['frameWidth']),
92 })
93 return formats
94
95 def _formats_from_smil(self, smil_xml):
96 formats = []
97 smil_doc = xml.etree.ElementTree.fromstring(smil_xml.encode('utf-8'))
98 els = smil_doc.findall('.//{http://www.w3.org/2001/SMIL20/Language}video')
99 for el in els:
100 src = el.attrib['src']
101 m = re.match(r'''(?xi)
102 (?P<ext>[a-z0-9]+):
103 (?P<path>
104 [/a-z0-9]+ # The directory and main part of the URL
105 _(?P<cbr>[0-9]+)k
106 _(?P<width>[0-9]+)x(?P<height>[0-9]+)
107 _(?P<vcodec>[a-z0-9]+)
108 _(?P<vbr>[0-9]+)
109 _(?P<acodec>[a-z0-9]+)
110 _(?P<abr>[0-9]+)
111 \.[a-z0-9]+ # File extension
112 )''', src)
113 if not m:
114 continue
115
116 format_url = self._SMIL_BASE_URL + m.group('path')
117 formats.append({
118 'url': format_url,
119 'format_id': 'SMIL_' + m.group('cbr'),
120 'vcodec': m.group('vcodec'),
121 'acodec': m.group('acodec'),
122 'vbr': int(m.group('vbr')),
123 'abr': int(m.group('abr')),
124 'ext': m.group('ext'),
125 'width': int(m.group('width')),
126 'height': int(m.group('height')),
127 })
128 return formats
129
130 def _real_extract(self, url):
131 mobj = re.match(self._VALID_URL, url)
132 video_id = mobj.group('id')
133
134 json_url = 'http://videoplayer.vevo.com/VideoService/AuthenticateVideo?isrc=%s' % video_id
135 video_info = self._download_json(json_url, video_id)['video']
136
137 formats = self._formats_from_json(video_info)
138
139 is_explicit = video_info.get('isExplicit')
140 if is_explicit is True:
141 age_limit = 18
142 elif is_explicit is False:
143 age_limit = 0
144 else:
145 age_limit = None
146
147 # Download SMIL
148 smil_blocks = sorted((
149 f for f in video_info['videoVersions']
150 if f['sourceType'] == 13),
151 key=lambda f: f['version'])
152
153 smil_url = '%s/Video/V2/VFILE/%s/%sr.smil' % (
154 self._SMIL_BASE_URL, video_id, video_id.lower())
155 if smil_blocks:
156 smil_url_m = self._search_regex(
157 r'url="([^"]+)"', smil_blocks[-1]['data'], 'SMIL URL',
158 fatal=False)
159 if smil_url_m is not None:
160 smil_url = smil_url_m
161
162 try:
163 smil_xml = self._download_webpage(smil_url, video_id,
164 'Downloading SMIL info')
165 formats.extend(self._formats_from_smil(smil_xml))
166 except ExtractorError as ee:
167 if not isinstance(ee.cause, compat_HTTPError):
168 raise
169 self._downloader.report_warning(
170 'Cannot download SMIL information, falling back to JSON ..')
171
172 timestamp_ms = int(self._search_regex(
173 r'/Date\((\d+)\)/', video_info['launchDate'], 'launch date'))
174
175 return {
176 'id': video_id,
177 'title': video_info['title'],
178 'formats': formats,
179 'thumbnail': video_info['imageUrl'],
180 'timestamp': timestamp_ms // 1000,
181 'uploader': video_info['mainArtists'][0]['artistName'],
182 'duration': video_info['duration'],
183 'age_limit': age_limit,
184 }