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