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.

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