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.

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