]>
Commit | Line | Data |
---|---|---|
32dac694 PH |
1 | from __future__ import unicode_literals |
2 | ||
fc287219 | 3 | import re |
fc287219 | 4 | |
0af25f78 | 5 | from .common import InfoExtractor |
1cc79574 | 6 | from ..compat import ( |
15707c7e | 7 | compat_urllib_parse_urlencode, |
e525d9a3 | 8 | compat_str, |
20a6a154 | 9 | compat_xpath, |
1cc79574 PH |
10 | ) |
11 | from ..utils import ( | |
fc287219 | 12 | ExtractorError, |
90834c78 | 13 | find_xpath_attr, |
5aafe895 | 14 | fix_xml_ampersands, |
8765151c | 15 | float_or_none, |
0ef68e04 | 16 | HEADRequest, |
5c2266df | 17 | sanitized_Request, |
340b0468 | 18 | unescapeHTML, |
8d9453b9 JMF |
19 | url_basename, |
20 | RegexNotFoundError, | |
5767b4ee | 21 | xpath_text, |
fc287219 PH |
22 | ) |
23 | ||
90834c78 | 24 | |
300fcad8 JMF |
25 | def _media_xml_tag(tag): |
26 | return '{http://search.yahoo.com/mrss/}%s' % tag | |
fc287219 | 27 | |
f7e02595 | 28 | |
0af25f78 | 29 | class MTVServicesInfoExtractor(InfoExtractor): |
340b0468 | 30 | _MOBILE_TEMPLATE = None |
a542e372 | 31 | _LANG = None |
8940c1c0 | 32 | |
f7e02595 JMF |
33 | @staticmethod |
34 | def _id_from_uri(uri): | |
35 | return uri.split(':')[-1] | |
36 | ||
37 | # This was originally implemented for ComedyCentral, but it also works here | |
38 | @staticmethod | |
39 | def _transform_rtmp_url(rtmp_video_url): | |
40 | m = re.match(r'^rtmpe?://.*?/(?P<finalid>gsp\..+?/.*)$', rtmp_video_url) | |
41 | if not m: | |
63b7b722 | 42 | return rtmp_video_url |
2774852c | 43 | base = 'http://viacommtvstrmfs.fplive.net/' |
ab2f744b JMF |
44 | return base + m.group('finalid') |
45 | ||
8940c1c0 JMF |
46 | def _get_feed_url(self, uri): |
47 | return self._FEED_URL | |
48 | ||
ab2f744b | 49 | def _get_thumbnail_url(self, uri, itemdoc): |
84db8181 JMF |
50 | search_path = '%s/%s' % (_media_xml_tag('group'), _media_xml_tag('thumbnail')) |
51 | thumb_node = itemdoc.find(search_path) | |
52 | if thumb_node is None: | |
53 | return None | |
54 | else: | |
55 | return thumb_node.attrib['url'] | |
f7e02595 | 56 | |
340b0468 JMF |
57 | def _extract_mobile_video_formats(self, mtvn_id): |
58 | webpage_url = self._MOBILE_TEMPLATE % mtvn_id | |
5c2266df | 59 | req = sanitized_Request(webpage_url) |
340b0468 | 60 | # Otherwise we get a webpage that would execute some javascript |
3fcfb8e9 | 61 | req.add_header('User-Agent', 'curl/7') |
340b0468 | 62 | webpage = self._download_webpage(req, mtvn_id, |
9e1a5b84 | 63 | 'Downloading mobile page') |
0ef68e04 JMF |
64 | metrics_url = unescapeHTML(self._search_regex(r'<a href="(http://metrics.+?)"', webpage, 'url')) |
65 | req = HEADRequest(metrics_url) | |
66 | response = self._request_webpage(req, mtvn_id, 'Resolving url') | |
67 | url = response.geturl() | |
68 | # Transform the url to get the best quality: | |
69 | url = re.sub(r'.+pxE=mp4', 'http://mtvnmobile.vo.llnwd.net/kip0/_pxn=0+_pxK=18639+_pxE=mp4', url, 1) | |
5f6a1245 | 70 | return [{'url': url, 'ext': 'mp4'}] |
340b0468 JMF |
71 | |
72 | def _extract_video_formats(self, mdoc, mtvn_id): | |
054d43bb | 73 | if re.match(r'.*/(error_country_block\.swf|geoblock\.mp4|copyright_error\.flv(?:\?geo\b.+?)?)$', mdoc.find('.//src').text) is not None: |
340b0468 | 74 | if mtvn_id is not None and self._MOBILE_TEMPLATE is not None: |
0ef68e04 | 75 | self.to_screen('The normal version is not available from your ' |
9e1a5b84 | 76 | 'country, trying with the mobile version') |
340b0468 | 77 | return self._extract_mobile_video_formats(mtvn_id) |
cc1db7f9 | 78 | raise ExtractorError('This video is not available from your country.', |
9e1a5b84 | 79 | expected=True) |
f7e02595 | 80 | |
f13d0933 PH |
81 | formats = [] |
82 | for rendition in mdoc.findall('.//rendition'): | |
83 | try: | |
84 | _, _, ext = rendition.attrib['type'].partition('/') | |
85 | rtmp_video_url = rendition.find('./src').text | |
ca0f500e PH |
86 | if rtmp_video_url.endswith('siteunavail.png'): |
87 | continue | |
a72df5f3 | 88 | new_url = self._transform_rtmp_url(rtmp_video_url) |
ca0f500e | 89 | formats.append({ |
a72df5f3 YCH |
90 | 'ext': 'flv' if new_url.startswith('rtmp') else ext, |
91 | 'url': new_url, | |
ca0f500e PH |
92 | 'format_id': rendition.get('bitrate'), |
93 | 'width': int(rendition.get('width')), | |
94 | 'height': int(rendition.get('height')), | |
95 | }) | |
f13d0933 PH |
96 | except (KeyError, TypeError): |
97 | raise ExtractorError('Invalid rendition field.') | |
34d863f3 | 98 | self._sort_formats(formats) |
f13d0933 | 99 | return formats |
f7e02595 | 100 | |
e525d9a3 S |
101 | def _extract_subtitles(self, mdoc, mtvn_id): |
102 | subtitles = {} | |
e525d9a3 S |
103 | for transcript in mdoc.findall('.//transcript'): |
104 | if transcript.get('kind') != 'captions': | |
105 | continue | |
106 | lang = transcript.get('srclang') | |
0af25f78 JMF |
107 | subtitles[lang] = [{ |
108 | 'url': compat_str(typographic.get('src')), | |
109 | 'ext': typographic.get('format') | |
110 | } for typographic in transcript.findall('./typographic')] | |
111 | return subtitles | |
e525d9a3 | 112 | |
f7e02595 JMF |
113 | def _get_video_info(self, itemdoc): |
114 | uri = itemdoc.find('guid').text | |
115 | video_id = self._id_from_uri(uri) | |
116 | self.report_extraction(video_id) | |
04cbc498 | 117 | content_el = itemdoc.find('%s/%s' % (_media_xml_tag('group'), _media_xml_tag('content'))) |
118 | mediagen_url = content_el.attrib['url'] | |
321a01f9 | 119 | # Remove the templates, like &device={device} |
32dac694 | 120 | mediagen_url = re.sub(r'&[^=]*?={.*?}(?=(&|$))', '', mediagen_url) |
f7e02595 | 121 | if 'acceptMethods' not in mediagen_url: |
56f447be S |
122 | mediagen_url += '&' if '?' in mediagen_url else '?' |
123 | mediagen_url += 'acceptMethods=fms' | |
6562df76 | 124 | |
e4f320a4 | 125 | mediagen_doc = self._download_xml(mediagen_url, video_id, |
9e1a5b84 | 126 | 'Downloading video urls') |
f7e02595 | 127 | |
0dfe9bc9 S |
128 | item = mediagen_doc.find('./video/item') |
129 | if item is not None and item.get('type') == 'text': | |
130 | message = '%s returned error: ' % self.IE_NAME | |
131 | if item.get('code') is not None: | |
132 | message += '%s - ' % item.get('code') | |
133 | message += item.text | |
134 | raise ExtractorError(message, expected=True) | |
135 | ||
5767b4ee | 136 | description = xpath_text(itemdoc, 'description') |
f13d0933 | 137 | |
90834c78 PH |
138 | title_el = None |
139 | if title_el is None: | |
140 | title_el = find_xpath_attr( | |
141 | itemdoc, './/{http://search.yahoo.com/mrss/}category', | |
142 | 'scheme', 'urn:mtvn:video_title') | |
90834c78 | 143 | if title_el is None: |
20a6a154 | 144 | title_el = itemdoc.find(compat_xpath('.//{http://search.yahoo.com/mrss/}title')) |
96cb10a5 | 145 | if title_el is None: |
20a6a154 | 146 | title_el = itemdoc.find(compat_xpath('.//title')) |
1df4229b PH |
147 | if title_el.text is None: |
148 | title_el = None | |
1df4229b | 149 | |
90834c78 PH |
150 | title = title_el.text |
151 | if title is None: | |
152 | raise ExtractorError('Could not find video title') | |
780ee4e5 | 153 | title = title.strip() |
90834c78 | 154 | |
340b0468 JMF |
155 | # This a short id that's used in the webpage urls |
156 | mtvn_id = None | |
157 | mtvn_id_node = find_xpath_attr(itemdoc, './/{http://search.yahoo.com/mrss/}category', | |
9e1a5b84 | 158 | 'scheme', 'urn:mtvn:id') |
340b0468 JMF |
159 | if mtvn_id_node is not None: |
160 | mtvn_id = mtvn_id_node.text | |
161 | ||
fb7abb31 | 162 | return { |
90834c78 | 163 | 'title': title, |
340b0468 | 164 | 'formats': self._extract_video_formats(mediagen_doc, mtvn_id), |
e525d9a3 | 165 | 'subtitles': self._extract_subtitles(mediagen_doc, mtvn_id), |
f13d0933 PH |
166 | 'id': video_id, |
167 | 'thumbnail': self._get_thumbnail_url(uri, itemdoc), | |
168 | 'description': description, | |
04cbc498 | 169 | 'duration': float_or_none(content_el.attrib.get('duration')), |
f13d0933 PH |
170 | } |
171 | ||
c1e90619 | 172 | def _get_feed_query(self, uri): |
173 | data = {'uri': uri} | |
174 | if self._LANG: | |
175 | data['lang'] = self._LANG | |
15707c7e | 176 | return compat_urllib_parse_urlencode(data) |
c1e90619 | 177 | |
f7e02595 JMF |
178 | def _get_videos_info(self, uri): |
179 | video_id = self._id_from_uri(uri) | |
8940c1c0 | 180 | feed_url = self._get_feed_url(uri) |
c1e90619 | 181 | info_url = feed_url + '?' + self._get_feed_query(uri) |
79fa9db0 S |
182 | return self._get_videos_info_from_url(info_url, video_id) |
183 | ||
184 | def _get_videos_info_from_url(self, url, video_id): | |
e2b38da9 | 185 | idoc = self._download_xml( |
79fa9db0 | 186 | url, video_id, |
32dac694 | 187 | 'Downloading info', transform_source=fix_xml_ampersands) |
5239075b PH |
188 | return self.playlist_result( |
189 | [self._get_video_info(item) for item in idoc.findall('.//item')]) | |
fc287219 | 190 | |
c1e90619 | 191 | def _extract_mgid(self, webpage): |
8d9453b9 | 192 | try: |
4bbf139a JMF |
193 | # the url can be http://media.mtvnservices.com/fb/{mgid}.swf |
194 | # or http://media.mtvnservices.com/{mgid} | |
195 | og_url = self._og_search_video_url(webpage) | |
196 | mgid = url_basename(og_url) | |
197 | if mgid.endswith('.swf'): | |
198 | mgid = mgid[:-4] | |
8d9453b9 | 199 | except RegexNotFoundError: |
b1298d8e AMW |
200 | mgid = None |
201 | ||
202 | if mgid is None or ':' not in mgid: | |
b9381e43 JMF |
203 | mgid = self._search_regex( |
204 | [r'data-mgid="(.*?)"', r'swfobject.embedSWF\(".*?(mgid:.*?)"'], | |
fc42bc6e S |
205 | webpage, 'mgid', default=None) |
206 | ||
207 | if not mgid: | |
208 | sm4_embed = self._html_search_meta( | |
209 | 'sm4:video:embed', webpage, 'sm4 embed', default='') | |
210 | mgid = self._search_regex( | |
211 | r'embed/(mgid:.+?)["\'&?/]', sm4_embed, 'mgid') | |
c1e90619 | 212 | return mgid |
e525d9a3 | 213 | |
c1e90619 | 214 | def _real_extract(self, url): |
215 | title = url_basename(url) | |
216 | webpage = self._download_webpage(url, title) | |
217 | mgid = self._extract_mgid(webpage) | |
e525d9a3 | 218 | videos_info = self._get_videos_info(mgid) |
e525d9a3 | 219 | return videos_info |
8d9453b9 | 220 | |
84db8181 | 221 | |
8940c1c0 JMF |
222 | class MTVServicesEmbeddedIE(MTVServicesInfoExtractor): |
223 | IE_NAME = 'mtvservices:embedded' | |
224 | _VALID_URL = r'https?://media\.mtvnservices\.com/embed/(?P<mgid>.+?)(\?|/|$)' | |
225 | ||
226 | _TEST = { | |
227 | # From http://www.thewrap.com/peter-dinklage-sums-up-game-of-thrones-in-45-seconds-video/ | |
228 | 'url': 'http://media.mtvnservices.com/embed/mgid:uma:video:mtv.com:1043906/cp~vid%3D1043906%26uri%3Dmgid%3Auma%3Avideo%3Amtv.com%3A1043906', | |
229 | 'md5': 'cb349b21a7897164cede95bd7bf3fbb9', | |
230 | 'info_dict': { | |
231 | 'id': '1043906', | |
232 | 'ext': 'mp4', | |
233 | 'title': 'Peter Dinklage Sums Up \'Game Of Thrones\' In 45 Seconds', | |
234 | 'description': '"Sexy sexy sexy, stabby stabby stabby, beautiful language," says Peter Dinklage as he tries summarizing "Game of Thrones" in under a minute.', | |
235 | }, | |
236 | } | |
237 | ||
fe1d858e S |
238 | @staticmethod |
239 | def _extract_url(webpage): | |
240 | mobj = re.search( | |
241 | r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//media.mtvnservices.com/embed/.+?)\1', webpage) | |
242 | if mobj: | |
243 | return mobj.group('url') | |
244 | ||
8940c1c0 JMF |
245 | def _get_feed_url(self, uri): |
246 | video_id = self._id_from_uri(uri) | |
247 | site_id = uri.replace(video_id, '') | |
71f8c7ce | 248 | config_url = ('http://media.mtvnservices.com/pmt/e1/players/{0}/' |
9e1a5b84 | 249 | 'context4/context5/config.xml'.format(site_id)) |
8940c1c0 JMF |
250 | config_doc = self._download_xml(config_url, video_id) |
251 | feed_node = config_doc.find('.//feed') | |
252 | feed_url = feed_node.text.strip().split('?')[0] | |
253 | return feed_url | |
254 | ||
255 | def _real_extract(self, url): | |
256 | mobj = re.match(self._VALID_URL, url) | |
257 | mgid = mobj.group('mgid') | |
258 | return self._get_videos_info(mgid) | |
259 | ||
260 | ||
84db8181 | 261 | class MTVIE(MTVServicesInfoExtractor): |
5c541b2c JMF |
262 | _VALID_URL = r'''(?x)^https?:// |
263 | (?:(?:www\.)?mtv\.com/videos/.+?/(?P<videoid>[0-9]+)/[^/]+$| | |
264 | m\.mtv\.com/videos/video\.rbml\?.*?id=(?P<mgid>[^&]+))''' | |
84db8181 JMF |
265 | |
266 | _FEED_URL = 'http://www.mtv.com/player/embed/AS3/rss/' | |
267 | ||
268 | _TESTS = [ | |
269 | { | |
32dac694 | 270 | 'url': 'http://www.mtv.com/videos/misc/853555/ours-vh1-storytellers.jhtml', |
32dac694 PH |
271 | 'md5': '850f3f143316b1e71fa56a4edfd6e0f8', |
272 | 'info_dict': { | |
ca0f500e PH |
273 | 'id': '853555', |
274 | 'ext': 'mp4', | |
32dac694 PH |
275 | 'title': 'Taylor Swift - "Ours (VH1 Storytellers)"', |
276 | 'description': 'Album: Taylor Swift performs "Ours" for VH1 Storytellers at Harvey Mudd College.', | |
84db8181 JMF |
277 | }, |
278 | }, | |
84db8181 JMF |
279 | ] |
280 | ||
281 | def _get_thumbnail_url(self, uri, itemdoc): | |
282 | return 'http://mtv.mtvnimages.com/uri/' + uri | |
283 | ||
fc287219 PH |
284 | def _real_extract(self, url): |
285 | mobj = re.match(self._VALID_URL, url) | |
fc287219 | 286 | video_id = mobj.group('videoid') |
c801b205 | 287 | uri = mobj.groupdict().get('mgid') |
5c541b2c JMF |
288 | if uri is None: |
289 | webpage = self._download_webpage(url, video_id) | |
5f6a1245 | 290 | |
5c541b2c | 291 | # Some videos come from Vevo.com |
ca0f500e PH |
292 | m_vevo = re.search( |
293 | r'(?s)isVevoVideo = true;.*?vevoVideoId = "(.*?)";', webpage) | |
5c541b2c | 294 | if m_vevo: |
8bcc8756 | 295 | vevo_id = m_vevo.group(1) |
32dac694 | 296 | self.to_screen('Vevo video detected: %s' % vevo_id) |
5c541b2c | 297 | return self.url_result('vevo:%s' % vevo_id, ie='Vevo') |
5f6a1245 | 298 | |
32dac694 | 299 | uri = self._html_search_regex(r'/uri/(.*?)\?', webpage, 'uri') |
f7e02595 | 300 | return self._get_videos_info(uri) |
bc4ba05f JMF |
301 | |
302 | ||
303 | class MTVIggyIE(MTVServicesInfoExtractor): | |
304 | IE_NAME = 'mtviggy.com' | |
305 | _VALID_URL = r'https?://www\.mtviggy\.com/videos/.+' | |
306 | _TEST = { | |
307 | 'url': 'http://www.mtviggy.com/videos/arcade-fire-behind-the-scenes-at-the-biggest-music-experiment-yet/', | |
308 | 'info_dict': { | |
309 | 'id': '984696', | |
310 | 'ext': 'mp4', | |
af1588c0 | 311 | 'title': 'Arcade Fire: Behind the Scenes at the Biggest Music Experiment Yet', |
bc4ba05f JMF |
312 | } |
313 | } | |
314 | _FEED_URL = 'http://all.mtvworldverticals.com/feed-xml/' | |
071c1013 | 315 | |
79fa9db0 | 316 | |
071c1013 PH |
317 | class MTVDEIE(MTVServicesInfoExtractor): |
318 | IE_NAME = 'mtv.de' | |
65488b82 | 319 | _VALID_URL = r'https?://(?:www\.)?mtv\.de/(?:artists|shows|news)/(?:[^/]+/)*(?P<id>\d+)-[^/#?]+/*(?:[#?].*)?$' |
79fa9db0 S |
320 | _TESTS = [{ |
321 | 'url': 'http://www.mtv.de/artists/10571-cro/videos/61131-traum', | |
322 | 'info_dict': { | |
323 | 'id': 'music_video-a50bc5f0b3aa4b3190aa', | |
324 | 'ext': 'mp4', | |
325 | 'title': 'MusicVideo_cro-traum', | |
326 | 'description': 'Cro - Traum', | |
071c1013 | 327 | }, |
79fa9db0 S |
328 | 'params': { |
329 | # rtmp download | |
330 | 'skip_download': True, | |
331 | }, | |
c3c9f879 S |
332 | }, { |
333 | # mediagen URL without query (e.g. http://videos.mtvnn.com/mediagen/e865da714c166d18d6f80893195fcb97) | |
334 | 'url': 'http://www.mtv.de/shows/933-teen-mom-2/staffeln/5353/folgen/63565-enthullungen', | |
335 | 'info_dict': { | |
336 | 'id': 'local_playlist-f5ae778b9832cc837189', | |
337 | 'ext': 'mp4', | |
338 | 'title': 'Episode_teen-mom-2_shows_season-5_episode-1_full-episode_part1', | |
339 | }, | |
340 | 'params': { | |
341 | # rtmp download | |
342 | 'skip_download': True, | |
343 | }, | |
65488b82 S |
344 | }, { |
345 | # single video in pagePlaylist with different id | |
346 | 'url': 'http://www.mtv.de/news/77491-mtv-movies-spotlight-pixels-teil-3', | |
347 | 'info_dict': { | |
348 | 'id': 'local_playlist-4e760566473c4c8c5344', | |
349 | 'ext': 'mp4', | |
350 | 'title': 'Article_mtv-movies-spotlight-pixels-teil-3_short-clips_part1', | |
351 | 'description': 'MTV Movies Supercut', | |
352 | }, | |
353 | 'params': { | |
354 | # rtmp download | |
355 | 'skip_download': True, | |
356 | }, | |
79fa9db0 | 357 | }] |
071c1013 PH |
358 | |
359 | def _real_extract(self, url): | |
79fa9db0 S |
360 | video_id = self._match_id(url) |
361 | ||
362 | webpage = self._download_webpage(url, video_id) | |
363 | ||
364 | playlist = self._parse_json( | |
365 | self._search_regex( | |
366 | r'window\.pagePlaylist\s*=\s*(\[.+?\]);\n', webpage, 'page playlist'), | |
367 | video_id) | |
071c1013 | 368 | |
65488b82 S |
369 | # news pages contain single video in playlist with different id |
370 | if len(playlist) == 1: | |
371 | return self._get_videos_info_from_url(playlist[0]['mrss'], video_id) | |
372 | ||
071c1013 | 373 | for item in playlist: |
79fa9db0 S |
374 | item_id = item.get('id') |
375 | if item_id and compat_str(item_id) == video_id: | |
376 | return self._get_videos_info_from_url(item['mrss'], video_id) |