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.

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