1 from __future__ 
import unicode_literals
 
   5 from .common 
import InfoExtractor
 
  23 def _media_xml_tag(tag
): 
  24     return '{http://search.yahoo.com/mrss/}%s' % tag
 
  27 class MTVServicesInfoExtractor(InfoExtractor
): 
  28     _MOBILE_TEMPLATE 
= None 
  32     def _id_from_uri(uri
): 
  33         return uri
.split(':')[-1] 
  35     # This was originally implemented for ComedyCentral, but it also works here 
  37     def _transform_rtmp_url(rtmp_video_url
): 
  38         m 
= re
.match(r
'^rtmpe?://.*?/(?P<finalid>gsp\..+?/.*)$', rtmp_video_url
) 
  41         base 
= 'http://viacommtvstrmfs.fplive.net/' 
  42         return base 
+ m
.group('finalid') 
  44     def _get_feed_url(self
, uri
): 
  47     def _get_thumbnail_url(self
, uri
, itemdoc
): 
  48         search_path 
= '%s/%s' % (_media_xml_tag('group'), _media_xml_tag('thumbnail')) 
  49         thumb_node 
= itemdoc
.find(search_path
) 
  50         if thumb_node 
is None: 
  53             return thumb_node
.attrib
['url'] 
  55     def _extract_mobile_video_formats(self
, mtvn_id
): 
  56         webpage_url 
= self
._MOBILE
_TEMPLATE 
% mtvn_id
 
  57         req 
= sanitized_Request(webpage_url
) 
  58         # Otherwise we get a webpage that would execute some javascript 
  59         req
.add_header('User-Agent', 'curl/7') 
  60         webpage 
= self
._download
_webpage
(req
, mtvn_id
, 
  61                                          'Downloading mobile page') 
  62         metrics_url 
= unescapeHTML(self
._search
_regex
(r
'<a href="(http://metrics.+?)"', webpage
, 'url')) 
  63         req 
= HEADRequest(metrics_url
) 
  64         response 
= self
._request
_webpage
(req
, mtvn_id
, 'Resolving url') 
  65         url 
= response
.geturl() 
  66         # Transform the url to get the best quality: 
  67         url 
= re
.sub(r
'.+pxE=mp4', 'http://mtvnmobile.vo.llnwd.net/kip0/_pxn=0+_pxK=18639+_pxE=mp4', url
, 1) 
  68         return [{'url': url
, 'ext': 'mp4'}] 
  70     def _extract_video_formats(self
, mdoc
, mtvn_id
): 
  71         if re
.match(r
'.*/(error_country_block\.swf|geoblock\.mp4|copyright_error\.flv(?:\?geo\b.+?)?)$', mdoc
.find('.//src').text
) is not None: 
  72             if mtvn_id 
is not None and self
._MOBILE
_TEMPLATE 
is not None: 
  73                 self
.to_screen('The normal version is not available from your ' 
  74                                'country, trying with the mobile version') 
  75                 return self
._extract
_mobile
_video
_formats
(mtvn_id
) 
  76             raise ExtractorError('This video is not available from your country.', 
  80         for rendition 
in mdoc
.findall('.//rendition'): 
  82                 _
, _
, ext 
= rendition
.attrib
['type'].partition('/') 
  83                 rtmp_video_url 
= rendition
.find('./src').text
 
  84                 if rtmp_video_url
.endswith('siteunavail.png'): 
  88                     'url': self
._transform
_rtmp
_url
(rtmp_video_url
), 
  89                     'format_id': rendition
.get('bitrate'), 
  90                     'width': int(rendition
.get('width')), 
  91                     'height': int(rendition
.get('height')), 
  93             except (KeyError, TypeError): 
  94                 raise ExtractorError('Invalid rendition field.') 
  95         self
._sort
_formats
(formats
) 
  98     def _extract_subtitles(self
, mdoc
, mtvn_id
): 
 100         for transcript 
in mdoc
.findall('.//transcript'): 
 101             if transcript
.get('kind') != 'captions': 
 103             lang 
= transcript
.get('srclang') 
 105                 'url': compat_str(typographic
.get('src')), 
 106                 'ext': typographic
.get('format') 
 107             } for typographic 
