vevo.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import re
  2. import json
  3. import xml.etree.ElementTree
  4. import datetime
  5. from .common import InfoExtractor
  6. from ..utils import (
  7. determine_ext,
  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'((http://www.vevo.com/watch/.*?/.*?/)|(vevo:))(?P<id>.*?)(\?|$)'
  16. _TEST = {
  17. u'url': u'http://www.vevo.com/watch/hurts/somebody-to-die-for/GB1101300280',
  18. u'file': u'GB1101300280.mp4',
  19. u'info_dict': {
  20. u"upload_date": u"20130624",
  21. u"uploader": u"Hurts",
  22. u"title": u"Somebody to Die For",
  23. u'duration': 230,
  24. }
  25. }
  26. def _real_extract(self, url):
  27. mobj = re.match(self._VALID_URL, url)
  28. video_id = mobj.group('id')
  29. json_url = 'http://videoplayer.vevo.com/VideoService/AuthenticateVideo?isrc=%s' % video_id
  30. info_json = self._download_webpage(json_url, video_id, u'Downloading json info')
  31. self.report_extraction(video_id)
  32. video_info = json.loads(info_json)['video']
  33. last_version = {'version': -1}
  34. for version in video_info['videoVersions']:
  35. # These are the HTTP downloads, other types are for different manifests
  36. if version['sourceType'] == 2:
  37. if version['version'] > last_version['version']:
  38. last_version = version
  39. if last_version['version'] == -1:
  40. raise ExtractorError(u'Unable to extract last version of the video')
  41. renditions = xml.etree.ElementTree.fromstring(last_version['data'])
  42. formats = []
  43. # Already sorted from worst to best quality
  44. for rend in renditions.findall('rendition'):
  45. attr = rend.attrib
  46. format_note = '%(videoCodec)s@%(videoBitrate)4sK, %(audioCodec)s@%(audioBitrate)3sK' % attr
  47. formats.append({
  48. 'url': attr['url'],
  49. 'format_id': attr['name'],
  50. 'format_note': format_note,
  51. 'height': int(attr['frameheight']),
  52. 'width': int(attr['frameWidth']),
  53. })
  54. timestamp_ms = int(self._search_regex(
  55. r'/Date\((\d+)\)/', video_info['launchDate'], u'launch date'))
  56. upload_date = datetime.datetime.fromtimestamp(timestamp_ms // 1000)
  57. info = {
  58. 'id': video_id,
  59. 'title': video_info['title'],
  60. 'formats': formats,
  61. 'thumbnail': video_info['imageUrl'],
  62. 'upload_date': upload_date.strftime('%Y%m%d'),
  63. 'uploader': video_info['mainArtists'][0]['artistName'],
  64. 'duration': video_info['duration'],
  65. }
  66. return info