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.

130 lines
5.1 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_formats(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. formats = []
  54. for rendition in mdoc.findall('.//rendition'):
  55. try:
  56. _, _, ext = rendition.attrib['type'].partition('/')
  57. rtmp_video_url = rendition.find('./src').text
  58. formats.append({'ext': ext,
  59. 'url': self._transform_rtmp_url(rtmp_video_url),
  60. 'format_id': rendition.get('bitrate'),
  61. 'width': int(rendition.get('width')),
  62. 'height': int(rendition.get('height')),
  63. })
  64. except (KeyError, TypeError):
  65. raise ExtractorError('Invalid rendition field.')
  66. return formats
  67. def _get_video_info(self, itemdoc):
  68. uri = itemdoc.find('guid').text
  69. video_id = self._id_from_uri(uri)
  70. self.report_extraction(video_id)
  71. mediagen_url = itemdoc.find('%s/%s' % (_media_xml_tag('group'), _media_xml_tag('content'))).attrib['url']
  72. if 'acceptMethods' not in mediagen_url:
  73. mediagen_url += '&acceptMethods=fms'
  74. mediagen_page = self._download_webpage(mediagen_url, video_id,
  75. u'Downloading video urls')
  76. description_node = itemdoc.find('description')
  77. if description_node is not None:
  78. description = description_node.text
  79. else:
  80. description = None
  81. info = {
  82. 'title': itemdoc.find('title').text,
  83. 'formats': self._extract_video_formats(mediagen_page),
  84. 'id': video_id,
  85. 'thumbnail': self._get_thumbnail_url(uri, itemdoc),
  86. 'description': description,
  87. }
  88. # TODO: Remove when #980 has been merged
  89. info.update(info['formats'][-1])
  90. return info
  91. def _get_videos_info(self, uri):
  92. video_id = self._id_from_uri(uri)
  93. data = compat_urllib_parse.urlencode({'uri': uri})
  94. infoXml = self._download_webpage(self._FEED_URL +'?' + data, video_id,
  95. u'Downloading info')
  96. idoc = xml.etree.ElementTree.fromstring(infoXml.encode('utf-8'))
  97. return [self._get_video_info(item) for item in idoc.findall('.//item')]
  98. def _real_extract(self, url):
  99. mobj = re.match(self._VALID_URL, url)
  100. video_id = mobj.group('videoid')
  101. webpage = self._download_webpage(url, video_id)
  102. # Some videos come from Vevo.com
  103. m_vevo = re.search(r'isVevoVideo = true;.*?vevoVideoId = "(.*?)";',
  104. webpage, re.DOTALL)
  105. if m_vevo:
  106. vevo_id = m_vevo.group(1);
  107. self.to_screen(u'Vevo video detected: %s' % vevo_id)
  108. return self.url_result('vevo:%s' % vevo_id, ie='Vevo')
  109. uri = self._html_search_regex(r'/uri/(.*?)\?', webpage, u'uri')
  110. return self._get_videos_info(uri)