]>
Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/mtv.py 
   1  from  __future__ 
import  unicode_literals
   5  from  . common 
import  InfoExtractor
  21  def  _media_xml_tag ( tag
):   22      return  '{http://search.yahoo.com/mrss/} %s '  %  tag
  25  class  MTVServicesInfoExtractor ( InfoExtractor
):   26      _MOBILE_TEMPLATE 
=  None   29      def  _id_from_uri ( uri
):   30          return  uri
. split ( ':' )[- 1 ]   32      # This was originally implemented for ComedyCentral, but it also works here   34      def  _transform_rtmp_url ( rtmp_video_url
):   35          m 
=  re
. match ( r
'^rtmpe?://.*?/(?P<finalid>gsp\..+?/.*)$' ,  rtmp_video_url
)   38          base 
=  'http://viacommtvstrmfs.fplive.net/'   39          return  base 
+  m
. group ( 'finalid' )   41      def  _get_feed_url ( self
,  uri
):   44      def  _get_thumbnail_url ( self
,  uri
,  itemdoc
):   45          search_path 
=  ' %s / %s '  % ( _media_xml_tag ( 'group' ),  _media_xml_tag ( 'thumbnail' ))   46          thumb_node 
=  itemdoc
. find ( search_path
)   47          if  thumb_node 
is None :   50              return  thumb_node
. attrib
[ 'url' ]   52      def  _extract_mobile_video_formats ( self
,  mtvn_id
):   53          webpage_url 
=  self
._ MOBILE
_ TEMPLATE 
%  mtvn_id
  54          req 
