]>
Commit | Line | Data |
---|---|---|
32dac694 PH |
1 | from __future__ import unicode_literals |
2 | ||
fc287219 | 3 | import re |
fc287219 PH |
4 | |
5 | from .common import InfoExtractor | |
6 | from ..utils import ( | |
f7e02595 | 7 | compat_urllib_parse, |
340b0468 | 8 | compat_urllib_request, |
fc287219 | 9 | ExtractorError, |
90834c78 | 10 | find_xpath_attr, |
5aafe895 | 11 | fix_xml_ampersands, |
0ef68e04 | 12 | HEADRequest, |
340b0468 | 13 | unescapeHTML, |
8d9453b9 JMF |
14 | url_basename, |
15 | RegexNotFoundError, | |
fc287219 PH |
16 | ) |
17 | ||
90834c78 | 18 | |
300fcad8 JMF |
19 | def _media_xml_tag(tag): |
20 | return '{http://search.yahoo.com/mrss/}%s' % tag | |
fc287219 | 21 | |
f7e02595 | 22 | |
84db8181 | 23 | class MTVServicesInfoExtractor(InfoExtractor): |
340b0468 | 24 | _MOBILE_TEMPLATE = None |
8940c1c0 | 25 | |
f7e02595 JMF |
26 | @staticmethod |
27 | def _id_from_uri(uri): | |
28 | return uri.split(':')[-1] | |
29 | ||
30 | # This was originally implemented for ComedyCentral, but it also works here | |
31 | @staticmethod | |
32 | def _transform_rtmp_url(rtmp_video_url): | |
33 | m = re.match(r'^rtmpe?://.*?/(?P<finalid>gsp\..+?/.*)$', rtmp_video_url) | |
34 | if not m: | |
63b7b722 | 35 | return rtmp_video_url |
f7e02595 | 36 | base = 'http://mtvnmobile.vo.llnwd.net/kip0/_pxn=1+_pxI0=Ripod-h264+_pxL0=undefined+_pxM0=+_pxK=18639+_pxE=mp4/44620/mtvnorigin/' |
ab2f744b JMF |
37 | return base + m.group('finalid') |
38 | ||
8940c1c0 JMF |
39 | def _get_feed_url(self, uri): |
40 | return self._FEED_URL | |
41 | ||
ab2f744b | 42 | def _get_thumbnail_url(self, uri, itemdoc): |
84db8181 JMF |
43 | search_path = '%s/%s' % (_media_xml_tag('group'), _media_xml_tag('thumbnail')) |
44 | thumb_node = itemdoc.find(search_path) | |
45 | if thumb_node is None: | |
46 | return None | |
47 | else: | |
48 | return thumb_node.attrib['url'] | |
f7e02595 | 49 | |
340b0468 JMF |
50 | def _extract_mobile_video_formats(self, mtvn_id): |
51 | webpage_url = self._MOBILE_TEMPLATE % mtvn_id | |
52 | req = compat_urllib_request.Request(webpage_url) | |
53 | # Otherwise we get a webpage that would execute some javascript | |
54 | req.add_header('Youtubedl-user-agent', 'curl/7') | |
55 | webpage = self._download_webpage(req, mtvn_id, | |
56 | 'Downloading mobile page') | |
0ef68e04 JMF |
57 | metrics_url = unescapeHTML(self._search_regex(r'<a href="(http://metrics.+?)"', webpage, 'url')) |
58 | req = HEADRequest(metrics_url) | |
59 | response = self._request_webpage(req, mtvn_id, 'Resolving url') | |
60 | url = response.geturl() | |
61 | # Transform the url to get the best quality: | |
62 | url = re.sub(r'.+pxE=mp4', 'http://mtvnmobile.vo.llnwd.net/kip0/_pxn=0+_pxK=18639+_pxE=mp4', url, 1) | |
63 | return [{'url': url,'ext': 'mp4'}] | |
340b0468 JMF |
64 | |
65 | def _extract_video_formats(self, mdoc, mtvn_id): | |
cc1db7f9 | 66 | if re.match(r'.*/(error_country_block\.swf|geoblock\.mp4)$', mdoc.find('.//src').text) is not None: |
340b0468 | 67 | if mtvn_id is not None and self._MOBILE_TEMPLATE is not None: |
0ef68e04 JMF |
68 | self.to_screen('The normal version is not available from your ' |
69 | 'country, trying with the mobile version') | |
340b0468 | 70 | return self._extract_mobile_video_formats(mtvn_id) |
cc1db7f9 JMF |
71 | raise ExtractorError('This video is not available from your country.', |
72 | expected=True) | |
f7e02595 | 73 | |
f13d0933 PH |
74 | formats = [] |
75 | for rendition in mdoc.findall('.//rendition'): | |
76 | try: | |
77 | _, _, ext = rendition.attrib['type'].partition('/') | |
78 | rtmp_video_url = rendition.find('./src').text | |
79 | formats.append({'ext': ext, | |
80 | 'url': self._transform_rtmp_url(rtmp_video_url), | |
81 | 'format_id': rendition.get('bitrate'), | |
82 | 'width': int(rendition.get('width')), | |
83 | 'height': int(rendition.get('height')), | |
84 | }) | |
85 | except (KeyError, TypeError): | |
86 | raise ExtractorError('Invalid rendition field.') | |
34d863f3 | 87 | self._sort_formats(formats) |
f13d0933 | 88 | return formats |
f7e02595 JMF |
89 | |
90 | def _get_video_info(self, itemdoc): | |
91 | uri = itemdoc.find('guid').text | |
92 | video_id = self._id_from_uri(uri) | |
93 | self.report_extraction(video_id) | |
300fcad8 | 94 | mediagen_url = itemdoc.find('%s/%s' % (_media_xml_tag('group'), _media_xml_tag('content'))).attrib['url'] |
321a01f9 | 95 | # Remove the templates, like &device={device} |
32dac694 | 96 | mediagen_url = re.sub(r'&[^=]*?={.*?}(?=(&|$))', '', mediagen_url) |
f7e02595 JMF |
97 | if 'acceptMethods' not in mediagen_url: |
98 | mediagen_url += '&acceptMethods=fms' | |
6562df76 | 99 | |
e4f320a4 JMF |
100 | mediagen_doc = self._download_xml(mediagen_url, video_id, |
101 | 'Downloading video urls') | |
f7e02595 JMF |
102 | |
103 | description_node = itemdoc.find('description') | |
104 | if description_node is not None: | |
0ab4ff63 | 105 | description = description_node.text.strip() |
f7e02595 JMF |
106 | else: |
107 | description = None | |
f13d0933 | 108 | |
90834c78 PH |
109 | title_el = None |
110 | if title_el is None: | |
111 | title_el = find_xpath_attr( | |
112 | itemdoc, './/{http://search.yahoo.com/mrss/}category', | |
113 | 'scheme', 'urn:mtvn:video_title') | |
90834c78 | 114 | if title_el is None: |
96cb10a5 S |
115 | title_el = itemdoc.find('.//{http://search.yahoo.com/mrss/}title') |
116 | if title_el is None: | |
713d31fa | 117 | title_el = itemdoc.find('.//title') |
1df4229b PH |
118 | if title_el.text is None: |
119 | title_el = None | |
1df4229b | 120 | |
90834c78 PH |
121 | title = title_el.text |
122 | if title is None: | |
123 | raise ExtractorError('Could not find video title') | |
780ee4e5 | 124 | title = title.strip() |
90834c78 | 125 | |
340b0468 JMF |
126 | # This a short id that's used in the webpage urls |
127 | mtvn_id = None | |
128 | mtvn_id_node = find_xpath_attr(itemdoc, './/{http://search.yahoo.com/mrss/}category', | |
129 | 'scheme', 'urn:mtvn:id') | |
130 | if mtvn_id_node is not None: | |
131 | mtvn_id = mtvn_id_node.text | |
132 | ||
fb7abb31 | 133 | return { |
90834c78 | 134 | 'title': title, |
340b0468 | 135 | 'formats': self._extract_video_formats(mediagen_doc, mtvn_id), |
f13d0933 PH |
136 | 'id': video_id, |
137 | 'thumbnail': self._get_thumbnail_url(uri, itemdoc), | |
138 | 'description': description, | |
139 | } | |
140 | ||
f7e02595 JMF |
141 | def _get_videos_info(self, uri): |
142 | video_id = self._id_from_uri(uri) | |
8940c1c0 | 143 | feed_url = self._get_feed_url(uri) |
f7e02595 | 144 | data = compat_urllib_parse.urlencode({'uri': uri}) |
e2b38da9 | 145 | idoc = self._download_xml( |
8940c1c0 | 146 | feed_url + '?' + data, video_id, |
32dac694 | 147 | 'Downloading info', transform_source=fix_xml_ampersands) |
f7e02595 | 148 | return [self._get_video_info(item) for item in idoc.findall('.//item')] |
fc287219 | 149 | |
8d9453b9 JMF |
150 | def _real_extract(self, url): |
151 | title = url_basename(url) | |
152 | webpage = self._download_webpage(url, title) | |
153 | try: | |
4bbf139a JMF |
154 | # the url can be http://media.mtvnservices.com/fb/{mgid}.swf |
155 | # or http://media.mtvnservices.com/{mgid} | |
156 | og_url = self._og_search_video_url(webpage) | |
157 | mgid = url_basename(og_url) | |
158 | if mgid.endswith('.swf'): | |
159 | mgid = mgid[:-4] | |
8d9453b9 | 160 | except RegexNotFoundError: |
b1298d8e AMW |
161 | mgid = None |
162 | ||
163 | if mgid is None or ':' not in mgid: | |
b9381e43 JMF |
164 | mgid = self._search_regex( |
165 | [r'data-mgid="(.*?)"', r'swfobject.embedSWF\(".*?(mgid:.*?)"'], | |
166 | webpage, u'mgid') | |
8d9453b9 JMF |
167 | return self._get_videos_info(mgid) |
168 | ||
84db8181 | 169 | |
8940c1c0 JMF |
170 | class MTVServicesEmbeddedIE(MTVServicesInfoExtractor): |
171 | IE_NAME = 'mtvservices:embedded' | |
172 | _VALID_URL = r'https?://media\.mtvnservices\.com/embed/(?P<mgid>.+?)(\?|/|$)' | |
173 | ||
174 | _TEST = { | |
175 | # From http://www.thewrap.com/peter-dinklage-sums-up-game-of-thrones-in-45-seconds-video/ | |
176 | 'url': 'http://media.mtvnservices.com/embed/mgid:uma:video:mtv.com:1043906/cp~vid%3D1043906%26uri%3Dmgid%3Auma%3Avideo%3Amtv.com%3A1043906', | |
177 | 'md5': 'cb349b21a7897164cede95bd7bf3fbb9', | |
178 | 'info_dict': { | |
179 | 'id': '1043906', | |
180 | 'ext': 'mp4', | |
181 | 'title': 'Peter Dinklage Sums Up \'Game Of Thrones\' In 45 Seconds', | |
182 | 'description': '"Sexy sexy sexy, stabby stabby stabby, beautiful language," says Peter Dinklage as he tries summarizing "Game of Thrones" in under a minute.', | |
183 | }, | |
184 | } | |
185 | ||
186 | def _get_feed_url(self, uri): | |
187 | video_id = self._id_from_uri(uri) | |
188 | site_id = uri.replace(video_id, '') | |
189 | config_url = 'http://media.mtvnservices.com/pmt/e1/players/{0}/config.xml'.format(site_id) | |
190 | config_doc = self._download_xml(config_url, video_id) | |
191 | feed_node = config_doc.find('.//feed') | |
192 | feed_url = feed_node.text.strip().split('?')[0] | |
193 | return feed_url | |
194 | ||
195 | def _real_extract(self, url): | |
196 | mobj = re.match(self._VALID_URL, url) | |
197 | mgid = mobj.group('mgid') | |
198 | return self._get_videos_info(mgid) | |
199 | ||
200 | ||
84db8181 | 201 | class MTVIE(MTVServicesInfoExtractor): |
5c541b2c JMF |
202 | _VALID_URL = r'''(?x)^https?:// |
203 | (?:(?:www\.)?mtv\.com/videos/.+?/(?P<videoid>[0-9]+)/[^/]+$| | |
204 | m\.mtv\.com/videos/video\.rbml\?.*?id=(?P<mgid>[^&]+))''' | |
84db8181 JMF |
205 | |
206 | _FEED_URL = 'http://www.mtv.com/player/embed/AS3/rss/' | |
207 | ||
208 | _TESTS = [ | |
209 | { | |
32dac694 PH |
210 | 'url': 'http://www.mtv.com/videos/misc/853555/ours-vh1-storytellers.jhtml', |
211 | 'file': '853555.mp4', | |
212 | 'md5': '850f3f143316b1e71fa56a4edfd6e0f8', | |
213 | 'info_dict': { | |
214 | 'title': 'Taylor Swift - "Ours (VH1 Storytellers)"', | |
215 | 'description': 'Album: Taylor Swift performs "Ours" for VH1 Storytellers at Harvey Mudd College.', | |
84db8181 JMF |
216 | }, |
217 | }, | |
218 | { | |
32dac694 PH |
219 | 'add_ie': ['Vevo'], |
220 | 'url': 'http://www.mtv.com/videos/taylor-swift/916187/everything-has-changed-ft-ed-sheeran.jhtml', | |
221 | 'file': 'USCJY1331283.mp4', | |
222 | 'md5': '73b4e7fcadd88929292fe52c3ced8caf', | |
223 | 'info_dict': { | |
224 | 'title': 'Everything Has Changed', | |
225 | 'upload_date': '20130606', | |
226 | 'uploader': 'Taylor Swift', | |
84db8181 | 227 | }, |
32dac694 | 228 | 'skip': 'VEVO is only available in some countries', |
84db8181 JMF |
229 | }, |
230 | ] | |
231 | ||
232 | def _get_thumbnail_url(self, uri, itemdoc): | |
233 | return 'http://mtv.mtvnimages.com/uri/' + uri | |
234 | ||
fc287219 PH |
235 | def _real_extract(self, url): |
236 | mobj = re.match(self._VALID_URL, url) | |
fc287219 | 237 | video_id = mobj.group('videoid') |
c801b205 | 238 | uri = mobj.groupdict().get('mgid') |
5c541b2c JMF |
239 | if uri is None: |
240 | webpage = self._download_webpage(url, video_id) | |
241 | ||
242 | # Some videos come from Vevo.com | |
243 | m_vevo = re.search(r'isVevoVideo = true;.*?vevoVideoId = "(.*?)";', | |
244 | webpage, re.DOTALL) | |
245 | if m_vevo: | |
246 | vevo_id = m_vevo.group(1); | |
32dac694 | 247 | self.to_screen('Vevo video detected: %s' % vevo_id) |
5c541b2c JMF |
248 | return self.url_result('vevo:%s' % vevo_id, ie='Vevo') |
249 | ||
32dac694 | 250 | uri = self._html_search_regex(r'/uri/(.*?)\?', webpage, 'uri') |
f7e02595 | 251 | return self._get_videos_info(uri) |
bc4ba05f JMF |
252 | |
253 | ||
254 | class MTVIggyIE(MTVServicesInfoExtractor): | |
255 | IE_NAME = 'mtviggy.com' | |
256 | _VALID_URL = r'https?://www\.mtviggy\.com/videos/.+' | |
257 | _TEST = { | |
258 | 'url': 'http://www.mtviggy.com/videos/arcade-fire-behind-the-scenes-at-the-biggest-music-experiment-yet/', | |
259 | 'info_dict': { | |
260 | 'id': '984696', | |
261 | 'ext': 'mp4', | |
af1588c0 | 262 | 'title': 'Arcade Fire: Behind the Scenes at the Biggest Music Experiment Yet', |
bc4ba05f JMF |
263 | } |
264 | } | |
265 | _FEED_URL = 'http://all.mtvworldverticals.com/feed-xml/' |