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.

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