in transcript
.findall('./typographic')] 
 110     def _get_video_info(self
, itemdoc
): 
 111         uri 
= itemdoc
.find('guid').text
 
 112         video_id 
= self
._id
_from
_uri
(uri
) 
 113         self
.report_extraction(video_id
) 
 114         content_el 
= itemdoc
.find('%s/%s' % (_media_xml_tag('group'), _media_xml_tag('content'))) 
 115         mediagen_url 
= content_el
.attrib
['url'] 
 116         # Remove the templates, like &device={device} 
 117         mediagen_url 
= re
.sub(r
'&[^=]*?={.*?}(?=(&|$))', '', mediagen_url
) 
 118         if 'acceptMethods' not in mediagen_url
: 
 119             mediagen_url 
+= '&' if '?' in mediagen_url 
else '?' 
 120             mediagen_url 
+= 'acceptMethods=fms' 
 122         mediagen_doc 
= self
._download
_xml
(mediagen_url
, video_id
, 
 123                                           'Downloading video urls') 
 125         item 
= mediagen_doc
.find('./video/item') 
 126         if item 
is not None and item
.get('type') == 'text': 
 127             message 
= '%s returned error: ' % self
.IE_NAME
 
 128             if item
.get('code') is not None: 
 129                 message 
+= '%s - ' % item
.get('code') 
 131             raise ExtractorError(message
, expected
=True) 
 133         description_node 
= itemdoc
.find('description') 
 134         if description_node 
is not None: 
 135             description 
= description_node
.text
.strip() 
 141             title_el 
= find_xpath_attr( 
 142                 itemdoc
, './/{http://search.yahoo.com/mrss/}category', 
 143                 'scheme', 'urn:mtvn:video_title') 
 145             title_el 
= itemdoc
.find('.//{http://search.yahoo.com/mrss/}title') 
 147             title_el 
= itemdoc
.find('.//title') or itemdoc
.find('./title') 
 148             if title_el
.text 
is None: 
 151         title 
= title_el
.text
 
 153             raise ExtractorError('Could not find video title') 
 154         title 
= title
.strip() 
 156         # This a short id that's used in the webpage urls 
 158         mtvn_id_node 
= find_xpath_attr(itemdoc
, './/{http://search.yahoo.com/mrss/}category', 
 159                                        'scheme', 'urn:mtvn:id') 
 160         if mtvn_id_node 
is not None: 
 161             mtvn_id 
= mtvn_id_node
.text
 
 165             'formats': self
._extract
_video
_formats
(mediagen_doc
, mtvn_id
), 
 166             'subtitles': self
._extract
_subtitles
(mediagen_doc
, mtvn_id
), 
 168             'thumbnail': self
._get
_thumbnail
_url
(uri
, itemdoc
), 
 169             'description': description
, 
 170             'duration': float_or_none(content_el
.attrib
.get('duration')), 
 173     def _get_feed_query(self
, uri
): 
 176             data
['lang'] = self
._LANG
 
 177         return compat_urllib_parse
.urlencode(data
) 
 179     def _get_videos_info(self
, uri
): 
 180         video_id 
= self
._id
_from
_uri
(uri
) 
 181         feed_url 
= self
._get
_feed
_url
(uri
) 
 182         info_url 
= feed_url 
+ '?' + self
._get
_feed
_query
(uri
) 
 183         return self
._get
_videos
_info
_from
_url
(info_url
, video_id
) 
 185     def _get_videos_info_from_url(self
, url
, video_id
): 
 186         idoc 
= self
._download
_xml
( 
 188             'Downloading info', transform_source
=fix_xml_ampersands
) 
 189         return self
