You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

182 lines
6.5 KiB

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