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.

208 lines
7.4 KiB

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