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.

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