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.

353 lines
13 KiB

12 years ago
12 years ago
12 years ago
  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..compat import (
  5. compat_str,
  6. compat_urlparse,
  7. compat_HTTPError,
  8. )
  9. from ..utils import (
  10. ExtractorError,
  11. int_or_none,
  12. sanitized_Request,
  13. parse_iso8601,
  14. )
  15. class VevoBaseIE(InfoExtractor):
  16. def _extract_json(self, webpage, video_id, item):
  17. return self._parse_json(
  18. self._search_regex(
  19. r'window\.__INITIAL_STORE__\s*=\s*({.+?});\s*</script>',
  20. webpage, 'initial store'),
  21. video_id)['default'][item]
  22. class VevoIE(VevoBaseIE):
  23. '''
  24. Accepts urls from vevo.com or in the format 'vevo:{id}'
  25. (currently used by MTVIE and MySpaceIE)
  26. '''
  27. _VALID_URL = r'''(?x)
  28. (?:https?://(?:www\.)?vevo\.com/watch/(?!playlist|genre)(?:[^/]+/(?:[^/]+/)?)?|
  29. https?://cache\.vevo\.com/m/html/embed\.html\?video=|
  30. https?://videoplayer\.vevo\.com/embed/embedded\?videoId=|
  31. vevo:)
  32. (?P<id>[^&?#]+)'''
  33. _TESTS = [{
  34. 'url': 'http://www.vevo.com/watch/hurts/somebody-to-die-for/GB1101300280',
  35. 'md5': '95ee28ee45e70130e3ab02b0f579ae23',
  36. 'info_dict': {
  37. 'id': 'GB1101300280',
  38. 'ext': 'mp4',
  39. 'title': 'Hurts - Somebody to Die For',
  40. 'timestamp': 1372057200,
  41. 'upload_date': '20130624',
  42. 'uploader': 'Hurts',
  43. 'track': 'Somebody to Die For',
  44. 'artist': 'Hurts',
  45. 'genre': 'Pop',
  46. },
  47. 'expected_warnings': ['Unable to download SMIL file', 'Unable to download info'],
  48. }, {
  49. 'note': 'v3 SMIL format',
  50. 'url': 'http://www.vevo.com/watch/cassadee-pope/i-wish-i-could-break-your-heart/USUV71302923',
  51. 'md5': 'f6ab09b034f8c22969020b042e5ac7fc',
  52. 'info_dict': {
  53. 'id': 'USUV71302923',
  54. 'ext': 'mp4',
  55. 'title': 'Cassadee Pope - I Wish I Could Break Your Heart',
  56. 'timestamp': 1392796919,
  57. 'upload_date': '20140219',
  58. 'uploader': 'Cassadee Pope',
  59. 'track': 'I Wish I Could Break Your Heart',
  60. 'artist': 'Cassadee Pope',
  61. 'genre': 'Country',
  62. },
  63. 'expected_warnings': ['Unable to download SMIL file', 'Unable to download info'],
  64. }, {
  65. 'note': 'Age-limited video',
  66. 'url': 'https://www.vevo.com/watch/justin-timberlake/tunnel-vision-explicit/USRV81300282',
  67. 'info_dict': {
  68. 'id': 'USRV81300282',
  69. 'ext': 'mp4',
  70. 'title': 'Justin Timberlake - Tunnel Vision (Explicit)',
  71. 'age_limit': 18,
  72. 'timestamp': 1372888800,
  73. 'upload_date': '20130703',
  74. 'uploader': 'Justin Timberlake',
  75. 'track': 'Tunnel Vision (Explicit)',
  76. 'artist': 'Justin Timberlake',
  77. 'genre': 'Pop',
  78. },
  79. 'expected_warnings': ['Unable to download SMIL file', 'Unable to download info'],
  80. }, {
  81. 'note': 'No video_info',
  82. 'url': 'http://www.vevo.com/watch/k-camp-1/Till-I-Die/USUV71503000',
  83. 'md5': '8b83cc492d72fc9cf74a02acee7dc1b0',
  84. 'info_dict': {
  85. 'id': 'USUV71503000',
  86. 'ext': 'mp4',
  87. 'title': 'K Camp ft. T.I. - Till I Die',
  88. 'age_limit': 18,
  89. 'timestamp': 1449468000,
  90. 'upload_date': '20151207',
  91. 'uploader': 'K Camp',
  92. 'track': 'Till I Die',
  93. 'artist': 'K Camp',
  94. 'genre': 'Hip-Hop',
  95. },
  96. 'expected_warnings': ['Unable to download SMIL file', 'Unable to download info'],
  97. }, {
  98. 'note': 'Featured test',
  99. 'url': 'https://www.vevo.com/watch/lemaitre/Wait/USUV71402190',
  100. 'md5': 'd28675e5e8805035d949dc5cf161071d',
  101. 'info_dict': {
  102. 'id': 'USUV71402190',
  103. 'ext': 'mp4',
  104. 'title': 'Lemaitre ft. LoLo - Wait',
  105. 'age_limit': 0,
  106. 'timestamp': 1413432000,
  107. 'upload_date': '20141016',
  108. 'uploader': 'Lemaitre',
  109. 'track': 'Wait',
  110. 'artist': 'Lemaitre',
  111. 'genre': 'Electronic',
  112. },
  113. 'expected_warnings': ['Unable to download SMIL file', 'Unable to download info'],
  114. }, {
  115. 'note': 'Only available via webpage',
  116. 'url': 'http://www.vevo.com/watch/GBUV71600656',
  117. 'md5': '67e79210613865b66a47c33baa5e37fe',
  118. 'info_dict': {
  119. 'id': 'GBUV71600656',
  120. 'ext': 'mp4',
  121. 'title': 'ABC - Viva Love',
  122. 'age_limit': 0,
  123. 'timestamp': 1461830400,
  124. 'upload_date': '20160428',
  125. 'uploader': 'ABC',
  126. 'track': 'Viva Love',
  127. 'artist': 'ABC',
  128. 'genre': 'Pop',
  129. },
  130. 'expected_warnings': ['Failed to download video versions info'],
  131. }, {
  132. # no genres available
  133. 'url': 'http://www.vevo.com/watch/INS171400764',
  134. 'only_matching': True,
  135. }]
  136. _VERSIONS = {
  137. 0: 'youtube', # only in AuthenticateVideo videoVersions
  138. 1: 'level3',
  139. 2: 'akamai',
  140. 3: 'level3',
  141. 4: 'amazon',
  142. }
  143. def _initialize_api(self, video_id):
  144. req = sanitized_Request(
  145. 'http://www.vevo.com/auth', data=b'')
  146. webpage = self._download_webpage(
  147. req, None,
  148. note='Retrieving oauth token',
  149. errnote='Unable to retrieve oauth token')
  150. if re.search(r'(?i)THIS PAGE IS CURRENTLY UNAVAILABLE IN YOUR REGION', webpage):
  151. self.raise_geo_restricted(
  152. '%s said: This page is currently unavailable in your region' % self.IE_NAME)
  153. auth_info = self._parse_json(webpage, video_id)
  154. self._api_url_template = self.http_scheme() + '//apiv2.vevo.com/%s?token=' + auth_info['access_token']
  155. def _call_api(self, path, *args, **kwargs):
  156. try:
  157. data = self._download_json(self._api_url_template % path, *args, **kwargs)
  158. except ExtractorError as e:
  159. if isinstance(e.cause, compat_HTTPError):
  160. errors = self._parse_json(e.cause.read().decode(), None)['errors']
  161. error_message = ', '.join([error['message'] for error in errors])
  162. raise ExtractorError('%s said: %s' % (self.IE_NAME, error_message), expected=True)
  163. raise
  164. return data
  165. def _real_extract(self, url):
  166. video_id = self._match_id(url)
  167. self._initialize_api(video_id)
  168. video_info = self._call_api(
  169. 'video/%s' % video_id, video_id, 'Downloading api video info',
  170. 'Failed to download video info')
  171. video_versions = self._call_api(
  172. 'video/%s/streams' % video_id, video_id,
  173. 'Downloading video versions info',
  174. 'Failed to download video versions info',
  175. fatal=False)
  176. # Some videos are only available via webpage (e.g.
  177. # https://github.com/rg3/youtube-dl/issues/9366)
  178. if not video_versions:
  179. webpage = self._download_webpage(url, video_id)
  180. video_versions = self._extract_json(webpage, video_id, 'streams')[video_id][0]
  181. uploader = None
  182. artist = None
  183. featured_artist = None
  184. artists = video_info.get('artists')
  185. for curr_artist in artists:
  186. if curr_artist.get('role') == 'Featured':
  187. featured_artist = curr_artist['name']
  188. else:
  189. artist = uploader = curr_artist['name']
  190. formats = []
  191. for video_version in video_versions:
  192. version = self._VERSIONS.get(video_version['version'])
  193. version_url = video_version.get('url')
  194. if not version_url:
  195. continue
  196. if '.ism' in version_url:
  197. continue
  198. elif '.mpd' in version_url:
  199. formats.extend(self._extract_mpd_formats(
  200. version_url, video_id, mpd_id='dash-%s' % version,
  201. note='Downloading %s MPD information' % version,
  202. errnote='Failed to download %s MPD information' % version,
  203. fatal=False))
  204. elif '.m3u8' in version_url:
  205. formats.extend(self._extract_m3u8_formats(
  206. version_url, video_id, 'mp4', 'm3u8_native',
  207. m3u8_id='hls-%s' % version,
  208. note='Downloading %s m3u8 information' % version,
  209. errnote='Failed to download %s m3u8 information' % version,
  210. fatal=False))
  211. else:
  212. m = re.search(r'''(?xi)
  213. _(?P<width>[0-9]+)x(?P<height>[0-9]+)
  214. _(?P<vcodec>[a-z0-9]+)
  215. _(?P<vbr>[0-9]+)
  216. _(?P<acodec>[a-z0-9]+)
  217. _(?P<abr>[0-9]+)
  218. \.(?P<ext>[a-z0-9]+)''', version_url)
  219. if not m:
  220. continue
  221. formats.append({
  222. 'url': version_url,
  223. 'format_id': 'http-%s-%s' % (version, video_version['quality']),
  224. 'vcodec': m.group('vcodec'),
  225. 'acodec': m.group('acodec'),
  226. 'vbr': int(m.group('vbr')),
  227. 'abr': int(m.group('abr')),
  228. 'ext': m.group('ext'),
  229. 'width': int(m.group('width')),
  230. 'height': int(m.group('height')),
  231. })
  232. self._sort_formats(formats)
  233. track = video_info['title']
  234. if featured_artist:
  235. artist = '%s ft. %s' % (artist, featured_artist)
  236. title = '%s - %s' % (artist, track) if artist else track
  237. genres = video_info.get('genres')
  238. genre = (
  239. genres[0] if genres and isinstance(genres, list) and
  240. isinstance(genres[0], compat_str) else None)
  241. is_explicit = video_info.get('isExplicit')
  242. if is_explicit is True:
  243. age_limit = 18
  244. elif is_explicit is False:
  245. age_limit = 0
  246. else:
  247. age_limit = None
  248. return {
  249. 'id': video_id,
  250. 'title': title,
  251. 'formats': formats,
  252. 'thumbnail': video_info.get('imageUrl') or video_info.get('thumbnailUrl'),
  253. 'timestamp': parse_iso8601(video_info.get('releaseDate')),
  254. 'uploader': uploader,
  255. 'duration': int_or_none(video_info.get('duration')),
  256. 'view_count': int_or_none(video_info.get('views', {}).get('total')),
  257. 'age_limit': age_limit,
  258. 'track': track,
  259. 'artist': uploader,
  260. 'genre': genre,
  261. }
  262. class VevoPlaylistIE(VevoBaseIE):
  263. _VALID_URL = r'https?://(?:www\.)?vevo\.com/watch/(?P<kind>playlist|genre)/(?P<id>[^/?#&]+)'
  264. _TESTS = [{
  265. 'url': 'http://www.vevo.com/watch/playlist/dadbf4e7-b99f-4184-9670-6f0e547b6a29',
  266. 'info_dict': {
  267. 'id': 'dadbf4e7-b99f-4184-9670-6f0e547b6a29',
  268. 'title': 'Best-Of: Birdman',
  269. },
  270. 'playlist_count': 10,
  271. }, {
  272. 'url': 'http://www.vevo.com/watch/genre/rock',
  273. 'info_dict': {
  274. 'id': 'rock',
  275. 'title': 'Rock',
  276. },
  277. 'playlist_count': 20,
  278. }, {
  279. 'url': 'http://www.vevo.com/watch/playlist/dadbf4e7-b99f-4184-9670-6f0e547b6a29?index=0',
  280. 'md5': '32dcdfddddf9ec6917fc88ca26d36282',
  281. 'info_dict': {
  282. 'id': 'USCMV1100073',
  283. 'ext': 'mp4',
  284. 'title': 'Birdman - Y.U. MAD',
  285. 'timestamp': 1323417600,
  286. 'upload_date': '20111209',
  287. 'uploader': 'Birdman',
  288. 'track': 'Y.U. MAD',
  289. 'artist': 'Birdman',
  290. 'genre': 'Rap/Hip-Hop',
  291. },
  292. 'expected_warnings': ['Unable to download SMIL file'],
  293. }, {
  294. 'url': 'http://www.vevo.com/watch/genre/rock?index=0',
  295. 'only_matching': True,
  296. }]
  297. def _real_extract(self, url):
  298. mobj = re.match(self._VALID_URL, url)
  299. playlist_id = mobj.group('id')
  300. playlist_kind = mobj.group('kind')
  301. webpage = self._download_webpage(url, playlist_id)
  302. qs = compat_urlparse.parse_qs(compat_urlparse.urlparse(url).query)
  303. index = qs.get('index', [None])[0]
  304. if index:
  305. video_id = self._search_regex(
  306. r'<meta[^>]+content=(["\'])vevo://video/(?P<id>.+?)\1[^>]*>',
  307. webpage, 'video id', default=None, group='id')
  308. if video_id:
  309. return self.url_result('vevo:%s' % video_id, VevoIE.ie_key())
  310. playlists = self._extract_json(webpage, playlist_id, '%ss' % playlist_kind)
  311. playlist = (list(playlists.values())[0]
  312. if playlist_kind == 'playlist' else playlists[playlist_id])
  313. entries = [
  314. self.url_result('vevo:%s' % src, VevoIE.ie_key())
  315. for src in playlist['isrcs']]
  316. return self.playlist_result(
  317. entries, playlist.get('playlistId') or playlist_id,
  318. playlist.get('name'), playlist.get('description'))