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