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.

265 lines
10 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_feed_url(self, uri):
  31. return self._FEED_URL
  32. def _get_thumbnail_url(self, uri, itemdoc):
  33. search_path = '%s/%s' % (_media_xml_tag('group'), _media_xml_tag('thumbnail'))
  34. thumb_node = itemdoc.find(search_path)
  35. if thumb_node is None:
  36. return None
  37. else:
  38. return thumb_node.attrib['url']
  39. def _extract_mobile_video_formats(self, mtvn_id):
  40. webpage_url = self._MOBILE_TEMPLATE % mtvn_id
  41. req = compat_urllib_request.Request(webpage_url)
  42. # Otherwise we get a webpage that would execute some javascript
  43. req.add_header('Youtubedl-user-agent', 'curl/7')
  44. webpage = self._download_webpage(req, mtvn_id,
  45. 'Downloading mobile page')
  46. metrics_url = unescapeHTML(self._search_regex(r'<a href="(http://metrics.+?)"', webpage, 'url'))
  47. req = HEADRequest(metrics_url)
  48. response = self._request_webpage(req, mtvn_id, 'Resolving url')
  49. url = response.geturl()
  50. # Transform the url to get the best quality:
  51. url = re.sub(r'.+pxE=mp4', 'http://mtvnmobile.vo.llnwd.net/kip0/_pxn=0+_pxK=18639+_pxE=mp4', url, 1)
  52. return [{'url': url,'ext': 'mp4'}]
  53. def _extract_video_formats(self, mdoc, mtvn_id):
  54. if re.match(r'.*/(error_country_block\.swf|geoblock\.mp4)$', mdoc.find('.//src').text) is not None:
  55. if mtvn_id is not None and self._MOBILE_TEMPLATE is not None:
  56. self.to_screen('The normal version is not available from your '
  57. 'country, trying with the mobile version')
  58. return self._extract_mobile_video_formats(mtvn_id)
  59. raise ExtractorError('This video is not available from your country.',
  60. expected=True)
  61. formats = []
  62. for rendition in mdoc.findall('.//rendition'):
  63. try:
  64. _, _, ext = rendition.attrib['type'].partition('/')
  65. rtmp_video_url = rendition.find('./src').text
  66. formats.append({'ext': ext,
  67. 'url': self._transform_rtmp_url(rtmp_video_url),
  68. 'format_id': rendition.get('bitrate'),
  69. 'width': int(rendition.get('width')),
  70. 'height': int(rendition.get('height')),
  71. })
  72. except (KeyError, TypeError):
  73. raise ExtractorError('Invalid rendition field.')
  74. self._sort_formats(formats)
  75. return formats
  76. def _get_video_info(self, itemdoc):
  77. uri = itemdoc.find('guid').text
  78. video_id = self._id_from_uri(uri)
  79. self.report_extraction(video_id)
  80. mediagen_url = itemdoc.find('%s/%s' % (_media_xml_tag('group'), _media_xml_tag('content'))).attrib['url']
  81. # Remove the templates, like &device={device}
  82. mediagen_url = re.sub(r'&[^=]*?={.*?}(?=(&|$))', '', mediagen_url)
  83. if 'acceptMethods' not in mediagen_url:
  84. mediagen_url += '&acceptMethods=fms'
  85. mediagen_doc = self._download_xml(mediagen_url, video_id,
  86. 'Downloading video urls')
  87. description_node = itemdoc.find('description')
  88. if description_node is not None:
  89. description = description_node.text.strip()
  90. else:
  91. description = None
  92. title_el = None
  93. if title_el is None:
  94. title_el = find_xpath_attr(
  95. itemdoc, './/{http://search.yahoo.com/mrss/}category',
  96. 'scheme', 'urn:mtvn:video_title')
  97. if title_el is None:
  98. title_el = itemdoc.find('.//{http://search.yahoo.com/mrss/}title')
  99. if title_el is None:
  100. title_el = itemdoc.find('.//title')
  101. if title_el.text is None:
  102. title_el = None
  103. title = title_el.text
  104. if title is None:
  105. raise ExtractorError('Could not find video title')
  106. title = title.strip()
  107. # This a short id that's used in the webpage urls
  108. mtvn_id = None
  109. mtvn_id_node = find_xpath_attr(itemdoc, './/{http://search.yahoo.com/mrss/}category',
  110. 'scheme', 'urn:mtvn:id')
  111. if mtvn_id_node is not None:
  112. mtvn_id = mtvn_id_node.text
  113. return {
  114. 'title': title,
  115. 'formats': self._extract_video_formats(mediagen_doc, mtvn_id),
  116. 'id': video_id,
  117. 'thumbnail': self._get_thumbnail_url(uri, itemdoc),
  118. 'description': description,
  119. }
  120. def _get_videos_info(self, uri):
  121. video_id = self._id_from_uri(uri)
  122. feed_url = self._get_feed_url(uri)
  123. data = compat_urllib_parse.urlencode({'uri': uri})
  124. idoc = self._download_xml(
  125. feed_url + '?' + data, video_id,
  126. 'Downloading info', transform_source=fix_xml_ampersands)
  127. return [self._get_video_info(item) for item in idoc.findall('.//item')]
  128. def _real_extract(self, url):
  129. title = url_basename(url)
  130. webpage = self._download_webpage(url, title)
  131. try:
  132. # the url can be http://media.mtvnservices.com/fb/{mgid}.swf
  133. # or http://media.mtvnservices.com/{mgid}
  134. og_url = self._og_search_video_url(webpage)
  135. mgid = url_basename(og_url)
  136. if mgid.endswith('.swf'):
  137. mgid = mgid[:-4]
  138. except RegexNotFoundError:
  139. mgid = None
  140. if mgid is None or ':' not in mgid:
  141. mgid = self._search_regex(
  142. [r'data-mgid="(.*?)"', r'swfobject.embedSWF\(".*?(mgid:.*?)"'],
  143. webpage, u'mgid')
  144. return self._get_videos_info(mgid)
  145. class MTVServicesEmbeddedIE(MTVServicesInfoExtractor):
  146. IE_NAME = 'mtvservices:embedded'
  147. _VALID_URL = r'https?://media\.mtvnservices\.com/embed/(?P<mgid>.+?)(\?|/|$)'
  148. _TEST = {
  149. # From http://www.thewrap.com/peter-dinklage-sums-up-game-of-thrones-in-45-seconds-video/
  150. 'url': 'http://media.mtvnservices.com/embed/mgid:uma:video:mtv.com:1043906/cp~vid%3D1043906%26uri%3Dmgid%3Auma%3Avideo%3Amtv.com%3A1043906',
  151. 'md5': 'cb349b21a7897164cede95bd7bf3fbb9',
  152. 'info_dict': {
  153. 'id': '1043906',
  154. 'ext': 'mp4',
  155. 'title': 'Peter Dinklage Sums Up \'Game Of Thrones\' In 45 Seconds',
  156. 'description': '"Sexy sexy sexy, stabby stabby stabby, beautiful language," says Peter Dinklage as he tries summarizing "Game of Thrones" in under a minute.',
  157. },
  158. }
  159. def _get_feed_url(self, uri):
  160. video_id = self._id_from_uri(uri)
  161. site_id = uri.replace(video_id, '')
  162. config_url = 'http://media.mtvnservices.com/pmt/e1/players/{0}/config.xml'.format(site_id)
  163. config_doc = self._download_xml(config_url, video_id)
  164. feed_node = config_doc.find('.//feed')
  165. feed_url = feed_node.text.strip().split('?')[0]
  166. return feed_url
  167. def _real_extract(self, url):
  168. mobj = re.match(self._VALID_URL, url)
  169. mgid = mobj.group('mgid')
  170. return self._get_videos_info(mgid)
  171. class MTVIE(MTVServicesInfoExtractor):
  172. _VALID_URL = r'''(?x)^https?://
  173. (?:(?:www\.)?mtv\.com/videos/.+?/(?P<videoid>[0-9]+)/[^/]+$|
  174. m\.mtv\.com/videos/video\.rbml\?.*?id=(?P<mgid>[^&]+))'''
  175. _FEED_URL = 'http://www.mtv.com/player/embed/AS3/rss/'
  176. _TESTS = [
  177. {
  178. 'url': 'http://www.mtv.com/videos/misc/853555/ours-vh1-storytellers.jhtml',
  179. 'file': '853555.mp4',
  180. 'md5': '850f3f143316b1e71fa56a4edfd6e0f8',
  181. 'info_dict': {
  182. 'title': 'Taylor Swift - "Ours (VH1 Storytellers)"',
  183. 'description': 'Album: Taylor Swift performs "Ours" for VH1 Storytellers at Harvey Mudd College.',
  184. },
  185. },
  186. {
  187. 'add_ie': ['Vevo'],
  188. 'url': 'http://www.mtv.com/videos/taylor-swift/916187/everything-has-changed-ft-ed-sheeran.jhtml',
  189. 'file': 'USCJY1331283.mp4',
  190. 'md5': '73b4e7fcadd88929292fe52c3ced8caf',
  191. 'info_dict': {
  192. 'title': 'Everything Has Changed',
  193. 'upload_date': '20130606',
  194. 'uploader': 'Taylor Swift',
  195. },
  196. 'skip': 'VEVO is only available in some countries',
  197. },
  198. ]
  199. def _get_thumbnail_url(self, uri, itemdoc):
  200. return 'http://mtv.mtvnimages.com/uri/' + uri
  201. def _real_extract(self, url):
  202. mobj = re.match(self._VALID_URL, url)
  203. video_id = mobj.group('videoid')
  204. uri = mobj.groupdict().get('mgid')
  205. if uri is None:
  206. webpage = self._download_webpage(url, video_id)
  207. # Some videos come from Vevo.com
  208. m_vevo = re.search(r'isVevoVideo = true;.*?vevoVideoId = "(.*?)";',
  209. webpage, re.DOTALL)
  210. if m_vevo:
  211. vevo_id = m_vevo.group(1);
  212. self.to_screen('Vevo video detected: %s' % vevo_id)
  213. return self.url_result('vevo:%s' % vevo_id, ie='Vevo')
  214. uri = self._html_search_regex(r'/uri/(.*?)\?', webpage, 'uri')
  215. return self._get_videos_info(uri)
  216. class MTVIggyIE(MTVServicesInfoExtractor):
  217. IE_NAME = 'mtviggy.com'
  218. _VALID_URL = r'https?://www\.mtviggy\.com/videos/.+'
  219. _TEST = {
  220. 'url': 'http://www.mtviggy.com/videos/arcade-fire-behind-the-scenes-at-the-biggest-music-experiment-yet/',
  221. 'info_dict': {
  222. 'id': '984696',
  223. 'ext': 'mp4',
  224. 'title': 'Arcade Fire: Behind the Scenes at the Biggest Music Experiment Yet',
  225. }
  226. }
  227. _FEED_URL = 'http://all.mtvworldverticals.com/feed-xml/'