vevo.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. from __future__ import unicode_literals
  2. import re
  3. import xml.etree.ElementTree
  4. import datetime
  5. from .common import InfoExtractor
  6. from ..utils import (
  7. compat_HTTPError,
  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": "06bea460acb744eab74a9d7dcb4bfd61",
  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. }
  34. }, {
  35. 'note': 'v3 SMIL format',
  36. 'url': 'http://www.vevo.com/watch/cassadee-pope/i-wish-i-could-break-your-heart/USUV71302923',
  37. 'md5': '893ec0e0d4426a1d96c01de8f2bdff58',
  38. 'info_dict': {
  39. 'id': 'USUV71302923',
  40. 'ext': 'mp4',
  41. 'upload_date': '20140219',
  42. 'uploader': 'Cassadee Pope',
  43. 'title': 'I Wish I Could Break Your Heart',
  44. 'duration': 226.101,
  45. }
  46. }]
  47. _SMIL_BASE_URL = 'http://smil.lvl3.vevo.com/'
  48. def _formats_from_json(self, video_info):
  49. last_version = {'version': -1}
  50. for version in video_info['videoVersions']:
  51. # These are the HTTP downloads, other types are for different manifests
  52. if version['sourceType'] == 2:
  53. if version['version'] > last_version['version']:
  54. last_version = version
  55. if last_version['version'] == -1:
  56. raise ExtractorError('Unable to extract last version of the video')
  57. renditions = xml.etree.ElementTree.fromstring(last_version['data'])
  58. formats = []
  59. # Already sorted from worst to best quality
  60. for rend in renditions.findall('rendition'):
  61. attr = rend.attrib
  62. format_note = '%(videoCodec)s@%(videoBitrate)4sk, %(audioCodec)s@%(audioBitrate)3sk' % attr
  63. formats.append({
  64. 'url': attr['url'],
  65. 'format_id': attr['name'],
  66. 'format_note': format_note,
  67. 'height': int(attr['frameheight']),
  68. 'width': int(attr['frameWidth']),
  69. })
  70. return formats
  71. def _formats_from_smil(self, smil_xml):
  72. formats = []
  73. smil_doc = xml.etree.ElementTree.fromstring(smil_xml.encode('utf-8'))
  74. els = smil_doc.findall('.//{http://www.w3.org/2001/SMIL20/Language}video')
  75. for el in els:
  76. src = el.attrib['src']
  77. m = re.match(r'''(?xi)
  78. (?P<ext>[a-z0-9]+):
  79. (?P<path>
  80. [/a-z0-9]+ # The directory and main part of the URL
  81. _(?P<cbr>[0-9]+)k
  82. _(?P<width>[0-9]+)x(?P<height>[0-9]+)
  83. _(?P<vcodec>[a-z0-9]+)
  84. _(?P<vbr>[0-9]+)
  85. _(?P<acodec>[a-z0-9]+)
  86. _(?P<abr>[0-9]+)
  87. \.[a-z0-9]+ # File extension
  88. )''', src)
  89. if not m:
  90. continue
  91. format_url = self._SMIL_BASE_URL + m.group('path')
  92. formats.append({
  93. 'url': format_url,
  94. 'format_id': 'SMIL_' + m.group('cbr'),
  95. 'vcodec': m.group('vcodec'),
  96. 'acodec': m.group('acodec'),
  97. 'vbr': int(m.group('vbr')),
  98. 'abr': int(m.group('abr')),
  99. 'ext': m.group('ext'),
  100. 'width': int(m.group('width')),
  101. 'height': int(m.group('height')),
  102. })
  103. return formats
  104. def _real_extract(self, url):
  105. mobj = re.match(self._VALID_URL, url)
  106. video_id = mobj.group('id')
  107. json_url = 'http://videoplayer.vevo.com/VideoService/AuthenticateVideo?isrc=%s' % video_id
  108. video_info = self._download_json(json_url, video_id)['video']
  109. formats = self._formats_from_json(video_info)
  110. # Download SMIL
  111. smil_blocks = sorted((
  112. f for f in video_info['videoVersions']
  113. if f['sourceType'] == 13),
  114. key=lambda f: f['version'])
  115. smil_url = '%s/Video/V2/VFILE/%s/%sr.smil' % (
  116. self._SMIL_BASE_URL, video_id, video_id.lower())
  117. if smil_blocks:
  118. smil_url_m = self._search_regex(
  119. r'url="([^"]+)"', smil_blocks[-1]['data'], 'SMIL URL',
  120. fatal=False)
  121. if smil_url_m is not None:
  122. smil_url = smil_url_m
  123. try:
  124. smil_xml = self._download_webpage(smil_url, video_id,
  125. 'Downloading SMIL info')
  126. formats.extend(self._formats_from_smil(smil_xml))
  127. except ExtractorError as ee:
  128. if not isinstance(ee.cause, compat_HTTPError):
  129. raise
  130. self._downloader.report_warning(
  131. 'Cannot download SMIL information, falling back to JSON ..')
  132. timestamp_ms = int(self._search_regex(
  133. r'/Date\((\d+)\)/', video_info['launchDate'], 'launch date'))
  134. upload_date = datetime.datetime.fromtimestamp(timestamp_ms // 1000)
  135. return {
  136. 'id': video_id,
  137. 'title': video_info['title'],
  138. 'formats': formats,
  139. 'thumbnail': video_info['imageUrl'],
  140. 'upload_date': upload_date.strftime('%Y%m%d'),
  141. 'uploader': video_info['mainArtists'][0]['artistName'],
  142. 'duration': video_info['duration'],
  143. }