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.

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