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.

206 lines
7.3 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_urllib_request,
  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": "95ee28ee45e70130e3ab02b0f579ae23",
  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': 'f6ab09b034f8c22969020b042e5ac7fc',
  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 _real_initialize(self):
  67. req = compat_urllib_request.Request(
  68. 'http://www.vevo.com/auth', data=b'')
  69. webpage = self._download_webpage(
  70. req, None,
  71. note='Retrieving oauth token',
  72. errnote='Unable to retrieve oauth token',
  73. fatal=False)
  74. if webpage is False:
  75. self._oauth_token = None
  76. else:
  77. self._oauth_token = self._search_regex(
  78. r'access_token":\s*"([^"]+)"',
  79. webpage, 'access token', fatal=False)
  80. def _formats_from_json(self, video_info):
  81. last_version = {'version': -1}
  82. for version in video_info['videoVersions']:
  83. # These are the HTTP downloads, other types are for different manifests
  84. if version['sourceType'] == 2:
  85. if version['version'] > last_version['version']:
  86. last_version = version
  87. if last_version['version'] == -1:
  88. raise ExtractorError('Unable to extract last version of the video')
  89. renditions = xml.etree.ElementTree.fromstring(last_version['data'])
  90. formats = []
  91. # Already sorted from worst to best quality
  92. for rend in renditions.findall('rendition'):
  93. attr = rend.attrib
  94. format_note = '%(videoCodec)s@%(videoBitrate)4sk, %(audioCodec)s@%(audioBitrate)3sk' % attr
  95. formats.append({
  96. 'url': attr['url'],
  97. 'format_id': attr['name'],
  98. 'format_note': format_note,
  99. 'height': int(attr['frameheight']),
  100. 'width': int(attr['frameWidth']),
  101. })
  102. return formats
  103. def _formats_from_smil(self, smil_xml):
  104. formats = []
  105. smil_doc = xml.etree.ElementTree.fromstring(smil_xml.encode('utf-8'))
  106. els = smil_doc.findall('.//{http://www.w3.org/2001/SMIL20/Language}video')
  107. for el in els:
  108. src = el.attrib['src']
  109. m = re.match(r'''(?xi)
  110. (?P<ext>[a-z0-9]+):
  111. (?P<path>
  112. [/a-z0-9]+ # The directory and main part of the URL
  113. _(?P<cbr>[0-9]+)k
  114. _(?P<width>[0-9]+)x(?P<height>[0-9]+)
  115. _(?P<vcodec>[a-z0-9]+)
  116. _(?P<vbr>[0-9]+)
  117. _(?P<acodec>[a-z0-9]+)
  118. _(?P<abr>[0-9]+)
  119. \.[a-z0-9]+ # File extension
  120. )''', src)
  121. if not m:
  122. continue
  123. format_url = self._SMIL_BASE_URL + m.group('path')
  124. formats.append({
  125. 'url': format_url,
  126. 'format_id': 'SMIL_' + m.group('cbr'),
  127. 'vcodec': m.group('vcodec'),
  128. 'acodec': m.group('acodec'),
  129. 'vbr': int(m.group('vbr')),
  130. 'abr': int(m.group('abr')),
  131. 'ext': m.group('ext'),
  132. 'width': int(m.group('width')),
  133. 'height': int(m.group('height')),
  134. })
  135. return formats
  136. def _download_api_formats(self, video_id):
  137. if not self._oauth_token:
  138. self._downloader.report_warning(
  139. 'No oauth token available, skipping API HLS download')
  140. return []
  141. api_url = 'https://apiv2.vevo.com/video/%s/streams/hls?token=%s' % (
  142. video_id, self._oauth_token)
  143. api_data = self._download_json(
  144. api_url, video_id,
  145. note='Downloading HLS formats',
  146. errnote='Failed to download HLS format list', fatal=False)
  147. if api_data is None:
  148. return []
  149. m3u8_url = api_data[0]['url']
  150. return self._extract_m3u8_formats(
  151. m3u8_url, video_id, entry_protocol='m3u8_native', ext='mp4',
  152. preference=0)
  153. def _real_extract(self, url):
  154. mobj = re.match(self._VALID_URL, url)
  155. video_id = mobj.group('id')
  156. json_url = 'http://videoplayer.vevo.com/VideoService/AuthenticateVideo?isrc=%s' % video_id
  157. response = self._download_json(json_url, video_id)
  158. video_info = response['video']
  159. if not video_info:
  160. if 'statusMessage' in response:
  161. raise ExtractorError('%s said: %s' % (self.IE_NAME, response['statusMessage']), expected=True)
  162. raise ExtractorError('Unable to extract videos')
  163. formats = self._formats_from_json(video_info)
  164. is_explicit = video_info.get('isExplicit')
  165. if is_explicit is True:
  166. age_limit = 18
  167. elif is_explicit is False:
  168. age_limit = 0
  169. else:
  170. age_limit = None
  171. # Download via HLS API
  172. formats.extend(self._download_api_formats(video_id))
  173. self._sort_formats(formats)
  174. timestamp_ms = int(self._search_regex(
  175. r'/Date\((\d+)\)/', video_info['launchDate'], 'launch date'))
  176. return {
  177. 'id': video_id,
  178. 'title': video_info['title'],
  179. 'formats': formats,
  180. 'thumbnail': video_info['imageUrl'],
  181. 'timestamp': timestamp_ms // 1000,
  182. 'uploader': video_info['mainArtists'][0]['artistName'],
  183. 'duration': video_info['duration'],
  184. 'age_limit': age_limit,
  185. }