.playlist_result( 
 190             [self
._get
_video
_info
(item
) for item 
in idoc
.findall('.//item')]) 
 192     def _extract_mgid(self
, webpage
): 
 194             # the url can be http://media.mtvnservices.com/fb/{mgid}.swf 
 195             # or http://media.mtvnservices.com/{mgid} 
 196             og_url 
= self
._og
_search
_video
_url
(webpage
) 
 197             mgid 
= url_basename(og_url
) 
 198             if mgid
.endswith('.swf'): 
 200         except RegexNotFoundError
: 
 203         if mgid 
is None or ':' not in mgid
: 
 204             mgid 
= self
._search
_regex
( 
 205                 [r
'data-mgid="(.*?)"', r
'swfobject.embedSWF\(".*?(mgid:.*?)"'], 
 206                 webpage
, 'mgid', default
=None) 
 209             sm4_embed 
= self
._html
_search
_meta
( 
 210                 'sm4:video:embed', webpage
, 'sm4 embed', default
='') 
 211             mgid 
= self
._search
_regex
( 
 212                 r
'embed/(mgid:.+?)["\'&?
/]', sm4_embed, 'mgid
') 
 215     def _real_extract(self, url): 
 216         title = url_basename(url) 
 217         webpage = self._download_webpage(url, title) 
 218         mgid = self._extract_mgid(webpage) 
 219         videos_info = self._get_videos_info(mgid) 
 223 class MTVServicesEmbeddedIE(MTVServicesInfoExtractor): 
 224     IE_NAME = 'mtvservices
:embedded
' 
 225     _VALID_URL = r'https?
://media\
.mtvnservices\
.com
/embed
/(?P
<mgid
>.+?
)(\?|
/|$
)' 
 228         # From http://www.thewrap.com/peter-dinklage-sums-up-game-of-thrones-in-45-seconds-video/ 
 229         'url
': 'http
://media
.mtvnservices
.com
/embed
/mgid
:uma
:video
:mtv
.com
:1043906/cp~vid
%3D1043906
%26uri
%3Dmgid
%3Auma
%3Avideo
%3Amtv
.com
%3A1043906
', 
 230         'md5
': 'cb349b21a7897164cede95bd7bf3fbb9
', 
 234             'title
': 'Peter Dinklage Sums Up 
\'Game Of Thrones
\' In 
45 Seconds
', 
 235             'description
': '"Sexy sexy sexy, stabby stabby stabby, beautiful language," says Peter Dinklage 
as he tries summarizing 
"Game of Thrones" in under a minute
.', 
 240     def _extract_url(webpage): 
 242             r'<iframe
