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