=  compat_urllib_request
. Request ( webpage_url
)   55          # Otherwise we get a webpage that would execute some javascript   56          req
. add_header ( 'Youtubedl-user-agent' ,  'curl/7' )   57          webpage 
=  self
._ download
_ webpage
( req
,  mtvn_id
,   58                                           'Downloading mobile page' )   59          metrics_url 
=  unescapeHTML ( self
._ search
_ regex
( r
'<a href="(http://metrics.+?)"' ,  webpage
,  'url' ))   60          req 
=  HEADRequest ( metrics_url
)   61          response 
=  self
._ request
_ webpage
( req
,  mtvn_id
,  'Resolving url' )   62          url 
=  response
. geturl ()   63          # Transform the url to get the best quality:   64          url 
=  re
. sub ( r
'.+pxE=mp4' ,  'http://mtvnmobile.vo.llnwd.net/kip0/_pxn=0+_pxK=18639+_pxE=mp4' ,  url
,  1 )   65          return  [{ 'url' :  url
,  'ext' :  'mp4' }]   67      def  _extract_video_formats ( self
,  mdoc
,  mtvn_id
):   68          if  re
. match ( r
'.*/(error_country_block\.swf|geoblock\.mp4)$' ,  mdoc
. find ( './/src' ). text
)  is not None :   69              if  mtvn_id 
is not None and  self
._ MOBILE
_ TEMPLATE 
is not None :   70                  self
. to_screen ( 'The normal version is not available from your '   71                                 'country, trying with the mobile version' )   72                  return  self
._ extract
_ mobile
_ video
_ formats
( mtvn_id
)   73              raise  ExtractorError ( 'This video is not available from your country.' ,   77          for  rendition 
in  mdoc
. findall ( './/rendition' ):   79                  _
,  _
,  ext 
=  rendition
. attrib
[ 'type' ]. partition ( '/' )   80                  rtmp_video_url 
=  rendition
. find ( './src' ). text
  81                  formats
. append ({ 'ext' :  ext
,   82                                  'url' :  self
._ transform
_ rtmp
_u rl
( rtmp_video_url
),   83                                  'format_id' :  rendition
. get ( 'bitrate' ),   84                                  'width' :  int ( rendition
. get ( 'width' )),   85                                  'height' :  int ( rendition
. get ( 'height' )),   87              except  ( KeyError ,  TypeError ):   88                  raise  ExtractorError ( 'Invalid rendition field.' )   89          self
._ sort
_ formats
( formats
)   92      def  _get_video_info ( self
,  itemdoc
):   93          uri 
=  itemdoc
. find ( 'guid' ). text
  94          video_id 
=  self
._ id
_ from
_u ri
( uri
)   95          self
. report_extraction ( video_id
)   96          mediagen_url 
=  itemdoc
. find ( ' %s / %s '  % ( _media_xml_tag ( 'group' ),  _media_xml_tag ( 'content' ))). attrib
[ 'url' ]   97          # Remove the templates, like &device={device}   98          mediagen_url 
=  re
. sub ( r
'&[^=]*?={.*?}(?=(&|$))' ,  '' ,  mediagen_url
)   99          if  'acceptMethods'  not in  mediagen_url
:  100              mediagen_url 
+=  '&acceptMethods=fms'  102          mediagen_doc 
=  self
._ download
_ xml
( mediagen_url
,  video_id
,  103                                            'Downloading video urls' )  105          description_node 
=  itemdoc
. find ( 'description' )  106          if  description_node 
is not None :  107              description 
=  description_node
. text
. strip ()  113              title_el 
=  find_xpath_attr (  114                  itemdoc
,  './/{http://search.yahoo.com/mrss/}category' ,  115                  'scheme' ,  'urn:mtvn:video_title' )  117              title_el 
=  itemdoc
. find ( './/{http://search.yahoo.com/mrss/}title' )  119              title_el 
=  itemdoc
. find ( './/title' )  120              if  title_el
. text 
is None :  123          title 
=  title_el
. text
 125              raise  ExtractorError ( 'Could not find video title' )  126          title 
=  title
. strip ()  128          # This a short id that's used in the webpage urls  130          mtvn_id_node 
=  find_xpath_attr ( itemdoc
,  './/{http://search.yahoo.com/mrss/}category' ,  131                                         'scheme' ,  'urn:mtvn:id' )  132          if  mtvn_id_node 
is not None :  133              mtvn_id 
=  mtvn_id_node
. text
 137              'formats' :  self
._ extract
_ video
_ formats
( mediagen_doc
,  mtvn_id
),  139              'thumbnail' :  self
._ get
_ thumbnail
_u rl
( uri
,  itemdoc
),  140              'description' :  description
,  143      def  _get_videos_info ( self
,  uri
):  144          video_id 
=  self
._ id
_ from
_u ri
( uri
)  145          feed_url 
=  self
._ get
_ feed
_u rl
( uri
)  146          data 
=  compat_urllib_parse
. urlencode ({ 'uri' :  uri
})  147          idoc 
=  self
._ download
_ xml
(  148              feed_url 
+  '?'  +  data
,  video_id
,  149              'Downloading info' ,  transform_source
= fix_xml_ampersands
)  150          return  self
. playlist_result (  151              [ self
._ get
_ video
_ info
( item
)  for  item 
in  idoc
. findall ( './/item' )])  153      def  _real_extract ( self
,  url
):  154          title 
=  url_basename ( url
)  155          webpage 
=  self
._ download
_ webpage
( url
,  title
)  157              # the url can be http://media.mtvnservices.com/fb/{mgid}.swf  158              # or http://media.mtvnservices.com/{mgid}  159              og_url 
=  self
._ og
_ search
_ video
_u rl
( webpage
)  160              mgid 
=  url_basename ( og_url
)  161              if  mgid
. endswith ( '.swf' ):  163          except  RegexNotFoundError
:  166          if  mgid 
is None or  ':'  not in  mgid
:  167              mgid 
=  self
._ search
_ regex
(  168                  [ r
'data-mgid="(.*?)"' ,  r
'swfobject.embedSWF\(".*?(mgid:.*?)"' ],  170          return  self
._ get
_ videos
_ info
( mgid
)  173  class  MTVServicesEmbeddedIE ( MTVServicesInfoExtractor
):  174      IE_NAME 
=  'mtvservices:embedded'  175      _VALID_URL 
=  r
'https?://media\.mtvnservices\.com/embed/(?P<mgid>.+?)(\?|/|$)'  178          # From http://www.thewrap.com/peter-dinklage-sums-up-game-of-thrones-in-45-seconds-video/  179          'url' :  'http://media.mtvnservices.com/embed/mgid:uma:video:mtv.com:1043906/cp~vid%3D1043906%26uri%3Dmgid%3Auma%3Avideo%3Amtv.com%3A1043906' ,  180          'md5' :  'cb349b21a7897164cede95bd7bf3fbb9' ,  184              'title' :  'Peter Dinklage Sums Up  \' Game Of Thrones \'  In 45 Seconds' ,  185              'description' :  '"Sexy sexy sexy, stabby stabby stabby, beautiful language," says Peter Dinklage as he tries summarizing "Game of Thrones" in under a minute.' ,  189      def  _get_feed_url ( self
,  uri
):  190          video_id 
=  self
._ id
_ from
_u ri
( uri
)  191          site_id 
=  uri
. replace ( video_id
,  '' )  192          config_url 
= ( 'http://media.mtvnservices.com/pmt/e1/players/ {0} /'  193                        'context4/context5/config.xml' . format ( site_id
))  194          config_doc 
=  self
._ download
_ xml
( config_url
,  video_id
)  195          feed_node 
=  config_doc
. find ( './/feed' )  196          feed_url 
=  feed_node
. text
. strip (). split ( '?' )[ 0 ]  199      def  _real_extract ( self
,  url
):  200          mobj 
=  re
. match ( self
._ VALID
_U RL
,  url
)  201          mgid 
=  mobj
. group ( 'mgid' )  202          return  self
._ get
_ videos
_ info
( mgid
)  205  class  MTVIE ( MTVServicesInfoExtractor
):  206      _VALID_URL 
=  r
'''(?x)^https?://  207          (?:(?:www\.)?mtv\.com/videos/.+?/(?P<videoid>[0-9]+)/[^/]+$|  208             m\.mtv\.com/videos/video\.rbml\?.*?id=(?P<mgid>[^&]+))'''  210      _FEED_URL 
=  'http://www.mtv.com/player/embed/AS3/rss/'  214              'url' :  'http://www.mtv.com/videos/misc/853555/ours-vh1-storytellers.jhtml' ,  215              'file' :  '853555.mp4' ,  216              'md5' :  '850f3f143316b1e71fa56a4edfd6e0f8' ,  218                  'title' :  'Taylor Swift - "Ours (VH1 Storytellers)"' ,  219                  'description' :  'Album: Taylor Swift performs "Ours" for VH1 Storytellers at Harvey Mudd College.' ,  224              'url' :  'http://www.mtv.com/videos/taylor-swift/916187/everything-has-changed-ft-ed-sheeran.jhtml' ,  225              'file' :  'USCJY1331283.mp4' ,  226              'md5' :  '73b4e7fcadd88929292fe52c3ced8caf' ,  228                  'title' :  'Everything Has Changed' ,  229                  'upload_date' :  '20130606' ,  230                  'uploader' :  'Taylor Swift' ,  232              'skip' :  'VEVO is only available in some countries' ,  236      def  _get_thumbnail_url ( self
,  uri
,  itemdoc
):  237          return  'http://mtv.mtvnimages.com/uri/'  +  uri
 239      def  _real_extract ( self
,  url
):  240          mobj 
=  re
. match ( self
._ VALID
_U RL
,  url
)  241          video_id 
=  mobj
. group ( 'videoid' )  242          uri 
=  mobj
. groupdict (). get ( 'mgid' )  244              webpage 
=  self
._ download
_ webpage
( url
,  video_id
)  246              # Some videos come from Vevo.com  247              m_vevo 
=  re
. search ( r
'isVevoVideo = true;.*?vevoVideoId = "(.*?)";' ,  250                  vevo_id 
=  m_vevo
. group ( 1 )  251                  self
. to_screen ( 'Vevo video detected:  %s '  %  vevo_id
)  252                  return  self
. url_result ( 'vevo: %s '  %  vevo_id
,  ie
= 'Vevo' )  254              uri 
=  self
._ html
_ search
_ regex
( r
'/uri/(.*?)\?' ,  webpage
,  'uri' )  255          return  self
._ get
_ videos
_ info
( uri
)  258  class  MTVIggyIE ( MTVServicesInfoExtractor
):  259      IE_NAME 
=  'mtviggy.com'  260      _VALID_URL 
=  r
'https?://www\.mtviggy\.com/videos/.+'  262          'url' :  'http://www.mtviggy.com/videos/arcade-fire-behind-the-scenes-at-the-biggest-music-experiment-yet/' ,  266              'title' :  'Arcade Fire: Behind the Scenes at the Biggest Music Experiment Yet' ,  269      _FEED_URL 
=  'http://all.mtvworldverticals.com/feed-xml/'