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