]>
Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/mtv.py
bc7f49ebbac86cda7aa1bb711076b783e24bfea8
1 from __future__
import unicode_literals
5 from . subtitles
import SubtitlesInfoExtractor
22 def _media_xml_tag ( tag
):
23 return '{http://search.yahoo.com/mrss/} %s ' % tag
26 class MTVServicesInfoExtractor ( SubtitlesInfoExtractor
):
27 _MOBILE_TEMPLATE
= None
30 def _id_from_uri ( uri
):
31 return uri
. split ( ':' )[- 1 ]
33 # This was originally implemented for ComedyCentral, but it also works here
35 def _transform_rtmp_url ( rtmp_video_url
):
36 m
= re
. match ( r
'^rtmpe?://.*?/(?P<finalid>gsp\..+?/.*)$' , rtmp_video_url
)
39 base
= 'http://viacommtvstrmfs.fplive.net/'
40 return base
+ m
. group ( 'finalid' )
42 def _get_feed_url ( self
, uri
):
45 def _get_thumbnail_url ( self
, uri
, itemdoc
):
46 search_path
= ' %s / %s ' % ( _media_xml_tag ( 'group' ), _media_xml_tag ( 'thumbnail' ))
47 thumb_node
= itemdoc
. find ( search_path
)
48 if thumb_node
is None :
51 return thumb_node
. attrib
[ 'url' ]
53 def _extract_mobile_video_formats ( self
, mtvn_id
):
54 webpage_url
= self
._ MOBILE
_ TEMPLATE
% mtvn_id
55 req
= compat_urllib_request
. Request ( webpage_url
)
56 # Otherwise we get a webpage that would execute some javascript
57 req
. add_header ( 'User-Agent' , 'curl/7' )
58 webpage
= self
._ download
_ webpage
( req
, mtvn_id
,
59 'Downloading mobile page' )
60 metrics_url
= unescapeHTML ( self
._ search
_ regex
( r
'<a href="(http://metrics.+?)"' , webpage
, 'url' ))
61 req
= HEADRequest ( metrics_url
)
62 response
= self
._ request
_ webpage
( req
, mtvn_id
, 'Resolving url' )
63 url
= response
. geturl ()
64 # Transform the url to get the best quality:
65 url
= re
. sub ( r
'.+pxE=mp4' , 'http://mtvnmobile.vo.llnwd.net/kip0/_pxn=0+_pxK=18639+_pxE=mp4' , url
, 1 )
66 return [{ 'url' : url
, 'ext' : 'mp4' }]
68 def _extract_video_formats ( self
, mdoc
, mtvn_id
):
69 if re
. match ( r
'.*/(error_country_block\.swf|geoblock\.mp4)$' , mdoc
. find ( './/src' ). text
) is not None :
70 if mtvn_id
is not None and self
._ MOBILE
_ TEMPLATE
is not None :
71 self
. to_screen ( 'The normal version is not available from your '
72 'country, trying with the mobile version' )
73 return self
._ extract
_ mobile
_ video
_ formats
( mtvn_id
)
74 raise ExtractorError ( 'This video is not available from your country.' ,
78 for rendition
in mdoc
. findall ( './/rendition' ):
80 _
, _
, ext
= rendition
. attrib
[ 'type' ]. partition ( '/' )
81 rtmp_video_url
= rendition
. find ( './src' ). text
82 if rtmp_video_url
. endswith ( 'siteunavail.png' ):
86 'url' : self
._ transform
_ rtmp
_u rl
( rtmp_video_url
),
87 'format_id' : rendition
. get ( 'bitrate' ),
88 'width' : int ( rendition
. get ( 'width' )),
89 'height' : int ( rendition
. get ( 'height' )),
91 except ( KeyError , TypeError ):
92 raise ExtractorError ( 'Invalid rendition field.' )
93 self
._ sort
_ formats
( formats
)
96 def _extract_subtitles ( self
, mdoc
, mtvn_id
):
100 'eia-608' : 'cea-608' ,
103 subtitles_format
= FORMATS
. get (
104 self
._ downloader
. params
. get ( 'subtitlesformat' ), 'ttml' )
105 for transcript
in mdoc
. findall ( './/transcript' ):
106 if transcript
. get ( 'kind' ) != 'captions' :
108 lang
= transcript
. get ( 'srclang' )
109 for typographic
in transcript
. findall ( './typographic' ):
110 captions_format
= typographic
. get ( 'format' )
111 if captions_format
== subtitles_format
:
112 subtitles
[ lang
] = compat_str ( typographic
. get ( 'src' ))
114 if self
._ downloader
. params
. get ( 'listsubtitles' , False ):
115 self
._l ist
_ available
_ subtitles
( mtvn_id
, subtitles
)
116 return self
. extract_subtitles ( mtvn_id
, subtitles
)
118 def _get_video_info ( self
, itemdoc
):
119 uri
= itemdoc
. find ( 'guid' ). text
120 video_id
= self
._ id
_ from
_u ri
( uri
)
121 self
. report_extraction ( video_id
)
122 mediagen_url
= itemdoc
. find ( ' %s / %s ' % ( _media_xml_tag ( 'group' ), _media_xml_tag ( 'content' ))). attrib
[ 'url' ]
123 # Remove the templates, like &device={device}
124 mediagen_url
= re
. sub ( r
'&[^=]*?={.*?}(?=(&|$))' , '' , mediagen_url
)
125 if 'acceptMethods' not in mediagen_url
:
126 mediagen_url
+= '&acceptMethods=fms'
128 mediagen_doc
= self
._ download
_ xml
( mediagen_url
, video_id
,
129 'Downloading video urls' )
131 description_node
= itemdoc
. find ( 'description' )
132 if description_node
is not None :
133 description
= description_node
. text
. strip ()
139 title_el
= find_xpath_attr (
140 itemdoc
, './/{http://search.yahoo.com/mrss/}category' ,
141 'scheme' , 'urn:mtvn:video_title' )
143 title_el
= itemdoc
. find ( './/{http://search.yahoo.com/mrss/}title' )
145 title_el
= itemdoc
. find ( './/title' )
146 if title_el
. text
is None :
149 title
= title_el
. text
151 raise ExtractorError ( 'Could not find video title' )
152 title
= title
. strip ()
154 # This a short id that's used in the webpage urls
156 mtvn_id_node
= find_xpath_attr ( itemdoc
, './/{http://search.yahoo.com/mrss/}category' ,
157 'scheme' , 'urn:mtvn:id' )
158 if mtvn_id_node
is not None :
159 mtvn_id
= mtvn_id_node
. text
163 'formats' : self
._ extract
_ video
_ formats
( mediagen_doc
, mtvn_id
),
164 'subtitles' : self
._ extract
_ subtitles
( mediagen_doc
, mtvn_id
),
166 'thumbnail' : self
._ get
_ thumbnail
_u rl
( uri
, itemdoc
),
167 'description' : description
,
170 def _get_videos_info ( self
, uri
):
171 video_id
= self
._ id
_ from
_u ri
( uri
)
172 feed_url
= self
._ get
_ feed
_u rl
( uri
)
173 data
= compat_urllib_parse
. urlencode ({ 'uri' : uri
})
174 idoc
= self
._ download
_ xml
(
175 feed_url
+ '?' + data
, video_id
,
176 'Downloading info' , transform_source
= fix_xml_ampersands
)
177 return self
. playlist_result (
178 [ self
._ get
_ video
_ info
( item
) for item
in idoc
. findall ( './/item' )])
180 def _real_extract ( self
, url
):
181 title
= url_basename ( url
)
182 webpage
= self
._ download
_ webpage
( url
, title
)
184 # the url can be http://media.mtvnservices.com/fb/{mgid}.swf
185 # or http://media.mtvnservices.com/{mgid}
186 og_url
= self
._ og
_ search
_ video
_u rl
( webpage
)
187 mgid
= url_basename ( og_url
)
188 if mgid
. endswith ( '.swf' ):
190 except RegexNotFoundError
:
193 if mgid
is None or ':' not in mgid
:
194 mgid
= self
._ search
_ regex
(
195 [ r
'data-mgid="(.*?)"' , r
'swfobject.embedSWF\(".*?(mgid:.*?)"' ],
198 videos_info
= self
._ get
_ videos
_ info
( mgid
)
199 if self
._ downloader
. params
. get ( 'listsubtitles' , False ):
204 class MTVServicesEmbeddedIE ( MTVServicesInfoExtractor
):
205 IE_NAME
= 'mtvservices:embedded'
206 _VALID_URL
= r
'https?://media\.mtvnservices\.com/embed/(?P<mgid>.+?)(\?|/|$)'
209 # From http://www.thewrap.com/peter-dinklage-sums-up-game-of-thrones-in-45-seconds-video/
210 'url' : 'http://media.mtvnservices.com/embed/mgid:uma:video:mtv.com:1043906/cp~vid%3D1043906%26uri%3Dmgid%3Auma%3Avideo%3Amtv.com%3A1043906' ,
211 'md5' : 'cb349b21a7897164cede95bd7bf3fbb9' ,
215 'title' : 'Peter Dinklage Sums Up \' Game Of Thrones \' In 45 Seconds' ,
216 'description' : '"Sexy sexy sexy, stabby stabby stabby, beautiful language," says Peter Dinklage as he tries summarizing "Game of Thrones" in under a minute.' ,
220 def _get_feed_url ( self
, uri
):
221 video_id
= self
._ id
_ from
_u ri
( uri
)
222 site_id
= uri
. replace ( video_id
, '' )
223 config_url
= ( 'http://media.mtvnservices.com/pmt/e1/players/ {0} /'
224 'context4/context5/config.xml' . format ( site_id
))
225 config_doc
= self
._ download
_ xml
( config_url
, video_id
)
226 feed_node
= config_doc
. find ( './/feed' )
227 feed_url
= feed_node
. text
. strip (). split ( '?' )[ 0 ]
230 def _real_extract ( self
, url
):
231 mobj
= re
. match ( self
._ VALID
_U RL
, url
)
232 mgid
= mobj
. group ( 'mgid' )
233 return self
._ get
_ videos
_ info
( mgid
)
236 class MTVIE ( MTVServicesInfoExtractor
):
237 _VALID_URL
= r
'''(?x)^https?://
238 (?:(?:www\.)?mtv\.com/videos/.+?/(?P<videoid>[0-9]+)/[^/]+$|
239 m\.mtv\.com/videos/video\.rbml\?.*?id=(?P<mgid>[^&]+))'''
241 _FEED_URL
= 'http://www.mtv.com/player/embed/AS3/rss/'
245 'url' : 'http://www.mtv.com/videos/misc/853555/ours-vh1-storytellers.jhtml' ,
246 'md5' : '850f3f143316b1e71fa56a4edfd6e0f8' ,
250 'title' : 'Taylor Swift - "Ours (VH1 Storytellers)"' ,
251 'description' : 'Album: Taylor Swift performs "Ours" for VH1 Storytellers at Harvey Mudd College.' ,
256 def _get_thumbnail_url ( self
, uri
, itemdoc
):
257 return 'http://mtv.mtvnimages.com/uri/' + uri
259 def _real_extract ( self
, url
):
260 mobj
= re
. match ( self
._ VALID
_U RL
, url
)
261 video_id
= mobj
. group ( 'videoid' )
262 uri
= mobj
. groupdict (). get ( 'mgid' )
264 webpage
= self
._ download
_ webpage
( url
, video_id
)
266 # Some videos come from Vevo.com
268 r
'(?s)isVevoVideo = true;.*?vevoVideoId = "(.*?)";' , webpage
)
270 vevo_id
= m_vevo
. group ( 1 )
271 self
. to_screen ( 'Vevo video detected: %s ' % vevo_id
)
272 return self
. url_result ( 'vevo: %s ' % vevo_id
, ie
= 'Vevo' )
274 uri
= self
._ html
_ search
_ regex
( r
'/uri/(.*?)\?' , webpage
, 'uri' )
275 return self
._ get
_ videos
_ info
( uri
)
278 class MTVIggyIE ( MTVServicesInfoExtractor
):
279 IE_NAME
= 'mtviggy.com'
280 _VALID_URL
= r
'https?://www\.mtviggy\.com/videos/.+'
282 'url' : 'http://www.mtviggy.com/videos/arcade-fire-behind-the-scenes-at-the-biggest-music-experiment-yet/' ,
286 'title' : 'Arcade Fire: Behind the Scenes at the Biggest Music Experiment Yet' ,
289 _FEED_URL
= 'http://all.mtvworldverticals.com/feed-xml/'