]>
Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/mtv.py
e5ca41b4091698ad2a180f9d2ca00b4b96218c1e
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
26 def _id_from_uri(uri
):
27 return uri
.split(':')[-1]
29 # This was originally implemented for ComedyCentral, but it also works here
31 def _transform_rtmp_url(rtmp_video_url
):
32 m
= re
.match(r
'^rtmpe?://.*?/(?P<finalid>gsp\..+?/.*)$', rtmp_video_url
)
35 base
= 'http://mtvnmobile.vo.llnwd.net/kip0/_pxn=1+_pxI0=Ripod-h264+_pxL0=undefined+_pxM0=+_pxK=18639+_pxE=mp4/44620/mtvnorigin/'
36 return base
+ m
.group('finalid')
38 def _get_thumbnail_url(self
, uri
, itemdoc
):
39 search_path
= '%s/%s' % (_media_xml_tag('group'), _media_xml_tag('thumbnail'))
40 thumb_node
= itemdoc
.find(search_path
)
41 if thumb_node
is None:
44 return thumb_node
.attrib
['url']
46 def _extract_mobile_video_formats(self
, mtvn_id
):
47 webpage_url
= self
._MOBILE
_TEMPLATE
% mtvn_id
48 req
= compat_urllib_request
.Request(webpage_url
)
49 # Otherwise we get a webpage that would execute some javascript
50 req
.add_header('Youtubedl-user-agent', 'curl/7')
51 webpage
= self
._download
_webpage
(req
, mtvn_id
,
52 'Downloading mobile page')
53 metrics_url
= unescapeHTML(self
._search
_regex
(r
'<a href="(http://metrics.+?)"', webpage
, 'url'))
54 req
= HEADRequest(metrics_url
)
55 response
= self
._request
_webpage
(req
, mtvn_id
, 'Resolving url')
56 url
= response
.geturl()
57 # Transform the url to get the best quality:
58 url
= re
.sub(r
'.+pxE=mp4', 'http://mtvnmobile.vo.llnwd.net/kip0/_pxn=0+_pxK=18639+_pxE=mp4', url
, 1)
59 return [{'url': url
,'ext': 'mp4'}]
61 def _extract_video_formats(self
, mdoc
, mtvn_id
):
62 if re
.match(r
'.*/(error_country_block\.swf|geoblock\.mp4)$', mdoc
.find('.//src').text
) is not None:
63 if mtvn_id
is not None and self
._MOBILE
_TEMPLATE
is not None:
64 self
.to_screen('The normal version is not available from your '
65 'country, trying with the mobile version')
66 return self
._extract
_mobile
_video
_formats
(mtvn_id
)
67 raise ExtractorError('This video is not available from your country.',
71 for rendition
in mdoc
.findall('.//rendition'):
73 _
, _
, ext
= rendition
.attrib
['type'].partition('/')
74 rtmp_video_url
= rendition
.find('./src').text
75 formats
.append({'ext': ext
,
76 'url': self
._transform
_rtmp
_url
(rtmp_video_url
),
77 'format_id': rendition
.get('bitrate'),
78 'width': int(rendition
.get('width')),
79 'height': int(rendition
.get('height')),
81 except (KeyError, TypeError):
82 raise ExtractorError('Invalid rendition field.')
83 self
._sort
_formats
(formats
)
86 def _get_video_info(self
, itemdoc
):
87 uri
= itemdoc
.find('guid').text
88 video_id
= self
._id
_from
_uri
(uri
)
89 self
.report_extraction(video_id
)
90 mediagen_url
= itemdoc
.find('%s/%s' % (_media_xml_tag('group'), _media_xml_tag('content'))).attrib
['url']
91 # Remove the templates, like &device={device}
92 mediagen_url
= re
.sub(r
'&[^=]*?={.*?}(?=(&|$))', '', mediagen_url
)
93 if 'acceptMethods' not in mediagen_url
:
94 mediagen_url
+= '&acceptMethods=fms'
96 mediagen_doc
= self
._download
_xml
(mediagen_url
, video_id
,
97 'Downloading video urls')
99 description_node
= itemdoc
.find('description')
100 if description_node
is not None:
101 description
= description_node
.text
.strip()
107 title_el
= find_xpath_attr(
108 itemdoc
, './/{http://search.yahoo.com/mrss/}category',
109 'scheme', 'urn:mtvn:video_title')
111 title_el
= itemdoc
.find('.//{http://search.yahoo.com/mrss/}title')
113 title_el
= itemdoc
.find('.//title')
114 if title_el
.text
is None:
117 title
= title_el
.text
119 raise ExtractorError('Could not find video title')
120 title
= title
.strip()
122 # This a short id that's used in the webpage urls
124 mtvn_id_node
= find_xpath_attr(itemdoc
, './/{http://search.yahoo.com/mrss/}category',
125 'scheme', 'urn:mtvn:id')
126 if mtvn_id_node
is not None:
127 mtvn_id
= mtvn_id_node
.text
131 'formats': self
._extract
_video
_formats
(mediagen_doc
, mtvn_id
),
133 'thumbnail': self
._get
_thumbnail
_url
(uri
, itemdoc
),
134 'description': description
,
137 def _get_videos_info(self
, uri
):
138 video_id
= self
._id
_from
_uri
(uri
)
139 data
= compat_urllib_parse
.urlencode({'uri': uri
})
141 idoc
= self
._download
_xml
(
142 self
._FEED
_URL
+ '?' + data
, video_id
,
143 'Downloading info', transform_source
=fix_xml_ampersands
)
144 return [self
._get
_video
_info
(item
) for item
in idoc
.findall('.//item')]
146 def _real_extract(self
, url
):
147 title
= url_basename(url
)
148 webpage
= self
._download
_webpage
(url
, title
)
150 # the url can be http://media.mtvnservices.com/fb/{mgid}.swf
151 # or http://media.mtvnservices.com/{mgid}
152 og_url
= self
._og
_search
_video
_url
(webpage
)
153 mgid
= url_basename(og_url
)
154 if mgid
.endswith('.swf'):
156 except RegexNotFoundError
:
157 mgid
= self
._search
_regex
(
158 [r
'data-mgid="(.*?)"', r
'swfobject.embedSWF\(".*?(mgid:.*?)"'],
160 return self
._get
_videos
_info
(mgid
)
163 class MTVIE(MTVServicesInfoExtractor
):
164 _VALID_URL
= r
'''(?x)^https?://
165 (?:(?:www\.)?mtv\.com/videos/.+?/(?P<videoid>[0-9]+)/[^/]+$|
166 m\.mtv\.com/videos/video\.rbml\?.*?id=(?P<mgid>[^&]+))'''
168 _FEED_URL
= 'http://www.mtv.com/player/embed/AS3/rss/'
172 'url': 'http://www.mtv.com/videos/misc/853555/ours-vh1-storytellers.jhtml',
173 'file': '853555.mp4',
174 'md5': '850f3f143316b1e71fa56a4edfd6e0f8',
176 'title': 'Taylor Swift - "Ours (VH1 Storytellers)"',
177 'description': 'Album: Taylor Swift performs "Ours" for VH1 Storytellers at Harvey Mudd College.',
182 'url': 'http://www.mtv.com/videos/taylor-swift/916187/everything-has-changed-ft-ed-sheeran.jhtml',
183 'file': 'USCJY1331283.mp4',
184 'md5': '73b4e7fcadd88929292fe52c3ced8caf',
186 'title': 'Everything Has Changed',
187 'upload_date': '20130606',
188 'uploader': 'Taylor Swift',
190 'skip': 'VEVO is only available in some countries',
194 def _get_thumbnail_url(self
, uri
, itemdoc
):
195 return 'http://mtv.mtvnimages.com/uri/' + uri
197 def _real_extract(self
, url
):
198 mobj
= re
.match(self
._VALID
_URL
, url
)
199 video_id
= mobj
.group('videoid')
200 uri
= mobj
.groupdict().get('mgid')
202 webpage
= self
._download
_webpage
(url
, video_id
)
204 # Some videos come from Vevo.com
205 m_vevo
= re
.search(r
'isVevoVideo = true;.*?vevoVideoId = "(.*?)";',
208 vevo_id
= m_vevo
.group(1);
209 self
.to_screen('Vevo video detected: %s' % vevo_id
)
210 return self
.url_result('vevo:%s' % vevo_id
, ie
='Vevo')
212 uri
= self
._html
_search
_regex
(r
'/uri/(.*?)\?', webpage
, 'uri')
213 return self
._get
_videos
_info
(uri
)
216 class MTVIggyIE(MTVServicesInfoExtractor
):
217 IE_NAME
= 'mtviggy.com'
218 _VALID_URL
= r
'https?://www\.mtviggy\.com/videos/.+'
220 'url': 'http://www.mtviggy.com/videos/arcade-fire-behind-the-scenes-at-the-biggest-music-experiment-yet/',
224 'title': 'Arcade Fire: Behind the Scenes at the Biggest Music Experiment Yet',
227 _FEED_URL
= 'http://all.mtvworldverticals.com/feed-xml/'