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.

145 lines
5.7 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. def fix_ampersand(s):
  74. """ Fix unencoded ampersand in XML """
  75. return s.replace(u'& ', '&amp; ')
  76. idoc = self._download_xml(
  77. self._FEED_URL + '?' + data, video_id,
  78. u'Downloading info', transform_source=fix_ampersand)
  79. return [self._get_video_info(item) for item in idoc.findall('.//item')]
  80. class MTVIE(MTVServicesInfoExtractor):
  81. _VALID_URL = r'''(?x)^https?://
  82. (?:(?:www\.)?mtv\.com/videos/.+?/(?P<videoid>[0-9]+)/[^/]+$|
  83. m\.mtv\.com/videos/video\.rbml\?.*?id=(?P<mgid>[^&]+))'''
  84. _FEED_URL = 'http://www.mtv.com/player/embed/AS3/rss/'
  85. _TESTS = [
  86. {
  87. u'url': u'http://www.mtv.com/videos/misc/853555/ours-vh1-storytellers.jhtml',
  88. u'file': u'853555.mp4',
  89. u'md5': u'850f3f143316b1e71fa56a4edfd6e0f8',
  90. u'info_dict': {
  91. u'title': u'Taylor Swift - "Ours (VH1 Storytellers)"',
  92. u'description': u'Album: Taylor Swift performs "Ours" for VH1 Storytellers at Harvey Mudd College.',
  93. },
  94. },
  95. {
  96. u'add_ie': ['Vevo'],
  97. u'url': u'http://www.mtv.com/videos/taylor-swift/916187/everything-has-changed-ft-ed-sheeran.jhtml',
  98. u'file': u'USCJY1331283.mp4',
  99. u'md5': u'73b4e7fcadd88929292fe52c3ced8caf',
  100. u'info_dict': {
  101. u'title': u'Everything Has Changed',
  102. u'upload_date': u'20130606',
  103. u'uploader': u'Taylor Swift',
  104. },
  105. u'skip': u'VEVO is only available in some countries',
  106. },
  107. ]
  108. def _get_thumbnail_url(self, uri, itemdoc):
  109. return 'http://mtv.mtvnimages.com/uri/' + uri
  110. def _real_extract(self, url):
  111. mobj = re.match(self._VALID_URL, url)
  112. video_id = mobj.group('videoid')
  113. uri = mobj.group('mgid')
  114. if uri is None:
  115. webpage = self._download_webpage(url, video_id)
  116. # Some videos come from Vevo.com
  117. m_vevo = re.search(r'isVevoVideo = true;.*?vevoVideoId = "(.*?)";',
  118. webpage, re.DOTALL)
  119. if m_vevo:
  120. vevo_id = m_vevo.group(1);
  121. self.to_screen(u'Vevo video detected: %s' % vevo_id)
  122. return self.url_result('vevo:%s' % vevo_id, ie='Vevo')
  123. uri = self._html_search_regex(r'/uri/(.*?)\?', webpage, u'uri')
  124. return self._get_videos_info(uri)