1 from __future__ import unicode_literals
5 from .common import InfoExtractor
16 def _media_xml_tag(tag):
17 return '{http://search.yahoo.com/mrss/}%s' % tag
20 class MTVServicesInfoExtractor(InfoExtractor):
22 def _id_from_uri(uri):
23 return uri.split(':')[-1]
25 # This was originally implemented for ComedyCentral, but it also works here
27 def _transform_rtmp_url(rtmp_video_url):
28 m = re.match(r'^rtmpe?://.*?/(?P<finalid>gsp\..+?/.*)$', 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')
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:
40 return thumb_node.attrib['url']
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)
47 for rendition in mdoc.findall('.//rendition'):
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')),
57 except (KeyError, TypeError):
58 raise ExtractorError('Invalid rendition field.')
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'
71 mediagen_doc = self._download_xml(mediagen_url, video_id,
72 'Downloading video urls')
74 description_node = itemdoc.find('description')
75 if description_node is not None:
76 description = description_node.text.strip()
82 title_el = find_xpath_attr(
83 itemdoc, './/{http://search.yahoo.com/mrss/}category',
84 'scheme', 'urn:mtvn:video_title')
86 title_el = itemdoc.find('.//{http://search.yahoo.com/mrss/}title')
88 title_el = itemdoc.find('./title')
89 if title_el.text is None:
92 title_el = itemdoc.find('./item/title')
96 raise ExtractorError('Could not find video title')
101 'formats': self._extract_video_formats(mediagen_doc),
103 'thumbnail': self._get_thumbnail_url(uri, itemdoc),
104 'description': description,
107 def _get_videos_info(self, uri):
108 video_id = self._id_from_uri(uri)
109 data = compat_urllib_parse.urlencode({'uri': uri})
111 idoc = self._download_xml(
112 self._FEED_URL + '?' + data, video_id,
113 'Downloading info', transform_source=fix_xml_ampersands)
114 return [self._get_video_info(item) for item in idoc.findall('.//item')]
116 def _real_extract(self, url):
117 title = url_basename(url)
118 webpage = self._download_webpage(url, title)
120 # the url can be http://media.mtvnservices.com/fb/{mgid}.swf
121 # or http://media.mtvnservices.com/{mgid}
122 og_url = self._og_search_video_url(webpage)
123 mgid = url_basename(og_url)
124 if mgid.endswith('.swf'):
126 except RegexNotFoundError:
127 mgid = self._search_regex(
128 [r'data-mgid="(.*?)"', r'swfobject.embedSWF\(".*?(mgid:.*?)"'],
130 return self._get_videos_info(mgid)
133 class MTVIE(MTVServicesInfoExtractor):
134 _VALID_URL = r'''(?x)^https?://
135 (?:(?:www\.)?mtv\.com/videos/.+?/(?P<videoid>[0-9]+)/[^/]+$|
136 m\.mtv\.com/videos/video\.rbml\?.*?id=(?P<mgid>[^&]+))'''
138 _FEED_URL = 'http://www.mtv.com/player/embed/AS3/rss/'
142 'url': 'http://www.mtv.com/videos/misc/853555/ours-vh1-storytellers.jhtml',
143 'file': '853555.mp4',
144 'md5': '850f3f143316b1e71fa56a4edfd6e0f8',
146 'title': 'Taylor Swift - "Ours (VH1 Storytellers)"',
147 'description': 'Album: Taylor Swift performs "Ours" for VH1 Storytellers at Harvey Mudd College.',
152 'url': 'http://www.mtv.com/videos/taylor-swift/916187/everything-has-changed-ft-ed-sheeran.jhtml',
153 'file': 'USCJY1331283.mp4',
154 'md5': '73b4e7fcadd88929292fe52c3ced8caf',
156 'title': 'Everything Has Changed',
157 'upload_date': '20130606',
158 'uploader': 'Taylor Swift',
160 'skip': 'VEVO is only available in some countries',
164 def _get_thumbnail_url(self, uri, itemdoc):
165 return 'http://mtv.mtvnimages.com/uri/' + uri
167 def _real_extract(self, url):
168 mobj = re.match(self._VALID_URL, url)
169 video_id = mobj.group('videoid')
170 uri = mobj.groupdict().get('mgid')
172 webpage = self._download_webpage(url, video_id)
174 # Some videos come from Vevo.com
175 m_vevo = re.search(r'isVevoVideo = true;.*?vevoVideoId = "(.*?)";',
178 vevo_id = m_vevo.group(1);
179 self.to_screen('Vevo video detected: %s' % vevo_id)
180 return self.url_result('vevo:%s' % vevo_id, ie='Vevo')
182 uri = self._html_search_regex(r'/uri/(.*?)\?', webpage, 'uri')
183 return self._get_videos_info(uri)
186 class MTVIggyIE(MTVServicesInfoExtractor):
187 IE_NAME = 'mtviggy.com'
188 _VALID_URL = r'https?://www\.mtviggy\.com/videos/.+'
190 'url': 'http://www.mtviggy.com/videos/arcade-fire-behind-the-scenes-at-the-biggest-music-experiment-yet/',
194 'title': 'Arcade Fire: Behind the Scenes at the Biggest Music Experiment Yet',
197 _FEED_URL = 'http://all.mtvworldverticals.com/feed-xml/'