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