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