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.

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