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.

121 lines
4.9 KiB

  1. import re
  2. import xml.etree.ElementTree
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. compat_urllib_parse,
  6. ExtractorError,
  7. )
  8. def _media_xml_tag(tag):
  9. return '{http://search.yahoo.com/mrss/}%s' % tag
  10. class MTVIE(InfoExtractor):
  11. _VALID_URL = r'^https?://(?:www\.)?mtv\.com/videos/.+?/(?P<videoid>[0-9]+)/[^/]+$'
  12. _FEED_URL = 'http://www.mtv.com/player/embed/AS3/rss/'
  13. _TESTS = [
  14. {
  15. u'url': u'http://www.mtv.com/videos/misc/853555/ours-vh1-storytellers.jhtml',
  16. u'file': u'853555.mp4',
  17. u'md5': u'850f3f143316b1e71fa56a4edfd6e0f8',
  18. u'info_dict': {
  19. u'title': u'Taylor Swift - "Ours (VH1 Storytellers)"',
  20. u'description': u'Album: Taylor Swift performs "Ours" for VH1 Storytellers at Harvey Mudd College.',
  21. },
  22. },
  23. {
  24. u'url': u'http://www.mtv.com/videos/taylor-swift/916187/everything-has-changed-ft-ed-sheeran.jhtml',
  25. u'file': u'USCJY1331283.mp4',
  26. u'md5': u'73b4e7fcadd88929292fe52c3ced8caf',
  27. u'info_dict': {
  28. u'title': u'Everything Has Changed',
  29. u'upload_date': u'20130606',
  30. u'uploader': u'Taylor Swift',
  31. },
  32. u'skip': u'VEVO is only available in some countries',
  33. },
  34. ]
  35. @staticmethod
  36. def _id_from_uri(uri):
  37. return uri.split(':')[-1]
  38. # This was originally implemented for ComedyCentral, but it also works here
  39. @staticmethod
  40. def _transform_rtmp_url(rtmp_video_url):
  41. m = re.match(r'^rtmpe?://.*?/(?P<finalid>gsp\..+?/.*)$', rtmp_video_url)
  42. if not m:
  43. raise ExtractorError(u'Cannot transform RTMP url')
  44. base = 'http://mtvnmobile.vo.llnwd.net/kip0/_pxn=1+_pxI0=Ripod-h264+_pxL0=undefined+_pxM0=+_pxK=18639+_pxE=mp4/44620/mtvnorigin/'
  45. return base + m.group('finalid')
  46. def _get_thumbnail_url(self, uri, itemdoc):
  47. return 'http://mtv.mtvnimages.com/uri/' + uri
  48. def _extract_video_url(self, metadataXml):
  49. if '/error_country_block.swf' in metadataXml:
  50. raise ExtractorError(u'This video is not available from your country.', expected=True)
  51. mdoc = xml.etree.ElementTree.fromstring(metadataXml.encode('utf-8'))
  52. renditions = mdoc.findall('.//rendition')
  53. # For now, always pick the highest quality.
  54. rendition = renditions[-1]
  55. try:
  56. _,_,ext = rendition.attrib['type'].partition('/')
  57. format = ext + '-' + rendition.attrib['width'] + 'x' + rendition.attrib['height'] + '_' + rendition.attrib['bitrate']
  58. rtmp_video_url = rendition.find('./src').text
  59. except KeyError:
  60. raise ExtractorError('Invalid rendition field.')
  61. video_url = self._transform_rtmp_url(rtmp_video_url)
  62. return {'ext': ext, 'url': video_url, 'format': format}
  63. def _get_video_info(self, itemdoc):
  64. uri = itemdoc.find('guid').text
  65. video_id = self._id_from_uri(uri)
  66. self.report_extraction(video_id)
  67. mediagen_url = itemdoc.find('%s/%s' % (_media_xml_tag('group'), _media_xml_tag('content'))).attrib['url']
  68. if 'acceptMethods' not in mediagen_url:
  69. mediagen_url += '&acceptMethods=fms'
  70. mediagen_page = self._download_webpage(mediagen_url, video_id,
  71. u'Downloading video urls')
  72. video_info = self._extract_video_url(mediagen_page)
  73. description_node = itemdoc.find('description')
  74. if description_node is not None:
  75. description = description_node.text
  76. else:
  77. description = None
  78. video_info.update({'title': itemdoc.find('title').text,
  79. 'id': video_id,
  80. 'thumbnail': self._get_thumbnail_url(uri, itemdoc),
  81. 'description': description,
  82. })
  83. return video_info
  84. def _get_videos_info(self, uri):
  85. video_id = self._id_from_uri(uri)
  86. data = compat_urllib_parse.urlencode({'uri': uri})
  87. infoXml = self._download_webpage(self._FEED_URL +'?' + data, video_id,
  88. u'Downloading info')
  89. idoc = xml.etree.ElementTree.fromstring(infoXml.encode('utf-8'))
  90. return [self._get_video_info(item) for item in idoc.findall('.//item')]
  91. def _real_extract(self, url):
  92. mobj = re.match(self._VALID_URL, url)
  93. video_id = mobj.group('videoid')
  94. webpage = self._download_webpage(url, video_id)
  95. # Some videos come from Vevo.com
  96. m_vevo = re.search(r'isVevoVideo = true;.*?vevoVideoId = "(.*?)";',
  97. webpage, re.DOTALL)
  98. if m_vevo:
  99. vevo_id = m_vevo.group(1);
  100. self.to_screen(u'Vevo video detected: %s' % vevo_id)
  101. return self.url_result('vevo:%s' % vevo_id, ie='Vevo')
  102. uri = self._html_search_regex(r'/uri/(.*?)\?', webpage, u'uri')
  103. return self._get_videos_info(uri)