1 from __future__ import unicode_literals
5 from .common import InfoExtractor
14 def _media_xml_tag(tag):
15 return '{http://search.yahoo.com/mrss/}%s' % tag
18 class MTVServicesInfoExtractor(InfoExtractor):
20 def _id_from_uri(uri):
21 return uri.split(':')[-1]
23 # This was originally implemented for ComedyCentral, but it also works here
25 def _transform_rtmp_url(rtmp_video_url):
26 m = re.match(r'^rtmpe?://.*?/(?P<finalid>gsp\..+?/.*)$', rtmp_video_url)
29 base = 'http://mtvnmobile.vo.llnwd.net/kip0/_pxn=1+_pxI0=Ripod-h264+_pxL0=undefined+_pxM0=+_pxK=18639+_pxE=mp4/44620/mtvnorigin/'
30 return base + m.group('finalid')
32 def _get_thumbnail_url(self, uri, itemdoc):
33 search_path = '%s/%s' % (_media_xml_tag('group'), _media_xml_tag('thumbnail'))
34 thumb_node = itemdoc.find(search_path)
35 if thumb_node is None:
38 return thumb_node.attrib['url']
40 def _extract_video_formats(self, mdoc):
41 if re.match(r'.*/error_country_block\.swf$', mdoc.find('.//src').text) is not None:
42 raise ExtractorError('This video is not available from your country.', expected=True)
45 for rendition in mdoc.findall('.//rendition'):
47 _, _, ext = rendition.attrib['type'].partition('/')
48 rtmp_video_url = rendition.find('./src').text
49 formats.append({'ext': ext,
50 'url': self._transform_rtmp_url(rtmp_video_url),
51 'format_id': rendition.get('bitrate'),
52 'width': int(rendition.get('width')),
53 'height': int(rendition.get('height')),
55 except (KeyError, TypeError):
56 raise ExtractorError('Invalid rendition field.')
59 def _get_video_info(self, itemdoc):
60 uri = itemdoc.find('guid').text
61 video_id = self._id_from_uri(uri)
62 self.report_extraction(video_id)
63 mediagen_url = itemdoc.find('%s/%s' % (_media_xml_tag('group'), _media_xml_tag('content'))).attrib['url']
64 # Remove the templates, like &device={device}
65 mediagen_url = re.sub(r'&[^=]*?={.*?}(?=(&|$))', '', mediagen_url)
66 if 'acceptMethods' not in mediagen_url:
67 mediagen_url += '&acceptMethods=fms'
68 mediagen_doc = self._download_xml(mediagen_url, video_id,
69 'Downloading video urls')
71 description_node = itemdoc.find('description')
72 if description_node is not None:
73 description = description_node.text.strip()
78 'title': itemdoc.find('title').text,
79 'formats': self._extract_video_formats(mediagen_doc),
81 'thumbnail': self._get_thumbnail_url(uri, itemdoc),
82 'description': description,
85 def _get_videos_info(self, uri):
86 video_id = self._id_from_uri(uri)
87 data = compat_urllib_parse.urlencode({'uri': uri})
89 idoc = self._download_xml(
90 self._FEED_URL + '?' + data, video_id,
91 'Downloading info', transform_source=fix_xml_ampersands)
92 return [self._get_video_info(item) for item in idoc.findall('.//item')]
94 def _real_extract(self, url):
95 title = url_basename(url)
96 webpage = self._download_webpage(url, title)
98 # the url is in the format http://media.mtvnservices.com/fb/{mgid}.swf
99 fb_url = self._og_search_video_url(webpage)
100 mgid = url_basename(fb_url).rpartition('.')[0]
101 except RegexNotFoundError:
102 mgid = self._search_regex(r'data-mgid="(.*?)"', webpage, u'mgid')
103 return self._get_videos_info(mgid)
106 class MTVIE(MTVServicesInfoExtractor):
107 _VALID_URL = r'''(?x)^https?://
108 (?:(?:www\.)?mtv\.com/videos/.+?/(?P<videoid>[0-9]+)/[^/]+$|
109 m\.mtv\.com/videos/video\.rbml\?.*?id=(?P<mgid>[^&]+))'''
111 _FEED_URL = 'http://www.mtv.com/player/embed/AS3/rss/'
115 'url': 'http://www.mtv.com/videos/misc/853555/ours-vh1-storytellers.jhtml',
116 'file': '853555.mp4',
117 'md5': '850f3f143316b1e71fa56a4edfd6e0f8',
119 'title': 'Taylor Swift - "Ours (VH1 Storytellers)"',
120 'description': 'Album: Taylor Swift performs "Ours" for VH1 Storytellers at Harvey Mudd College.',
125 'url': 'http://www.mtv.com/videos/taylor-swift/916187/everything-has-changed-ft-ed-sheeran.jhtml',
126 'file': 'USCJY1331283.mp4',
127 'md5': '73b4e7fcadd88929292fe52c3ced8caf',
129 'title': 'Everything Has Changed',
130 'upload_date': '20130606',
131 'uploader': 'Taylor Swift',
133 'skip': 'VEVO is only available in some countries',
137 def _get_thumbnail_url(self, uri, itemdoc):
138 return 'http://mtv.mtvnimages.com/uri/' + uri
140 def _real_extract(self, url):
141 mobj = re.match(self._VALID_URL, url)
142 video_id = mobj.group('videoid')
143 uri = mobj.groupdict().get('mgid')
145 webpage = self._download_webpage(url, video_id)
147 # Some videos come from Vevo.com
148 m_vevo = re.search(r'isVevoVideo = true;.*?vevoVideoId = "(.*?)";',
151 vevo_id = m_vevo.group(1);
152 self.to_screen('Vevo video detected: %s' % vevo_id)
153 return self.url_result('vevo:%s' % vevo_id, ie='Vevo')
155 uri = self._html_search_regex(r'/uri/(.*?)\?', webpage, 'uri')
156 return self._get_videos_info(uri)