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.

156 lines
6.0 KiB

  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. compat_urllib_parse,
  6. ExtractorError,
  7. fix_xml_ampersands,
  8. url_basename,
  9. RegexNotFoundError,
  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, mdoc):
  33. if re.match(r'.*/error_country_block\.swf$', mdoc.find('.//src').text) is not None:
  34. raise ExtractorError('This video is not available from your country.', expected=True)
  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'&[^=]*?={.*?}(?=(&|$))', '', mediagen_url)
  56. if 'acceptMethods' not in mediagen_url:
  57. mediagen_url += '&acceptMethods=fms'
  58. mediagen_doc = self._download_xml(mediagen_url, video_id,
  59. '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. return {
  66. 'title': itemdoc.find('title').text,
  67. 'formats': self._extract_video_formats(mediagen_doc),
  68. 'id': video_id,
  69. 'thumbnail': self._get_thumbnail_url(uri, itemdoc),
  70. 'description': description,
  71. }
  72. def _get_videos_info(self, uri):
  73. video_id = self._id_from_uri(uri)
  74. data = compat_urllib_parse.urlencode({'uri': uri})
  75. idoc = self._download_xml(
  76. self._FEED_URL + '?' + data, video_id,
  77. 'Downloading info', transform_source=fix_xml_ampersands)
  78. return [self._get_video_info(item) for item in idoc.findall('.//item')]
  79. def _real_extract(self, url):
  80. title = url_basename(url)
  81. webpage = self._download_webpage(url, title)
  82. try:
  83. # the url is in the format http://media.mtvnservices.com/fb/{mgid}.swf
  84. fb_url = self._og_search_video_url(webpage)
  85. mgid = url_basename(fb_url).rpartition('.')[0]
  86. except RegexNotFoundError:
  87. mgid = self._search_regex(r'data-mgid="(.*?)"', webpage, u'mgid')
  88. return self._get_videos_info(mgid)
  89. class MTVIE(MTVServicesInfoExtractor):
  90. _VALID_URL = r'''(?x)^https?://
  91. (?:(?:www\.)?mtv\.com/videos/.+?/(?P<videoid>[0-9]+)/[^/]+$|
  92. m\.mtv\.com/videos/video\.rbml\?.*?id=(?P<mgid>[^&]+))'''
  93. _FEED_URL = 'http://www.mtv.com/player/embed/AS3/rss/'
  94. _TESTS = [
  95. {
  96. 'url': 'http://www.mtv.com/videos/misc/853555/ours-vh1-storytellers.jhtml',
  97. 'file': '853555.mp4',
  98. 'md5': '850f3f143316b1e71fa56a4edfd6e0f8',
  99. 'info_dict': {
  100. 'title': 'Taylor Swift - "Ours (VH1 Storytellers)"',
  101. 'description': 'Album: Taylor Swift performs "Ours" for VH1 Storytellers at Harvey Mudd College.',
  102. },
  103. },
  104. {
  105. 'add_ie': ['Vevo'],
  106. 'url': 'http://www.mtv.com/videos/taylor-swift/916187/everything-has-changed-ft-ed-sheeran.jhtml',
  107. 'file': 'USCJY1331283.mp4',
  108. 'md5': '73b4e7fcadd88929292fe52c3ced8caf',
  109. 'info_dict': {
  110. 'title': 'Everything Has Changed',
  111. 'upload_date': '20130606',
  112. 'uploader': 'Taylor Swift',
  113. },
  114. 'skip': 'VEVO is only available in some countries',
  115. },
  116. ]
  117. def _get_thumbnail_url(self, uri, itemdoc):
  118. return 'http://mtv.mtvnimages.com/uri/' + uri
  119. def _real_extract(self, url):
  120. mobj = re.match(self._VALID_URL, url)
  121. video_id = mobj.group('videoid')
  122. uri = mobj.groupdict().get('mgid')
  123. if uri is None:
  124. webpage = self._download_webpage(url, video_id)
  125. # Some videos come from Vevo.com
  126. m_vevo = re.search(r'isVevoVideo = true;.*?vevoVideoId = "(.*?)";',
  127. webpage, re.DOTALL)
  128. if m_vevo:
  129. vevo_id = m_vevo.group(1);
  130. self.to_screen('Vevo video detected: %s' % vevo_id)
  131. return self.url_result('vevo:%s' % vevo_id, ie='Vevo')
  132. uri = self._html_search_regex(r'/uri/(.*?)\?', webpage, 'uri')
  133. return self._get_videos_info(uri)