]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/vevo.py
Imported Upstream version 2013.12.23
[youtubedl] / youtube_dl / extractor / vevo.py
1 import re
2 import json
3 import xml.etree.ElementTree
4 import datetime
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 u'url': u'http://www.vevo.com/watch/hurts/somebody-to-die-for/GB1101300280',
26 u'file': u'GB1101300280.mp4',
27 u"md5": u"06bea460acb744eab74a9d7dcb4bfd61",
28 u'info_dict': {
29 u"upload_date": u"20130624",
30 u"uploader": u"Hurts",
31 u"title": u"Somebody to Die For",
32 u"duration": 230.12,
33 u"width": 1920,
34 u"height": 1080,
35 }
36 }]
37 _SMIL_BASE_URL = 'http://smil.lvl3.vevo.com/'
38
39 def _formats_from_json(self, video_info):
40 last_version = {'version': -1}
41 for version in video_info['videoVersions']:
42 # These are the HTTP downloads, other types are for different manifests
43 if version['sourceType'] == 2:
44 if version['version'] > last_version['version']:
45 last_version = version
46 if last_version['version'] == -1:
47 raise ExtractorError(u'Unable to extract last version of the video')
48
49 renditions = xml.etree.ElementTree.fromstring(last_version['data'])
50 formats = []
51 # Already sorted from worst to best quality
52 for rend in renditions.findall('rendition'):
53 attr = rend.attrib
54 format_note = '%(videoCodec)s@%(videoBitrate)4sk, %(audioCodec)s@%(audioBitrate)3sk' % attr
55 formats.append({
56 'url': attr['url'],
57 'format_id': attr['name'],
58 'format_note': format_note,
59 'height': int(attr['frameheight']),
60 'width': int(attr['frameWidth']),
61 })
62 return formats
63
64 def _formats_from_smil(self, smil_xml):
65 formats = []
66 smil_doc = xml.etree.ElementTree.fromstring(smil_xml.encode('utf-8'))
67 els = smil_doc.findall('.//{http://www.w3.org/2001/SMIL20/Language}video')
68 for el in els:
69 src = el.attrib['src']
70 m = re.match(r'''(?xi)
71 (?P<ext>[a-z0-9]+):
72 (?P<path>
73 [/a-z0-9]+ # The directory and main part of the URL
74 _(?P<cbr>[0-9]+)k
75 _(?P<width>[0-9]+)x(?P<height>[0-9]+)
76 _(?P<vcodec>[a-z0-9]+)
77 _(?P<vbr>[0-9]+)
78 _(?P<acodec>[a-z0-9]+)
79 _(?P<abr>[0-9]+)
80 \.[a-z0-9]+ # File extension
81 )''', src)
82 if not m:
83 continue
84
85 format_url = self._SMIL_BASE_URL + m.group('path')
86 formats.append({
87 'url': format_url,
88 'format_id': u'SMIL_' + m.group('cbr'),
89 'vcodec': m.group('vcodec'),
90 'acodec': m.group('acodec'),
91 'vbr': int(m.group('vbr')),
92 'abr': int(m.group('abr')),
93 'ext': m.group('ext'),
94 'width': int(m.group('width')),
95 'height': int(m.group('height')),
96 })
97 return formats
98
99 def _real_extract(self, url):
100 mobj = re.match(self._VALID_URL, url)
101 video_id = mobj.group('id')
102
103 json_url = 'http://videoplayer.vevo.com/VideoService/AuthenticateVideo?isrc=%s' % video_id
104 info_json = self._download_webpage(json_url, video_id, u'Downloading json info')
105 video_info = json.loads(info_json)['video']
106
107 formats = self._formats_from_json(video_info)
108 try:
109 smil_url = '%s/Video/V2/VFILE/%s/%sr.smil' % (
110 self._SMIL_BASE_URL, video_id, video_id.lower())
111 smil_xml = self._download_webpage(smil_url, video_id,
112 u'Downloading SMIL info')
113 formats.extend(self._formats_from_smil(smil_xml))
114 except ExtractorError as ee:
115 if not isinstance(ee.cause, compat_HTTPError):
116 raise
117 self._downloader.report_warning(
118 u'Cannot download SMIL information, falling back to JSON ..')
119
120 timestamp_ms = int(self._search_regex(
121 r'/Date\((\d+)\)/', video_info['launchDate'], u'launch date'))
122 upload_date = datetime.datetime.fromtimestamp(timestamp_ms // 1000)
123 info = {
124 'id': video_id,
125 'title': video_info['title'],
126 'formats': formats,
127 'thumbnail': video_info['imageUrl'],
128 'upload_date': upload_date.strftime('%Y%m%d'),
129 'uploader': video_info['mainArtists'][0]['artistName'],
130 'duration': video_info['duration'],
131 }
132
133 return info