]>
Commit | Line | Data |
---|---|---|
1 | from __future__ import unicode_literals | |
2 | ||
3 | import re | |
4 | ||
5 | from .common import InfoExtractor | |
6 | from ..utils import ( | |
7 | compat_urllib_parse, | |
8 | ExtractorError, | |
9 | find_xpath_attr, | |
10 | fix_xml_ampersands, | |
11 | url_basename, | |
12 | RegexNotFoundError, | |
13 | ) | |
14 | ||
15 | ||
16 | def _media_xml_tag(tag): | |
17 | return '{http://search.yahoo.com/mrss/}%s' % tag | |
18 | ||
19 | ||
20 | class MTVServicesInfoExtractor(InfoExtractor): | |
21 | @staticmethod | |
22 | def _id_from_uri(uri): | |
23 | return uri.split(':')[-1] | |
24 | ||
25 | # This was originally implemented for ComedyCentral, but it also works here | |
26 | @staticmethod | |
27 | def _transform_rtmp_url(rtmp_video_url): | |
28 | m = re.match(r'^rtmpe?://.*?/(?P<finalid>gsp\..+?/.*)$', rtmp_video_url) | |
29 | if not m: | |
30 | return rtmp_video_url | |
31 | base = 'http://mtvnmobile.vo.llnwd.net/kip0/_pxn=1+_pxI0=Ripod-h264+_pxL0=undefined+_pxM0=+_pxK=18639+_pxE=mp4/44620/mtvnorigin/' | |
32 | return base + m.group('finalid') | |
33 | ||
34 | def _get_thumbnail_url(self, uri, itemdoc): | |
35 | search_path = '%s/%s' % (_media_xml_tag('group'), _media_xml_tag('thumbnail')) | |
36 | thumb_node = itemdoc.find(search_path) | |
37 | if thumb_node is None: | |
38 | return None | |
39 | else: | |
40 | return thumb_node.attrib['url'] | |
41 | ||
42 | def _extract_video_formats(self, mdoc): | |
43 | if re.match(r'.*/error_country_block\.swf$', mdoc.find('.//src').text) is not None: | |
44 | raise ExtractorError('This video is not available from your country.', expected=True) | |
45 | ||
46 | formats = [] | |
47 | for rendition in mdoc.findall('.//rendition'): | |
48 | try: | |
49 | _, _, ext = rendition.attrib['type'].partition('/') | |
50 | rtmp_video_url = rendition.find('./src').text | |
51 | formats.append({'ext': ext, | |
52 | 'url': self._transform_rtmp_url(rtmp_video_url), | |
53 | 'format_id': rendition.get('bitrate'), | |
54 | 'width': int(rendition.get('width')), | |
55 | 'height': int(rendition.get('height')), | |
56 | }) | |
57 | except (KeyError, TypeError): | |
58 | raise ExtractorError('Invalid rendition field.') | |
59 | return formats | |
60 | ||
61 | def _get_video_info(self, itemdoc): | |
62 | uri = itemdoc.find('guid').text | |
63 | video_id = self._id_from_uri(uri) | |
64 | self.report_extraction(video_id) | |
65 | mediagen_url = itemdoc.find('%s/%s' % (_media_xml_tag('group'), _media_xml_tag('content'))).attrib['url'] | |
66 | # Remove the templates, like &device={device} | |
67 | mediagen_url = re.sub(r'&[^=]*?={.*?}(?=(&|$))', '', mediagen_url) | |
68 | if 'acceptMethods' not in mediagen_url: | |
69 | mediagen_url += '&acceptMethods=fms' | |
70 | ||
71 | mediagen_doc = self._download_xml(mediagen_url, video_id, | |
72 | 'Downloading video urls') | |
73 | ||
74 | description_node = itemdoc.find('description') | |
75 | if description_node is not None: | |
76 | description = description_node.text.strip() | |
77 | else: | |
78 | description = None | |
79 | ||
80 | title_el = None | |
81 | if title_el is None: | |
82 | title_el = find_xpath_attr( | |
83 | itemdoc, './/{http://search.yahoo.com/mrss/}category', | |
84 | 'scheme', 'urn:mtvn:video_title') | |
85 | if title_el is None: | |
86 | title_el = itemdoc.find('.//{http://search.yahoo.com/mrss/}title') | |
87 | if title_el is None: | |
88 | title_el = itemdoc.find('.//title') | |
89 | title = title_el.text | |
90 | if title is None: | |
91 | raise ExtractorError('Could not find video title') | |
92 | ||
93 | return { | |
94 | 'title': title, | |
95 | 'formats': self._extract_video_formats(mediagen_doc), | |
96 | 'id': video_id, | |
97 | 'thumbnail': self._get_thumbnail_url(uri, itemdoc), | |
98 | 'description': description, | |
99 | } | |
100 | ||
101 | def _get_videos_info(self, uri): | |
102 | video_id = self._id_from_uri(uri) | |
103 | data = compat_urllib_parse.urlencode({'uri': uri}) | |
104 | ||
105 | idoc = self._download_xml( | |
106 | self._FEED_URL + '?' + data, video_id, | |
107 | 'Downloading info', transform_source=fix_xml_ampersands) | |
108 | return [self._get_video_info(item) for item in idoc.findall('.//item')] | |
109 | ||
110 | def _real_extract(self, url): | |
111 | title = url_basename(url) | |
112 | webpage = self._download_webpage(url, title) | |
113 | try: | |
114 | # the url is in the format http://media.mtvnservices.com/fb/{mgid}.swf | |
115 | fb_url = self._og_search_video_url(webpage) | |
116 | mgid = url_basename(fb_url).rpartition('.')[0] | |
117 | except RegexNotFoundError: | |
118 | mgid = self._search_regex(r'data-mgid="(.*?)"', webpage, u'mgid') | |
119 | return self._get_videos_info(mgid) | |
120 | ||
121 | ||
122 | class MTVIE(MTVServicesInfoExtractor): | |
123 | _VALID_URL = r'''(?x)^https?:// | |
124 | (?:(?:www\.)?mtv\.com/videos/.+?/(?P<videoid>[0-9]+)/[^/]+$| | |
125 | m\.mtv\.com/videos/video\.rbml\?.*?id=(?P<mgid>[^&]+))''' | |
126 | ||
127 | _FEED_URL = 'http://www.mtv.com/player/embed/AS3/rss/' | |
128 | ||
129 | _TESTS = [ | |
130 | { | |
131 | 'url': 'http://www.mtv.com/videos/misc/853555/ours-vh1-storytellers.jhtml', | |
132 | 'file': '853555.mp4', | |
133 | 'md5': '850f3f143316b1e71fa56a4edfd6e0f8', | |
134 | 'info_dict': { | |
135 | 'title': 'Taylor Swift - "Ours (VH1 Storytellers)"', | |
136 | 'description': 'Album: Taylor Swift performs "Ours" for VH1 Storytellers at Harvey Mudd College.', | |
137 | }, | |
138 | }, | |
139 | { | |
140 | 'add_ie': ['Vevo'], | |
141 | 'url': 'http://www.mtv.com/videos/taylor-swift/916187/everything-has-changed-ft-ed-sheeran.jhtml', | |
142 | 'file': 'USCJY1331283.mp4', | |
143 | 'md5': '73b4e7fcadd88929292fe52c3ced8caf', | |
144 | 'info_dict': { | |
145 | 'title': 'Everything Has Changed', | |
146 | 'upload_date': '20130606', | |
147 | 'uploader': 'Taylor Swift', | |
148 | }, | |
149 | 'skip': 'VEVO is only available in some countries', | |
150 | }, | |
151 | ] | |
152 | ||
153 | def _get_thumbnail_url(self, uri, itemdoc): | |
154 | return 'http://mtv.mtvnimages.com/uri/' + uri | |
155 | ||
156 | def _real_extract(self, url): | |
157 | mobj = re.match(self._VALID_URL, url) | |
158 | video_id = mobj.group('videoid') | |
159 | uri = mobj.groupdict().get('mgid') | |
160 | if uri is None: | |
161 | webpage = self._download_webpage(url, video_id) | |
162 | ||
163 | # Some videos come from Vevo.com | |
164 | m_vevo = re.search(r'isVevoVideo = true;.*?vevoVideoId = "(.*?)";', | |
165 | webpage, re.DOTALL) | |
166 | if m_vevo: | |
167 | vevo_id = m_vevo.group(1); | |
168 | self.to_screen('Vevo video detected: %s' % vevo_id) | |
169 | return self.url_result('vevo:%s' % vevo_id, ie='Vevo') | |
170 | ||
171 | uri = self._html_search_regex(r'/uri/(.*?)\?', webpage, 'uri') | |
172 | return self._get_videos_info(uri) | |
173 | ||
174 | ||
175 | class MTVIggyIE(MTVServicesInfoExtractor): | |
176 | IE_NAME = 'mtviggy.com' | |
177 | _VALID_URL = r'https?://www\.mtviggy\.com/videos/.+' | |
178 | _TEST = { | |
179 | 'url': 'http://www.mtviggy.com/videos/arcade-fire-behind-the-scenes-at-the-biggest-music-experiment-yet/', | |
180 | 'info_dict': { | |
181 | 'id': '984696', | |
182 | 'ext': 'mp4', | |
183 | 'title': 'Arcade Fire: Behind the Scenes at the Biggest Music Experiment Yet', | |
184 | } | |
185 | } | |
186 | _FEED_URL = 'http://all.mtvworldverticals.com/feed-xml/' |