1from __future__ import unicode_literals 2 3import json 4import re 5 6from .common import InfoExtractor 7from ..compat import ( 8 compat_parse_qs, 9 compat_urllib_parse, 10 compat_urllib_parse_unquote, 11) 12from ..utils import ( 13 determine_ext, 14 ExtractorError, 15 int_or_none, 16 get_element_by_attribute, 17 mimetype2ext, 18) 19 20 21class MetacafeIE(InfoExtractor): 22 _VALID_URL = r'https?://(?:www\.)?metacafe\.com/watch/(?P<id>[^/]+)/(?P<display_id>[^/?#]+)' 23 _DISCLAIMER = 'http://www.metacafe.com/family_filter/' 24 _FILTER_POST = 'http://www.metacafe.com/f/index.php?inputType=filter&controllerGroup=user' 25 IE_NAME = 'metacafe' 26 _TESTS = [ 27 # Youtube video 28 { 29 'add_ie': ['Youtube'], 30 'url': 'http://metacafe.com/watch/yt-_aUehQsCQtM/the_electric_company_short_i_pbs_kids_go/', 31 'info_dict': { 32 'id': '_aUehQsCQtM', 33 'ext': 'mp4', 34 'upload_date': '20090102', 35 'title': 'The Electric Company | "Short I" | PBS KIDS GO!', 36 'description': 'md5:2439a8ef6d5a70e380c22f5ad323e5a8', 37 'uploader': 'PBS', 38 'uploader_id': 'PBS' 39 } 40 }, 41 # Normal metacafe video 42 { 43 'url': 'http://www.metacafe.com/watch/11121940/news_stuff_you_wont_do_with_your_playstation_4/', 44 'md5': '6e0bca200eaad2552e6915ed6fd4d9ad', 45 'info_dict': { 46 'id': '11121940', 47 'ext': 'mp4', 48 'title': 'News: Stuff You Won\'t Do with Your PlayStation 4', 49 'uploader': 'ign', 50 'description': 'Sony released a massive FAQ on the PlayStation Blog detailing the PS4\'s capabilities and limitations.', 51 }, 52 'skip': 'Page is temporarily unavailable.', 53 }, 54 # metacafe video with family filter 55 { 56 'url': 'http://www.metacafe.com/watch/2155630/adult_art_by_david_hart_156/', 57 'md5': 'b06082c5079bbdcde677a6291fbdf376', 58 'info_dict': { 59 'id': '2155630', 60 'ext': 'mp4', 61 'title': 'Adult Art By David Hart 156', 62 'uploader': '63346', 63 'description': 'md5:9afac8fc885252201ad14563694040fc', 64 }, 65 'params': { 66 'skip_download': True, 67 }, 68 }, 69 # AnyClip video 70 { 71 'url': 'http://www.metacafe.com/watch/an-dVVXnuY7Jh77J/the_andromeda_strain_1971_stop_the_bomb_part_3/', 72 'info_dict': { 73 'id': 'an-dVVXnuY7Jh77J', 74 'ext': 'mp4', 75 'title': 'The Andromeda Strain (1971): Stop the Bomb Part 3', 76 'uploader': 'AnyClip', 77 'description': 'md5:cbef0460d31e3807f6feb4e7a5952e5b', 78 }, 79 }, 80 # age-restricted video 81 { 82 'url': 'http://www.metacafe.com/watch/5186653/bbc_internal_christmas_tape_79_uncensored_outtakes_etc/', 83 'md5': '98dde7c1a35d02178e8ab7560fe8bd09', 84 'info_dict': { 85 'id': '5186653', 86 'ext': 'mp4', 87 'title': 'BBC INTERNAL Christmas Tape \'79 - UNCENSORED Outtakes, Etc.', 88 'uploader': 'Dwayne Pipe', 89 'description': 'md5:950bf4c581e2c059911fa3ffbe377e4b', 90 'age_limit': 18, 91 }, 92 }, 93 # cbs video 94 { 95 'url': 'http://www.metacafe.com/watch/cb-8VD4r_Zws8VP/open_this_is_face_the_nation_february_9/', 96 'info_dict': { 97 'id': '8VD4r_Zws8VP', 98 'ext': 'flv', 99 'title': 'Open: This is Face the Nation, February 9', 100 'description': 'md5:8a9ceec26d1f7ed6eab610834cc1a476', 101 'duration': 96, 102 'uploader': 'CBSI-NEW', 103 'upload_date': '20140209', 104 'timestamp': 1391959800, 105 }, 106 'params': { 107 # rtmp download 108 'skip_download': True, 109 }, 110 }, 111 # Movieclips.com video 112 { 113 'url': 'http://www.metacafe.com/watch/mv-Wy7ZU/my_week_with_marilyn_do_you_love_me/', 114 'info_dict': { 115 'id': 'mv-Wy7ZU', 116 'ext': 'mp4', 117 'title': 'My Week with Marilyn - Do You Love Me?', 118 'description': 'From the movie My Week with Marilyn - Colin (Eddie Redmayne) professes his love to Marilyn (Michelle Williams) and gets her to promise to return to set and finish the movie.', 119 'uploader': 'movie_trailers', 120 'duration': 176, 121 }, 122 'params': { 123 'skip_download': 'requires rtmpdump', 124 } 125 } 126 ] 127 128 def report_disclaimer(self): 129 self.to_screen('Retrieving disclaimer') 130 131 def _real_extract(self, url): 132 # Extract id and simplified title from URL 133 video_id, display_id = self._match_valid_url(url).groups() 134 135 # the video may come from an external site 136 m_external = re.match(r'^(\w{2})-(.*)$', video_id) 137 if m_external is not None: 138 prefix, ext_id = m_external.groups() 139 # Check if video comes from YouTube 140 if prefix == 'yt': 141 return self.url_result('http://www.youtube.com/watch?v=%s' % ext_id, 'Youtube') 142 # CBS videos use theplatform.com 143 if prefix == 'cb': 144 return self.url_result('theplatform:%s' % ext_id, 'ThePlatform') 145 146 headers = { 147 # Disable family filter 148 'Cookie': 'user=%s; ' % compat_urllib_parse.quote(json.dumps({'ffilter': False})) 149 } 150 151 # AnyClip videos require the flashversion cookie so that we get the link 152 # to the mp4 file 153 if video_id.startswith('an-'): 154 headers['Cookie'] += 'flashVersion=0; ' 155 156 # Retrieve video webpage to extract further information 157 webpage = self._download_webpage(url, video_id, headers=headers) 158 159 error = get_element_by_attribute( 160 'class', 'notfound-page-title', webpage) 161 if error: 162 raise ExtractorError(error, expected=True) 163 164 video_title = self._html_search_meta( 165 ['og:title', 'twitter:title'], webpage, 'title', default=None) or self._search_regex(r'<h1>(.*?)</h1>', webpage, 'title') 166 167 # Extract URL, uploader and title from webpage 168 self.report_extraction(video_id) 169 video_url = None 170 mobj = re.search(r'(?m)&(?:media|video)URL=([^&]+)', webpage) 171 if mobj is not None: 172 mediaURL = compat_urllib_parse_unquote(mobj.group(1)) 173 video_ext = determine_ext(mediaURL) 174 175 # Extract gdaKey if available 176 mobj = re.search(r'(?m)&gdaKey=(.*?)&', webpage) 177 if mobj is None: 178 video_url = mediaURL 179 else: 180 gdaKey = mobj.group(1) 181 video_url = '%s?__gda__=%s' % (mediaURL, gdaKey) 182 if video_url is None: 183 mobj = re.search(r'<video src="([^"]+)"', webpage) 184 if mobj: 185 video_url = mobj.group(1) 186 video_ext = 'mp4' 187 if video_url is None: 188 flashvars = self._search_regex( 189 r' name="flashvars" value="(.*?)"', webpage, 'flashvars', 190 default=None) 191 if flashvars: 192 vardict = compat_parse_qs(flashvars) 193 if 'mediaData' not in vardict: 194 raise ExtractorError('Unable to extract media URL') 195 mobj = re.search( 196 r'"mediaURL":"(?P<mediaURL>http.*?)",(.*?)"key":"(?P<key>.*?)"', vardict['mediaData'][0]) 197 if mobj is None: 198 raise ExtractorError('Unable to extract media URL') 199 mediaURL = mobj.group('mediaURL').replace('\\/', '/') 200 video_url = '%s?__gda__=%s' % (mediaURL, mobj.group('key')) 201 video_ext = determine_ext(video_url) 202 if video_url is None: 203 player_url = self._search_regex( 204 r"swfobject\.embedSWF\('([^']+)'", 205 webpage, 'config URL', default=None) 206 if player_url: 207 config_url = self._search_regex( 208 r'config=(.+)$', player_url, 'config URL') 209 config_doc = self._download_xml( 210 config_url, video_id, 211 note='Downloading video config') 212 smil_url = config_doc.find('.//properties').attrib['smil_file'] 213 smil_doc = self._download_xml( 214 smil_url, video_id, 215 note='Downloading SMIL document') 216 base_url = smil_doc.find('./head/meta').attrib['base'] 217 video_url = [] 218 for vn in smil_doc.findall('.//video'): 219 br = int(vn.attrib['system-bitrate']) 220 play_path = vn.attrib['src'] 221 video_url.append({ 222 'format_id': 'smil-%d' % br, 223 'url': base_url, 224 'play_path': play_path, 225 'page_url': url, 226 'player_url': player_url, 227 'ext': play_path.partition(':')[0], 228 }) 229 if video_url is None: 230 flashvars = self._parse_json(self._search_regex( 231 r'flashvars\s*=\s*({.*});', webpage, 'flashvars', 232 default=None), video_id, fatal=False) 233 if flashvars: 234 video_url = [] 235 for source in flashvars.get('sources'): 236 source_url = source.get('src') 237 if not source_url: 238 continue 239 ext = mimetype2ext(source.get('type')) or determine_ext(source_url) 240 if ext == 'm3u8': 241 video_url.extend(self._extract_m3u8_formats( 242 source_url, video_id, 'mp4', 243 'm3u8_native', m3u8_id='hls', fatal=False)) 244 else: 245 video_url.append({ 246 'url': source_url, 247 'ext': ext, 248 }) 249 250 if video_url is None: 251 raise ExtractorError('Unsupported video type') 252 253 description = self._html_search_meta( 254 ['og:description', 'twitter:description', 'description'], 255 webpage, 'title', fatal=False) 256 thumbnail = self._html_search_meta( 257 ['og:image', 'twitter:image'], webpage, 'title', fatal=False) 258 video_uploader = self._html_search_regex( 259 r'submitter=(.*?);|googletag\.pubads\(\)\.setTargeting\("(?:channel|submiter)","([^"]+)"\);', 260 webpage, 'uploader nickname', fatal=False) 261 duration = int_or_none( 262 self._html_search_meta('video:duration', webpage, default=None)) 263 age_limit = ( 264 18 265 if re.search(r'(?:"contentRating":|"rating",)"restricted"', webpage) 266 else 0) 267 268 if isinstance(video_url, list): 269 formats = video_url 270 else: 271 formats = [{ 272 'url': video_url, 273 'ext': video_ext, 274 }] 275 self._sort_formats(formats) 276 277 return { 278 'id': video_id, 279 'display_id': display_id, 280 'description': description, 281 'uploader': video_uploader, 282 'title': video_title, 283 'thumbnail': thumbnail, 284 'age_limit': age_limit, 285 'formats': formats, 286 'duration': duration, 287 } 288