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.

133 lines
5.3 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'add_ie': ['Vevo'],
  25. u'url': u'http://www.mtv.com/videos/taylor-swift/916187/everything-has-changed-ft-ed-sheeran.jhtml',
  26. u'file': u'USCJY1331283.mp4',
  27. u'md5': u'73b4e7fcadd88929292fe52c3ced8caf',
  28. u'info_dict': {
  29. u'title': u'Everything Has Changed',
  30. u'upload_date': u'20130606',
  31. u'uploader': u'Taylor Swift',
  32. },
  33. u'skip': u'VEVO is only available in some countries',
  34. },
  35. ]
  36. @staticmethod
  37. def _id_from_uri(uri):
  38. return uri.split(':')[-1]
  39. # This was originally implemented for ComedyCentral, but it also works here
  40. @staticmethod
  41. def _transform_rtmp_url(rtmp_video_url):
  42. m = re.match(r'^rtmpe?://.*?/(?P<finalid>gsp\..+?/.*)$', rtmp_video_url)
  43. if not m:
  44. raise ExtractorError(u'Cannot transform RTMP url')
  45. base = 'http://mtvnmobile.vo.llnwd.net/kip0/_pxn=1+_pxI0=Ripod-h264+_pxL0=undefined+_pxM0=+_pxK=18639+_pxE=mp4/44620/mtvnorigin/'
  46. return base + m.group('finalid')
  47. def _get_thumbnail_url(self, uri, itemdoc):
  48. return 'http://mtv.mtvnimages.com/uri/' + uri
  49. def _extract_video_formats(self, metadataXml):
  50. if '/error_country_block.swf' in metadataXml:
  51. raise ExtractorError(u'This video is not available from your country.', expected=True)
  52. mdoc = xml.etree.ElementTree.fromstring(metadataXml.encode('utf-8'))
  53. renditions = mdoc.findall('.//rendition')
  54. formats = []
  55. for rendition in mdoc.findall('.//rendition'):
  56. try:
  57. _, _, ext = rendition.attrib['type'].partition('/')
  58. rtmp_video_url = rendition.find('./src').text
  59. formats.append({'ext': ext,
  60. 'url': self._transform_rtmp_url(rtmp_video_url),
  61. 'format_id': rendition.get('bitrate'),
  62. 'width': int(rendition.get('width')),
  63. 'height': int(rendition.get('height')),
  64. })
  65. except (KeyError, TypeError):
  66. raise ExtractorError('Invalid rendition field.')
  67. return formats
  68. def _get_video_info(self, itemdoc):
  69. uri = itemdoc.find('guid').text
  70. video_id = self._id_from_uri(uri)
  71. self.report_extraction(video_id)
  72. mediagen_url = itemdoc.find('%s/%s' % (_media_xml_tag('group'), _media_xml_tag('content'))).attrib['url']
  73. # Remove the templates, like &device={device}
  74. mediagen_url = re.sub(r'&[^=]*?={.*?}(?=(&|$))', u'', mediagen_url)
  75. if 'acceptMethods' not in mediagen_url:
  76. mediagen_url += '&acceptMethods=fms'
  77. mediagen_page = self._download_webpage(mediagen_url, video_id,
  78. u'Downloading video urls')
  79. description_node = itemdoc.find('description')
  80. if description_node is not None:
  81. description = description_node.text.strip()
  82. else:
  83. description = None
  84. info = {
  85. 'title': itemdoc.find('title').text,
  86. 'formats': self._extract_video_formats(mediagen_page),
  87. 'id': video_id,
  88. 'thumbnail': self._get_thumbnail_url(uri, itemdoc),
  89. 'description': description,
  90. }
  91. # TODO: Remove when #980 has been merged
  92. info.update(info['formats'][-1])
  93. return info
  94. def _get_videos_info(self, uri):
  95. video_id = self._id_from_uri(uri)
  96. data = compat_urllib_parse.urlencode({'uri': uri})
  97. infoXml = self._download_webpage(self._FEED_URL +'?' + data, video_id,
  98. u'Downloading info')
  99. idoc = xml.etree.ElementTree.fromstring(infoXml.encode('utf-8'))
  100. return [self._get_video_info(item) for item in idoc.findall('.//item')]
  101. def _real_extract(self, url):
  102. mobj = re.match(self._VALID_URL, url)
  103. video_id = mobj.group('videoid')
  104. webpage = self._download_webpage(url, video_id)
  105. # Some videos come from Vevo.com
  106. m_vevo = re.search(r'isVevoVideo = true;.*?vevoVideoId = "(.*?)";',
  107. webpage, re.DOTALL)
  108. if m_vevo:
  109. vevo_id = m_vevo.group(1);
  110. self.to_screen(u'Vevo video detected: %s' % vevo_id)
  111. return self.url_result('vevo:%s' % vevo_id, ie='Vevo')
  112. uri = self._html_search_regex(r'/uri/(.*?)\?', webpage, u'uri')
  113. return self._get_videos_info(uri)