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.

303 lines
12 KiB

11 years ago
11 years ago
11 years ago
11 years ago
  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..compat import compat_etree_fromstring
  5. from ..utils import (
  6. ExtractorError,
  7. int_or_none,
  8. sanitized_Request,
  9. parse_iso8601,
  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. 'title': 'Somebody to Die For',
  29. 'upload_date': '20130624',
  30. 'uploader': 'Hurts',
  31. 'timestamp': 1372057200,
  32. },
  33. }, {
  34. 'note': 'v3 SMIL format',
  35. 'url': 'http://www.vevo.com/watch/cassadee-pope/i-wish-i-could-break-your-heart/USUV71302923',
  36. 'md5': 'f6ab09b034f8c22969020b042e5ac7fc',
  37. 'info_dict': {
  38. 'id': 'USUV71302923',
  39. 'ext': 'mp4',
  40. 'title': 'I Wish I Could Break Your Heart',
  41. 'upload_date': '20140219',
  42. 'uploader': 'Cassadee Pope',
  43. 'timestamp': 1392796919,
  44. },
  45. }, {
  46. 'note': 'Age-limited video',
  47. 'url': 'https://www.vevo.com/watch/justin-timberlake/tunnel-vision-explicit/USRV81300282',
  48. 'info_dict': {
  49. 'id': 'USRV81300282',
  50. 'ext': 'mp4',
  51. 'title': 'Tunnel Vision (Explicit)',
  52. 'upload_date': '20130703',
  53. 'age_limit': 18,
  54. 'uploader': 'Justin Timberlake',
  55. 'timestamp': 1372888800,
  56. },
  57. }, {
  58. 'note': 'No video_info',
  59. 'url': 'http://www.vevo.com/watch/k-camp-1/Till-I-Die/USUV71503000',
  60. 'md5': '8b83cc492d72fc9cf74a02acee7dc1b0',
  61. 'info_dict': {
  62. 'id': 'USUV71503000',
  63. 'ext': 'mp4',
  64. 'title': 'Till I Die',
  65. 'upload_date': '20151207',
  66. 'age_limit': 18,
  67. 'uploader': 'K Camp',
  68. 'timestamp': 1449468000,
  69. },
  70. }]
  71. _SMIL_BASE_URL = 'http://smil.lvl3.vevo.com'
  72. _SOURCE_TYPES = {
  73. 0: 'youtube',
  74. 1: 'brightcove',
  75. 2: 'http',
  76. 3: 'hls_ios',
  77. 4: 'hls',
  78. 5: 'smil', # http
  79. 7: 'f4m_cc',
  80. 8: 'f4m_ak',
  81. 9: 'f4m_l3',
  82. 10: 'ism',
  83. 13: 'smil', # rtmp
  84. 18: 'dash',
  85. }
  86. _VERSIONS = {
  87. 0: 'youtube', # only in AuthenticateVideo videoVersions
  88. 1: 'level3',
  89. 2: 'akamai',
  90. 3: 'level3',
  91. 4: 'amazon',
  92. }
  93. def _parse_smil_formats(self, smil, smil_url, video_id, namespace=None, f4m_params=None, transform_rtmp_url=None):
  94. formats = []
  95. els = smil.findall('.//{http://www.w3.org/2001/SMIL20/Language}video')
  96. for el in els:
  97. src = el.attrib['src']
  98. m = re.match(r'''(?xi)
  99. (?P<ext>[a-z0-9]+):
  100. (?P<path>
  101. [/a-z0-9]+ # The directory and main part of the URL
  102. _(?P<tbr>[0-9]+)k
  103. _(?P<width>[0-9]+)x(?P<height>[0-9]+)
  104. _(?P<vcodec>[a-z0-9]+)
  105. _(?P<vbr>[0-9]+)
  106. _(?P<acodec>[a-z0-9]+)
  107. _(?P<abr>[0-9]+)
  108. \.[a-z0-9]+ # File extension
  109. )''', src)
  110. if not m:
  111. continue
  112. format_url = self._SMIL_BASE_URL + m.group('path')
  113. formats.append({
  114. 'url': format_url,
  115. 'format_id': 'smil_' + m.group('tbr'),
  116. 'vcodec': m.group('vcodec'),
  117. 'acodec': m.group('acodec'),
  118. 'tbr': int(m.group('tbr')),
  119. 'vbr': int(m.group('vbr')),
  120. 'abr': int(m.group('abr')),
  121. 'ext': m.group('ext'),
  122. 'width': int(m.group('width')),
  123. 'height': int(m.group('height')),
  124. })
  125. return formats
  126. def _initialize_api(self, video_id):
  127. req = sanitized_Request(
  128. 'http://www.vevo.com/auth', data=b'')
  129. webpage = self._download_webpage(
  130. req, None,
  131. note='Retrieving oauth token',
  132. errnote='Unable to retrieve oauth token')
  133. if 'THIS PAGE IS CURRENTLY UNAVAILABLE IN YOUR REGION' in webpage:
  134. raise ExtractorError(
  135. '%s said: This page is currently unavailable in your region.' % self.IE_NAME, expected=True)
  136. auth_info = self._parse_json(webpage, video_id)
  137. self._api_url_template = self.http_scheme() + '//apiv2.vevo.com/%s?token=' + auth_info['access_token']
  138. def _call_api(self, path, video_id, note, errnote, fatal=True):
  139. return self._download_json(self._api_url_template % path, video_id, note, errnote)
  140. def _real_extract(self, url):
  141. video_id = self._match_id(url)
  142. json_url = 'http://videoplayer.vevo.com/VideoService/AuthenticateVideo?isrc=%s' % video_id
  143. response = self._download_json(
  144. json_url, video_id, 'Downloading video info', 'Unable to download info')
  145. video_info = response.get('video') or {}
  146. video_versions = video_info.get('videoVersions')
  147. uploader = None
  148. timestamp = None
  149. view_count = None
  150. formats = []
  151. if not video_info:
  152. if response.get('statusCode') != 909:
  153. ytid = response.get('errorInfo', {}).get('ytid')
  154. if ytid:
  155. self.report_warning(
  156. 'Video is geoblocked, trying with the YouTube video %s' % ytid)
  157. return self.url_result(ytid, 'Youtube', ytid)
  158. if 'statusMessage' in response:
  159. raise ExtractorError('%s said: %s' % (
  160. self.IE_NAME, response['statusMessage']), expected=True)
  161. raise ExtractorError('Unable to extract videos')
  162. self._initialize_api(video_id)
  163. video_info = self._call_api(
  164. 'video/%s' % video_id, video_id, 'Downloading api video info',
  165. 'Failed to download video info')
  166. video_versions = self._call_api(
  167. 'video/%s/streams' % video_id, video_id,
  168. 'Downloading video versions info',
  169. 'Failed to download video versions info')
  170. timestamp = parse_iso8601(video_info.get('releaseDate'))
  171. artists = video_info.get('artists')
  172. if artists:
  173. uploader = artists[0]['name']
  174. view_count = int_or_none(video_info.get('views', {}).get('total'))
  175. for video_version in video_versions:
  176. version = self._VERSIONS.get(video_version['version'])
  177. version_url = video_version.get('url')
  178. if not version_url:
  179. continue
  180. if '.ism' in version_url:
  181. continue
  182. elif '.mpd' in version_url:
  183. formats.extend(self._extract_mpd_formats(
  184. version_url, video_id, mpd_id='dash-%s' % version,
  185. note='Downloading %s MPD information' % version,
  186. errnote='Failed to download %s MPD information' % version,
  187. fatal=False))
  188. elif '.m3u8' in version_url:
  189. formats.extend(self._extract_m3u8_formats(
  190. version_url, video_id, 'mp4', 'm3u8_native',
  191. m3u8_id='hls-%s' % version,
  192. note='Downloading %s m3u8 information' % version,
  193. errnote='Failed to download %s m3u8 information' % version,
  194. fatal=False))
  195. else:
  196. m = re.search(r'''(?xi)
  197. _(?P<width>[0-9]+)x(?P<height>[0-9]+)
  198. _(?P<vcodec>[a-z0-9]+)
  199. _(?P<vbr>[0-9]+)
  200. _(?P<acodec>[a-z0-9]+)
  201. _(?P<abr>[0-9]+)
  202. \.(?P<ext>[a-z0-9]+)''', version_url)
  203. if not m:
  204. continue
  205. formats.append({
  206. 'url': version_url,
  207. 'format_id': 'http-%s-%s' % (version, video_version['quality']),
  208. 'vcodec': m.group('vcodec'),
  209. 'acodec': m.group('acodec'),
  210. 'vbr': int(m.group('vbr')),
  211. 'abr': int(m.group('abr')),
  212. 'ext': m.group('ext'),
  213. 'width': int(m.group('width')),
  214. 'height': int(m.group('height')),
  215. })
  216. else:
  217. timestamp = int_or_none(self._search_regex(
  218. r'/Date\((\d+)\)/',
  219. video_info['releaseDate'], 'release date', fatal=False),
  220. scale=1000)
  221. artists = video_info.get('mainArtists')
  222. if artists:
  223. uploader = artists[0]['artistName']
  224. smil_parsed = False
  225. for video_version in video_info['videoVersions']:
  226. version = self._VERSIONS.get(video_version['version'])
  227. if version == 'youtube':
  228. continue
  229. else:
  230. source_type = self._SOURCE_TYPES.get(video_version['sourceType'])
  231. renditions = compat_etree_fromstring(video_version['data'])
  232. if source_type == 'http':
  233. for rend in renditions.findall('rendition'):
  234. attr = rend.attrib
  235. formats.append({
  236. 'url': attr['url'],
  237. 'format_id': 'http-%s-%s' % (version, attr['name']),
  238. 'height': int_or_none(attr.get('frameheight')),
  239. 'width': int_or_none(attr.get('frameWidth')),
  240. 'tbr': int_or_none(attr.get('totalBitrate')),
  241. 'vbr': int_or_none(attr.get('videoBitrate')),
  242. 'abr': int_or_none(attr.get('audioBitrate')),
  243. 'vcodec': attr.get('videoCodec'),
  244. 'acodec': attr.get('audioCodec'),
  245. })
  246. elif source_type == 'hls':
  247. formats.extend(self._extract_m3u8_formats(
  248. renditions.find('rendition').attrib['url'], video_id,
  249. 'mp4', 'm3u8_native', m3u8_id='hls-%s' % version,
  250. note='Downloading %s m3u8 information' % version,
  251. errnote='Failed to download %s m3u8 information' % version,
  252. fatal=False))
  253. elif source_type == 'smil' and version == 'level3' and not smil_parsed:
  254. formats.extend(self._extract_smil_formats(
  255. renditions.find('rendition').attrib['url'], video_id, False))
  256. smil_parsed = True
  257. self._sort_formats(formats)
  258. title = video_info['title']
  259. is_explicit = video_info.get('isExplicit')
  260. if is_explicit is True:
  261. age_limit = 18
  262. elif is_explicit is False:
  263. age_limit = 0
  264. else:
  265. age_limit = None
  266. duration = video_info.get('duration')
  267. return {
  268. 'id': video_id,
  269. 'title': title,
  270. 'formats': formats,
  271. 'thumbnail': video_info.get('imageUrl') or video_info.get('thumbnailUrl'),
  272. 'timestamp': timestamp,
  273. 'uploader': uploader,
  274. 'duration': duration,
  275. 'view_count': view_count,
  276. 'age_limit': age_limit,
  277. }