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.

160 lines
6.1 KiB

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