vevo.py 12 KB

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