]>
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, |
fc287219 | 8 | ExtractorError, |
90834c78 | 9 | find_xpath_attr, |
5aafe895 | 10 | fix_xml_ampersands, |
8d9453b9 JMF |
11 | url_basename, |
12 | RegexNotFoundError, | |
fc287219 PH |
13 | ) |
14 | ||
90834c78 | 15 | |
300fcad8 JMF |
16 | def _media_xml_tag(tag): |
17 | return '{http://search.yahoo.com/mrss/}%s' % tag | |
fc287219 | 18 | |
f7e02595 | 19 | |
84db8181 | 20 | class MTVServicesInfoExtractor(InfoExtractor): |
f7e02595 JMF |
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: | |
63b7b722 | 30 | return rtmp_video_url |
f7e02595 | 31 | base = 'http://mtvnmobile.vo.llnwd.net/kip0/_pxn=1+_pxI0=Ripod-h264+_pxL0=undefined+_pxM0=+_pxK=18639+_pxE=mp4/44620/mtvnorigin/' |
ab2f744b JMF |
32 | return base + m.group('finalid') |
33 | ||
34 | def _get_thumbnail_url(self, uri, itemdoc): | |
84db8181 JMF |
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'] | |
f7e02595 | 41 | |
e4f320a4 JMF |
42 | def _extract_video_formats(self, mdoc): |
43 | if re.match(r'.*/error_country_block\.swf$', mdoc.find('.//src').text) is not None: | |
32dac694 | 44 | raise ExtractorError('This video is not available from your country.', expected=True) |
f7e02595 | 45 | |
f13d0933 PH |
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 | |
f7e02595 JMF |
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) | |
300fcad8 | 65 | mediagen_url = itemdoc.find('%s/%s' % (_media_xml_tag('group'), _media_xml_tag('content'))).attrib['url'] |
321a01f9 | 66 | # Remove the templates, like &device={device} |
32dac694 | 67 | mediagen_url = re.sub(r'&[^=]*?={.*?}(?=(&|$))', '', mediagen_url) |
f7e02595 JMF |
68 | if 'acceptMethods' not in mediagen_url: |
69 | mediagen_url += '&acceptMethods=fms' | |
6562df76 | 70 | |
e4f320a4 JMF |
71 | mediagen_doc = self._download_xml(mediagen_url, video_id, |
72 | 'Downloading video urls') | |
f7e02595 JMF |
73 | |
74 | description_node = itemdoc.find('description') | |
75 | if description_node is not None: | |
0ab4ff63 | 76 | description = description_node.text.strip() |
f7e02595 JMF |
77 | else: |
78 | description = None | |
f13d0933 | 79 | |
90834c78 PH |
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') | |
780ee4e5 | 92 | title = title.strip() |
90834c78 | 93 | |
fb7abb31 | 94 | return { |
90834c78 | 95 | 'title': title, |
e4f320a4 | 96 | 'formats': self._extract_video_formats(mediagen_doc), |
f13d0933 PH |
97 | 'id': video_id, |
98 | 'thumbnail': self._get_thumbnail_url(uri, itemdoc), | |
99 | 'description': description, | |
100 | } | |
101 | ||
f7e02595 JMF |
102 | def _get_videos_info(self, uri): |
103 | video_id = self._id_from_uri(uri) | |
104 | data = compat_urllib_parse.urlencode({'uri': uri}) | |
e2b38da9 | 105 | |
e2b38da9 PH |
106 | idoc = self._download_xml( |
107 | self._FEED_URL + '?' + data, video_id, | |
32dac694 | 108 | 'Downloading info', transform_source=fix_xml_ampersands) |
f7e02595 | 109 | return [self._get_video_info(item) for item in idoc.findall('.//item')] |
fc287219 | 110 | |
8d9453b9 JMF |
111 | def _real_extract(self, url): |
112 | title = url_basename(url) | |
113 | webpage = self._download_webpage(url, title) | |
114 | try: | |
4bbf139a JMF |
115 | # the url can be http://media.mtvnservices.com/fb/{mgid}.swf |
116 | # or http://media.mtvnservices.com/{mgid} | |
117 | og_url = self._og_search_video_url(webpage) | |
118 | mgid = url_basename(og_url) | |
119 | if mgid.endswith('.swf'): | |
120 | mgid = mgid[:-4] | |
8d9453b9 | 121 | except RegexNotFoundError: |
b9381e43 JMF |
122 | mgid = self._search_regex( |
123 | [r'data-mgid="(.*?)"', r'swfobject.embedSWF\(".*?(mgid:.*?)"'], | |
124 | webpage, u'mgid') | |
8d9453b9 JMF |
125 | return self._get_videos_info(mgid) |
126 | ||
84db8181 JMF |
127 | |
128 | class MTVIE(MTVServicesInfoExtractor): | |
5c541b2c JMF |
129 | _VALID_URL = r'''(?x)^https?:// |
130 | (?:(?:www\.)?mtv\.com/videos/.+?/(?P<videoid>[0-9]+)/[^/]+$| | |
131 | m\.mtv\.com/videos/video\.rbml\?.*?id=(?P<mgid>[^&]+))''' | |
84db8181 JMF |
132 | |
133 | _FEED_URL = 'http://www.mtv.com/player/embed/AS3/rss/' | |
134 | ||
135 | _TESTS = [ | |
136 | { | |
32dac694 PH |
137 | 'url': 'http://www.mtv.com/videos/misc/853555/ours-vh1-storytellers.jhtml', |
138 | 'file': '853555.mp4', | |
139 | 'md5': '850f3f143316b1e71fa56a4edfd6e0f8', | |
140 | 'info_dict': { | |
141 | 'title': 'Taylor Swift - "Ours (VH1 Storytellers)"', | |
142 | 'description': 'Album: Taylor Swift performs "Ours" for VH1 Storytellers at Harvey Mudd College.', | |
84db8181 JMF |
143 | }, |
144 | }, | |
145 | { | |
32dac694 PH |
146 | 'add_ie': ['Vevo'], |
147 | 'url': 'http://www.mtv.com/videos/taylor-swift/916187/everything-has-changed-ft-ed-sheeran.jhtml', | |
148 | 'file': 'USCJY1331283.mp4', | |
149 | 'md5': '73b4e7fcadd88929292fe52c3ced8caf', | |
150 | 'info_dict': { | |
151 | 'title': 'Everything Has Changed', | |
152 | 'upload_date': '20130606', | |
153 | 'uploader': 'Taylor Swift', | |
84db8181 | 154 | }, |
32dac694 | 155 | 'skip': 'VEVO is only available in some countries', |
84db8181 JMF |
156 | }, |
157 | ] | |
158 | ||
159 | def _get_thumbnail_url(self, uri, itemdoc): | |
160 | return 'http://mtv.mtvnimages.com/uri/' + uri | |
161 | ||
fc287219 PH |
162 | def _real_extract(self, url): |
163 | mobj = re.match(self._VALID_URL, url) | |
fc287219 | 164 | video_id = mobj.group('videoid') |
c801b205 | 165 | uri = mobj.groupdict().get('mgid') |
5c541b2c JMF |
166 | if uri is None: |
167 | webpage = self._download_webpage(url, video_id) | |
168 | ||
169 | # Some videos come from Vevo.com | |
170 | m_vevo = re.search(r'isVevoVideo = true;.*?vevoVideoId = "(.*?)";', | |
171 | webpage, re.DOTALL) | |
172 | if m_vevo: | |
173 | vevo_id = m_vevo.group(1); | |
32dac694 | 174 | self.to_screen('Vevo video detected: %s' % vevo_id) |
5c541b2c JMF |
175 | return self.url_result('vevo:%s' % vevo_id, ie='Vevo') |
176 | ||
32dac694 | 177 | uri = self._html_search_regex(r'/uri/(.*?)\?', webpage, 'uri') |
f7e02595 | 178 | return self._get_videos_info(uri) |
bc4ba05f JMF |
179 | |
180 | ||
181 | class MTVIggyIE(MTVServicesInfoExtractor): | |
182 | IE_NAME = 'mtviggy.com' | |
183 | _VALID_URL = r'https?://www\.mtviggy\.com/videos/.+' | |
184 | _TEST = { | |
185 | 'url': 'http://www.mtviggy.com/videos/arcade-fire-behind-the-scenes-at-the-biggest-music-experiment-yet/', | |
186 | 'info_dict': { | |
187 | 'id': '984696', | |
188 | 'ext': 'mp4', | |
af1588c0 | 189 | 'title': 'Arcade Fire: Behind the Scenes at the Biggest Music Experiment Yet', |
bc4ba05f JMF |
190 | } |
191 | } | |
192 | _FEED_URL = 'http://all.mtvworldverticals.com/feed-xml/' |