[mtvde] Simplify (Closes #6673)
[youtube-dl] / youtube_dl / extractor / mtv.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..compat import (
7     compat_urllib_parse,
8     compat_urllib_request,
9     compat_str,
10 )
11 from ..utils import (
12     ExtractorError,
13     find_xpath_attr,
14     fix_xml_ampersands,
15     HEADRequest,
16     unescapeHTML,
17     url_basename,
18     RegexNotFoundError,
19 )
20
21
22 def _media_xml_tag(tag):
23     return '{http://search.yahoo.com/mrss/}%s' % tag
24
25
26 class MTVServicesInfoExtractor(InfoExtractor):
27     _MOBILE_TEMPLATE = None
28     _LANG = None
29
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:
39             return rtmp_video_url
40         base = 'http://viacommtvstrmfs.fplive.net/'
41         return base + m.group('finalid')
42
43     def _get_feed_url(self, uri):
44         return self._FEED_URL
45
46     def _get_thumbnail_url(self, uri, itemdoc):
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']
53
54     def _extract_mobile_video_formats(self, mtvn_id):
55         webpage_url = self._MOBILE_TEMPLATE % mtvn_id
56         req = compat_urllib_request.Request(webpage_url)
57         # Otherwise we get a webpage that would execute some javascript
58         req.add_header('User-Agent', 'curl/7')
59         webpage = self._download_webpage(req, mtvn_id,
60                                          'Downloading mobile page')
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)
67         return [{'url': url, 'ext': 'mp4'}]
68
69     def _extract_video_formats(self, mdoc, mtvn_id):
70         if re.match(r'.*/(error_country_block\.swf|geoblock\.mp4)$', mdoc.find('.//src').text) is not None:
71             if mtvn_id is not None and self._MOBILE_TEMPLATE is not None:
72                 self.to_screen('The normal version is not available from your '
73                                'country, trying with the mobile version')
74                 return self._extract_mobile_video_formats(mtvn_id)
75             raise ExtractorError('This video is not available from your country.',
76                                  expected=True)
77
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
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                 })
92             except (KeyError, TypeError):
93                 raise ExtractorError('Invalid rendition field.')
94         self._sort_formats(formats)
95         return formats
96
97     def _extract_subtitles(self, mdoc, mtvn_id):
98         subtitles = {}
99         for transcript in mdoc.findall('.//transcript'):
100             if transcript.get('kind') != 'captions':
101                 continue
102             lang = transcript.get('srclang')
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
108
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)
113         mediagen_url = itemdoc.find('%s/%s' % (_media_xml_tag('group'), _media_xml_tag('content'))).attrib['url']
114         # Remove the templates, like &device={device}
115         mediagen_url = re.sub(r'&[^=]*?={.*?}(?=(&|$))', '', mediagen_url)
116         if 'acceptMethods' not in mediagen_url:
117             mediagen_url += '&acceptMethods=fms'
118
119         mediagen_doc = self._download_xml(mediagen_url, video_id,
120                                           'Downloading video urls')
121
122         item = mediagen_doc.find('./video/item')
123         if item is not None and item.get('type') == 'text':
124             message = '%s returned error: ' % self.IE_NAME
125             if item.get('code') is not None:
126                 message += '%s - ' % item.get('code')
127             message += item.text
128             raise ExtractorError(message, expected=True)
129
130         description_node = itemdoc.find('description')
131         if description_node is not None:
132             description = description_node.text.strip()
133         else:
134             description = None
135
136         title_el = None
137         if title_el is None:
138             title_el = find_xpath_attr(
139                 itemdoc, './/{http://search.yahoo.com/mrss/}category',
140                 'scheme', 'urn:mtvn:video_title')
141         if title_el is None:
142             title_el = itemdoc.find('.//{http://search.yahoo.com/mrss/}title')
143         if title_el is None:
144             title_el = itemdoc.find('.//title')
145             if title_el.text is None:
146                 title_el = None
147
148         title = title_el.text
149         if title is None:
150             raise ExtractorError('Could not find video title')
151         title = title.strip()
152
153         # This a short id that's used in the webpage urls
154         mtvn_id = None
155         mtvn_id_node = find_xpath_attr(itemdoc, './/{http://search.yahoo.com/mrss/}category',
156                                        'scheme', 'urn:mtvn:id')
157         if mtvn_id_node is not None:
158             mtvn_id = mtvn_id_node.text
159
160         return {
161             'title': title,
162             'formats': self._extract_video_formats(mediagen_doc, mtvn_id),
163             'subtitles': self._extract_subtitles(mediagen_doc, mtvn_id),
164             'id': video_id,
165             'thumbnail': self._get_thumbnail_url(uri, itemdoc),
166             'description': description,
167         }
168
169     def _get_videos_info(self, uri):
170         video_id = self._id_from_uri(uri)
171         feed_url = self._get_feed_url(uri)
172         data = compat_urllib_parse.urlencode({'uri': uri})
173         info_url = feed_url + '?'
174         if self._LANG:
175             info_url += 'lang=%s&' % self._LANG
176         info_url += data
177         return self._get_videos_info_from_url(info_url, video_id)
178
179     def _get_videos_info_from_url(self, url, video_id):
180         idoc = self._download_xml(
181             url, video_id,
182             'Downloading info', transform_source=fix_xml_ampersands)
183         return self.playlist_result(
184             [self._get_video_info(item) for item in idoc.findall('.//item')])
185
186     def _real_extract(self, url):
187         title = url_basename(url)
188         webpage = self._download_webpage(url, title)
189         try:
190             # the url can be http://media.mtvnservices.com/fb/{mgid}.swf
191             # or http://media.mtvnservices.com/{mgid}
192             og_url = self._og_search_video_url(webpage)
193             mgid = url_basename(og_url)
194             if mgid.endswith('.swf'):
195                 mgid = mgid[:-4]
196         except RegexNotFoundError:
197             mgid = None
198
199         if mgid is None or ':' not in mgid:
200             mgid = self._search_regex(
201                 [r'data-mgid="(.*?)"', r'swfobject.embedSWF\(".*?(mgid:.*?)"'],
202                 webpage, 'mgid')
203
204         videos_info = self._get_videos_info(mgid)
205         return videos_info
206
207
208 class MTVServicesEmbeddedIE(MTVServicesInfoExtractor):
209     IE_NAME = 'mtvservices:embedded'
210     _VALID_URL = r'https?://media\.mtvnservices\.com/embed/(?P<mgid>.+?)(\?|/|$)'
211
212     _TEST = {
213         # From http://www.thewrap.com/peter-dinklage-sums-up-game-of-thrones-in-45-seconds-video/
214         'url': 'http://media.mtvnservices.com/embed/mgid:uma:video:mtv.com:1043906/cp~vid%3D1043906%26uri%3Dmgid%3Auma%3Avideo%3Amtv.com%3A1043906',
215         'md5': 'cb349b21a7897164cede95bd7bf3fbb9',
216         'info_dict': {
217             'id': '1043906',
218             'ext': 'mp4',
219             'title': 'Peter Dinklage Sums Up \'Game Of Thrones\' In 45 Seconds',
220             'description': '"Sexy sexy sexy, stabby stabby stabby, beautiful language," says Peter Dinklage as he tries summarizing "Game of Thrones" in under a minute.',
221         },
222     }
223
224     def _get_feed_url(self, uri):
225         video_id = self._id_from_uri(uri)
226         site_id = uri.replace(video_id, '')
227         config_url = ('http://media.mtvnservices.com/pmt/e1/players/{0}/'
228                       'context4/context5/config.xml'.format(site_id))
229         config_doc = self._download_xml(config_url, video_id)
230         feed_node = config_doc.find('.//feed')
231         feed_url = feed_node.text.strip().split('?')[0]
232         return feed_url
233
234     def _real_extract(self, url):
235         mobj = re.match(self._VALID_URL, url)
236         mgid = mobj.group('mgid')
237         return self._get_videos_info(mgid)
238
239
240 class MTVIE(MTVServicesInfoExtractor):
241     _VALID_URL = r'''(?x)^https?://
242         (?:(?:www\.)?mtv\.com/videos/.+?/(?P<videoid>[0-9]+)/[^/]+$|
243            m\.mtv\.com/videos/video\.rbml\?.*?id=(?P<mgid>[^&]+))'''
244
245     _FEED_URL = 'http://www.mtv.com/player/embed/AS3/rss/'
246
247     _TESTS = [
248         {
249             'url': 'http://www.mtv.com/videos/misc/853555/ours-vh1-storytellers.jhtml',
250             'md5': '850f3f143316b1e71fa56a4edfd6e0f8',
251             'info_dict': {
252                 'id': '853555',
253                 'ext': 'mp4',
254                 'title': 'Taylor Swift - "Ours (VH1 Storytellers)"',
255                 'description': 'Album: Taylor Swift performs "Ours" for VH1 Storytellers at Harvey Mudd College.',
256             },
257         },
258     ]
259
260     def _get_thumbnail_url(self, uri, itemdoc):
261         return 'http://mtv.mtvnimages.com/uri/' + uri
262
263     def _real_extract(self, url):
264         mobj = re.match(self._VALID_URL, url)
265         video_id = mobj.group('videoid')
266         uri = mobj.groupdict().get('mgid')
267         if uri is None:
268             webpage = self._download_webpage(url, video_id)
269
270             # Some videos come from Vevo.com
271             m_vevo = re.search(
272                 r'(?s)isVevoVideo = true;.*?vevoVideoId = "(.*?)";', webpage)
273             if m_vevo:
274                 vevo_id = m_vevo.group(1)
275                 self.to_screen('Vevo video detected: %s' % vevo_id)
276                 return self.url_result('vevo:%s' % vevo_id, ie='Vevo')
277
278             uri = self._html_search_regex(r'/uri/(.*?)\?', webpage, 'uri')
279         return self._get_videos_info(uri)
280
281
282 class MTVIggyIE(MTVServicesInfoExtractor):
283     IE_NAME = 'mtviggy.com'
284     _VALID_URL = r'https?://www\.mtviggy\.com/videos/.+'
285     _TEST = {
286         'url': 'http://www.mtviggy.com/videos/arcade-fire-behind-the-scenes-at-the-biggest-music-experiment-yet/',
287         'info_dict': {
288             'id': '984696',
289             'ext': 'mp4',
290             'title': 'Arcade Fire: Behind the Scenes at the Biggest Music Experiment Yet',
291         }
292     }
293     _FEED_URL = 'http://all.mtvworldverticals.com/feed-xml/'
294
295
296 class MTVDEIE(MTVServicesInfoExtractor):
297     IE_NAME = 'mtv.de'
298     _VALID_URL = r'https?://(?:www\.)?mtv\.de/(?:artists|shows)/(?:[^/]+/)+(?P<id>\d+)-[^/#?]+/*(?:[#?].*)?$'
299     _TESTS = [{
300         'url': 'http://www.mtv.de/artists/10571-cro/videos/61131-traum',
301         'info_dict': {
302             'id': 'music_video-a50bc5f0b3aa4b3190aa',
303             'ext': 'mp4',
304             'title': 'MusicVideo_cro-traum',
305             'description': 'Cro - Traum',
306         },
307         'params': {
308             # rtmp download
309             'skip_download': True,
310         },
311     }]
312
313     def _real_extract(self, url):
314         video_id = self._match_id(url)
315
316         webpage = self._download_webpage(url, video_id)
317
318         playlist = self._parse_json(
319             self._search_regex(
320                 r'window\.pagePlaylist\s*=\s*(\[.+?\]);\n', webpage, 'page playlist'),
321             video_id)
322
323         for item in playlist:
324             item_id = item.get('id')
325             if item_id and compat_str(item_id) == video_id:
326                 return self._get_videos_info_from_url(item['mrss'], video_id)