[^
>]+?src
=(["\'])(?P<url>(?:https?:)?//media.mtvnservices.com/embed/.+?)\1', webpage) 
 244             return mobj.group('url') 
 246     def _get_feed_url(self, uri): 
 247         video_id = self._id_from_uri(uri) 
 248         site_id = uri.replace(video_id, '') 
 249         config_url = ('http://media.mtvnservices.com/pmt/e1/players/{0}/' 
 250                       'context4/context5/config.xml'.format(site_id)) 
 251         config_doc = self._download_xml(config_url, video_id) 
 252         feed_node = config_doc.find('.//feed') 
 253         feed_url = feed_node.text.strip().split('?')[0] 
 256     def _real_extract(self, url): 
 257         mobj = re.match(self._VALID_URL, url) 
 258         mgid = mobj.group('mgid') 
 259         return self._get_videos_info(mgid) 
 262 class MTVIE(MTVServicesInfoExtractor): 
 263     _VALID_URL = r'''(?x)^https?:// 
 264         (?:(?:www\.)?mtv\.com/videos/.+?/(?P<videoid>[0-9]+)/[^/]+$| 
 265            m\.mtv\.com/videos/video\.rbml\?.*?id=(?P<mgid>[^&]+))''' 
 267     _FEED_URL = 'http://www.mtv.com/player/embed/AS3/rss/' 
 271             'url': 'http://www.mtv.com/videos/misc/853555/ours-vh1-storytellers.jhtml', 
 272             'md5': '850f3f143316b1e71fa56a4edfd6e0f8', 
 276                 'title': 'Taylor Swift - "Ours (VH1 Storytellers
)"', 
 277                 'description': 'Album: Taylor Swift performs "Ours
" for VH1 Storytellers at Harvey Mudd College.', 
 282     def _get_thumbnail_url(self, uri, itemdoc): 
 283         return 'http://mtv.mtvnimages.com/uri/' + uri 
 285     def _real_extract(self, url): 
 286         mobj = re.match(self._VALID_URL, url) 
 287         video_id = mobj.group('videoid') 
 288         uri = mobj.groupdict().get('mgid') 
 290             webpage = self._download_webpage(url, video_id) 
 292             # Some videos come from Vevo.com 
 294                 r'(?s)isVevoVideo = true;.*?vevoVideoId = "(.*?
)";', webpage) 
 296                 vevo_id = m_vevo.group(1) 
 297                 self.to_screen('Vevo video detected: %s' % vevo_id) 
 298                 return self.url_result('vevo:%s' % vevo_id, ie='Vevo') 
 300             uri = self._html_search_regex(r'/uri/(.*?)\?', webpage, 'uri') 
 301         return self._get_videos_info(uri) 
 304 class MTVIggyIE(MTVServicesInfoExtractor): 
 305     IE_NAME = 'mtviggy.com' 
 306     _VALID_URL = r'https?://www\.mtviggy\.com/videos/.+' 
 308         'url': 'http://www.mtviggy.com/videos/arcade-fire-behind-the-scenes-at-the-biggest-music-experiment-yet/', 
 312             'title': 'Arcade Fire: Behind the Scenes at the Biggest Music Experiment Yet', 
 315     _FEED_URL = 'http://all.mtvworldverticals.com/feed-xml/' 
 318 class MTVDEIE(MTVServicesInfoExtractor): 
 320     _VALID_URL = r'https?://(?:www\.)?mtv\.de/(?:artists|shows|news)/(?:[^/]+/)*(?P<id>\d+)-[^/#?]+/*(?:[#?].*)?$' 
 322         'url': 'http://www.mtv.de/artists/10571-cro/videos/61131-traum', 
 324             'id': 'music_video-a50bc5f0b3aa4b3190aa', 
 326             'title': 'MusicVideo_cro-traum', 
 327             'description': 'Cro - Traum', 
 331             'skip_download': True, 
 334         # mediagen URL without query (e.g. http://videos.mtvnn.com/mediagen/e865da714c166d18d6f80893195fcb97) 
 335         'url': 'http://www.mtv.de/shows/933-teen-mom-2/staffeln/5353/folgen/63565-enthullungen', 
 337             'id': 'local_playlist-f5ae778b9832cc837189', 
 339             'title': 'Episode_teen-mom-2_shows_season-5_episode-1_full-episode_part1', 
 343             'skip_download': True, 
 346         # single video in pagePlaylist with different id 
 347         'url': 'http://www.mtv.de/news/77491-mtv-movies-spotlight-pixels-teil-3', 
 349             'id': 'local_playlist-4e760566473c4c8c5344', 
 351             'title': 'Article_mtv-movies-spotlight-pixels-teil-3_short-clips_part1', 
 352             'description': 'MTV Movies Supercut', 
 356             'skip_download': True, 
 360     def _real_extract(self, url): 
 361         video_id = self._match_id(url) 
 363         webpage = self._download_webpage(url, video_id) 
 365         playlist = self._parse_json( 
 367                 r'window\.pagePlaylist\s*=\s*(\[.+?\]);\n', webpage, 'page playlist'), 
 370         # news pages contain single video in playlist with different id 
 371         if len(playlist) == 1: 
 372             return self._get_videos_info_from_url(playlist[0]['mrss'], video_id) 
 374         for item in playlist: 
 375             item_id = item.get('id') 
 376             if item_id and compat_str(item_id) == video_id: 
 377                 return self._get_videos_info_from_url(item['mrss'], video_id)