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