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.

131 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'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. return rtmp_video_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. 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. # Remove the templates, like &device={device}
  73. mediagen_url = re.sub(r'&[^=]*?={.*?}(?=(&|$))', u'', mediagen_url)
  74. if 'acceptMethods' not in mediagen_url:
  75. mediagen_url += '&acceptMethods=fms'
  76. mediagen_page = self._download_webpage(mediagen_url, video_id,
  77. u'Downloading video urls')
  78. description_node = itemdoc.find('description')
  79. if description_node is not None:
  80. description = description_node.text.strip()
  81. else:
  82. description = None
  83. info = {
  84. 'title': itemdoc.find('title').text,
  85. 'formats': self._extract_video_formats(mediagen_page),
  86. 'id': video_id,
  87. 'thumbnail': self._get_thumbnail_url(uri, itemdoc),
  88. 'description': description,
  89. }
  90. # TODO: Remove when #980 has been merged
  91. info.update(info['formats'][-1])
  92. return info
  93. def _get_videos_info(self, uri):
  94. video_id = self._id_from_uri(uri)
  95. data = compat_urllib_parse.urlencode({'uri': uri})
  96. idoc = self._download_xml(self._FEED_URL +'?' + data, video_id,
  97. u'Downloading info')
  98. return [self._get_video_info(item) for item in idoc.findall('.//item')]
  99. def _real_extract(self, url):
  100. mobj = re.match(self._VALID_URL, url)
  101. video_id = mobj.group('videoid')
  102. webpage = self._download_webpage(url, video_id)
  103. # Some videos come from Vevo.com
  104. m_vevo = re.search(r'isVevoVideo = true;.*?vevoVideoId = "(.*?)";',
  105. webpage, re.DOTALL)
  106. if m_vevo:
  107. vevo_id = m_vevo.group(1);
  108. self.to_screen(u'Vevo video detected: %s' % vevo_id)
  109. return self.url_result('vevo:%s' % vevo_id, ie='Vevo')
  110. uri = self._html_search_regex(r'/uri/(.*?)\?', webpage, u'uri')
  111. return self._get_videos_info(uri)