]> gitweb @ CieloNegro.org - youtube-dl.git/blob - youtube_dl/extractor/mtv.py
[toutv] really skip test
[youtube-dl.git] / youtube_dl / extractor / mtv.py
1 import re
2 import xml.etree.ElementTree
3
4 from .common import InfoExtractor
5 from ..utils import (
6     compat_urllib_parse,
7     ExtractorError,
8 )
9
10 def _media_xml_tag(tag):
11     return '{http://search.yahoo.com/mrss/}%s' % tag
12
13 class MTVIE(InfoExtractor):
14     _VALID_URL = r'^https?://(?:www\.)?mtv\.com/videos/.+?/(?P<videoid>[0-9]+)/[^/]+$'
15
16     _FEED_URL = 'http://www.mtv.com/player/embed/AS3/rss/'
17
18     _TESTS = [
19         {
20             u'url': u'http://www.mtv.com/videos/misc/853555/ours-vh1-storytellers.jhtml',
21             u'file': u'853555.mp4',
22             u'md5': u'850f3f143316b1e71fa56a4edfd6e0f8',
23             u'info_dict': {
24                 u'title': u'Taylor Swift - "Ours (VH1 Storytellers)"',
25                 u'description': u'Album: Taylor Swift performs "Ours" for VH1 Storytellers at Harvey Mudd College.',
26             },
27         },
28         {
29             u'add_ie': ['Vevo'],
30             u'url': u'http://www.mtv.com/videos/taylor-swift/916187/everything-has-changed-ft-ed-sheeran.jhtml',
31             u'file': u'USCJY1331283.mp4',
32             u'md5': u'73b4e7fcadd88929292fe52c3ced8caf',
33             u'info_dict': {
34                 u'title': u'Everything Has Changed',
35                 u'upload_date': u'20130606',
36                 u'uploader': u'Taylor Swift',
37             },
38             u'skip': u'VEVO is only available in some countries',
39         },
40     ]
41
42     @staticmethod
43     def _id_from_uri(uri):
44         return uri.split(':')[-1]
45
46     # This was originally implemented for ComedyCentral, but it also works here
47     @staticmethod
48     def _transform_rtmp_url(rtmp_video_url):
49         m = re.match(r'^rtmpe?://.*?/(?P<finalid>gsp\..+?/.*)$', rtmp_video_url)
50         if not m:
51             return rtmp_video_url
52         base = 'http://mtvnmobile.vo.llnwd.net/kip0/_pxn=1+_pxI0=Ripod-h264+_pxL0=undefined+_pxM0=+_pxK=18639+_pxE=mp4/44620/mtvnorigin/'
53         return base + m.group('finalid')
54
55     def _get_thumbnail_url(self, uri, itemdoc):
56         return 'http://mtv.mtvnimages.com/uri/' + uri
57
58     def _extract_video_formats(self, metadataXml):
59         if '/error_country_block.swf' in metadataXml:
60             raise ExtractorError(u'This video is not available from your country.', expected=True)
61         mdoc = xml.etree.ElementTree.fromstring(metadataXml.encode('utf-8'))
62
63         formats = []
64         for rendition in mdoc.findall('.//rendition'):
65             try:
66                 _, _, ext = rendition.attrib['type'].partition('/')
67                 rtmp_video_url = rendition.find('./src').text
68                 formats.append({'ext': ext,
69                                 'url': self._transform_rtmp_url(rtmp_video_url),
70                                 'format_id': rendition.get('bitrate'),
71                                 'width': int(rendition.get('width')),
72                                 'height': int(rendition.get('height')),
73                                 })
74             except (KeyError, TypeError):
75                 raise ExtractorError('Invalid rendition field.')
76         return formats
77
78     def _get_video_info(self, itemdoc):
79         uri = itemdoc.find('guid').text
80         video_id = self._id_from_uri(uri)
81         self.report_extraction(video_id)
82         mediagen_url = itemdoc.find('%s/%s' % (_media_xml_tag('group'), _media_xml_tag('content'))).attrib['url']
83         # Remove the templates, like &device={device}
84         mediagen_url = re.sub(r'&[^=]*?={.*?}(?=(&|$))', u'', mediagen_url)
85         if 'acceptMethods' not in mediagen_url:
86             mediagen_url += '&acceptMethods=fms'
87         mediagen_page = self._download_webpage(mediagen_url, video_id,
88                                                u'Downloading video urls')
89
90         description_node = itemdoc.find('description')
91         if description_node is not None:
92             description = description_node.text.strip()
93         else:
94             description = None
95
96         info = {
97             'title': itemdoc.find('title').text,
98             'formats': self._extract_video_formats(mediagen_page),
99             'id': video_id,
100             'thumbnail': self._get_thumbnail_url(uri, itemdoc),
101             'description': description,
102         }
103
104         # TODO: Remove when #980 has been merged
105         info.update(info['formats'][-1])
106
107         return info
108
109     def _get_videos_info(self, uri):
110         video_id = self._id_from_uri(uri)
111         data = compat_urllib_parse.urlencode({'uri': uri})
112         infoXml = self._download_webpage(self._FEED_URL +'?' + data, video_id,
113                                          u'Downloading info')
114         idoc = xml.etree.ElementTree.fromstring(infoXml.encode('utf-8'))
115         return [self._get_video_info(item) for item in idoc.findall('.//item')]
116
117     def _real_extract(self, url):
118         mobj = re.match(self._VALID_URL, url)
119         video_id = mobj.group('videoid')
120
121         webpage = self._download_webpage(url, video_id)
122
123         # Some videos come from Vevo.com
124         m_vevo = re.search(r'isVevoVideo = true;.*?vevoVideoId = "(.*?)";',
125                            webpage, re.DOTALL)
126         if m_vevo:
127             vevo_id = m_vevo.group(1);
128             self.to_screen(u'Vevo video detected: %s' % vevo_id)
129             return self.url_result('vevo:%s' % vevo_id, ie='Vevo')
130
131         uri = self._html_search_regex(r'/uri/(.*?)\?', webpage, u'uri')
132         return self._get_videos_info(uri)