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.

220 lines
8.5 KiB

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