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