]>
Commit | Line | Data |
---|---|---|
e028d0d1 | 1 | #!/usr/bin/env python |
db13c16e | 2 | # coding: utf-8 |
e028d0d1 | 3 | |
89087418 PH |
4 | from __future__ import unicode_literals |
5 | ||
5d254f77 PH |
6 | # Allow direct execution |
7 | import os | |
e028d0d1 JMF |
8 | import sys |
9 | import unittest | |
5d254f77 | 10 | sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
e028d0d1 | 11 | |
0217c783 PH |
12 | import copy |
13 | ||
c57f7757 | 14 | from test.helper import FakeYDL, assertRegexpMatches |
cefecac1 U |
15 | from youtube_dlc import YoutubeDL |
16 | from youtube_dlc.compat import compat_str, compat_urllib_error | |
17 | from youtube_dlc.extractor import YoutubeIE | |
18 | from youtube_dlc.extractor.common import InfoExtractor | |
19 | from youtube_dlc.postprocessor.common import PostProcessor | |
20 | from youtube_dlc.utils import ExtractorError, match_filter_func | |
e028d0d1 | 21 | |
8508557e JMF |
22 | TEST_URL = 'http://localhost/sample.mp4' |
23 | ||
e028d0d1 JMF |
24 | |
25 | class YDL(FakeYDL): | |
f4d96df0 PH |
26 | def __init__(self, *args, **kwargs): |
27 | super(YDL, self).__init__(*args, **kwargs) | |
e028d0d1 | 28 | self.downloaded_info_dicts = [] |
f4d96df0 | 29 | self.msgs = [] |
5d254f77 | 30 | |
e028d0d1 JMF |
31 | def process_info(self, info_dict): |
32 | self.downloaded_info_dicts.append(info_dict) | |
33 | ||
f4d96df0 PH |
34 | def to_screen(self, msg): |
35 | self.msgs.append(msg) | |
36 | ||
5d254f77 | 37 | |
3537b93d PH |
38 | def _make_result(formats, **kwargs): |
39 | res = { | |
40 | 'formats': formats, | |
41 | 'id': 'testid', | |
42 | 'title': 'testttitle', | |
43 | 'extractor': 'testex', | |
0396806f | 44 | 'extractor_key': 'TestEx', |
732044af | 45 | 'webpage_url': 'http://example.com/watch?v=shenanigans', |
3537b93d PH |
46 | } |
47 | res.update(**kwargs) | |
48 | return res | |
49 | ||
50 | ||
e028d0d1 JMF |
51 | class TestFormatSelection(unittest.TestCase): |
52 | def test_prefer_free_formats(self): | |
53 | # Same resolution => download webm | |
54 | ydl = YDL() | |
55 | ydl.params['prefer_free_formats'] = True | |
5d254f77 | 56 | formats = [ |
8508557e JMF |
57 | {'ext': 'webm', 'height': 460, 'url': TEST_URL}, |
58 | {'ext': 'mp4', 'height': 460, 'url': TEST_URL}, | |
5d254f77 | 59 | ] |
3537b93d | 60 | info_dict = _make_result(formats) |
3d4a70b8 PH |
61 | yie = YoutubeIE(ydl) |
62 | yie._sort_formats(info_dict['formats']) | |
e028d0d1 JMF |
63 | ydl.process_ie_result(info_dict) |
64 | downloaded = ydl.downloaded_info_dicts[0] | |
89087418 | 65 | self.assertEqual(downloaded['ext'], 'webm') |
e028d0d1 JMF |
66 | |
67 | # Different resolution => download best quality (mp4) | |
68 | ydl = YDL() | |
69 | ydl.params['prefer_free_formats'] = True | |
5d254f77 | 70 | formats = [ |
8508557e JMF |
71 | {'ext': 'webm', 'height': 720, 'url': TEST_URL}, |
72 | {'ext': 'mp4', 'height': 1080, 'url': TEST_URL}, | |
5d254f77 | 73 | ] |
89087418 | 74 | info_dict['formats'] = formats |
3d4a70b8 PH |
75 | yie = YoutubeIE(ydl) |
76 | yie._sort_formats(info_dict['formats']) | |
e028d0d1 JMF |
77 | ydl.process_ie_result(info_dict) |
78 | downloaded = ydl.downloaded_info_dicts[0] | |
89087418 | 79 | self.assertEqual(downloaded['ext'], 'mp4') |
e028d0d1 | 80 | |
1c783bca | 81 | # No prefer_free_formats => prefer mp4 and flv for greater compatibility |
e028d0d1 JMF |
82 | ydl = YDL() |
83 | ydl.params['prefer_free_formats'] = False | |
5d254f77 | 84 | formats = [ |
8508557e JMF |
85 | {'ext': 'webm', 'height': 720, 'url': TEST_URL}, |
86 | {'ext': 'mp4', 'height': 720, 'url': TEST_URL}, | |
87 | {'ext': 'flv', 'height': 720, 'url': TEST_URL}, | |
5d254f77 | 88 | ] |
89087418 | 89 | info_dict['formats'] = formats |
3d4a70b8 PH |
90 | yie = YoutubeIE(ydl) |
91 | yie._sort_formats(info_dict['formats']) | |
92 | ydl.process_ie_result(info_dict) | |
93 | downloaded = ydl.downloaded_info_dicts[0] | |
89087418 | 94 | self.assertEqual(downloaded['ext'], 'mp4') |
3d4a70b8 PH |
95 | |
96 | ydl = YDL() | |
97 | ydl.params['prefer_free_formats'] = False | |
98 | formats = [ | |
8508557e JMF |
99 | {'ext': 'flv', 'height': 720, 'url': TEST_URL}, |
100 | {'ext': 'webm', 'height': 720, 'url': TEST_URL}, | |
3d4a70b8 | 101 | ] |
89087418 | 102 | info_dict['formats'] = formats |
3d4a70b8 PH |
103 | yie = YoutubeIE(ydl) |
104 | yie._sort_formats(info_dict['formats']) | |
e028d0d1 JMF |
105 | ydl.process_ie_result(info_dict) |
106 | downloaded = ydl.downloaded_info_dicts[0] | |
89087418 | 107 | self.assertEqual(downloaded['ext'], 'flv') |
e028d0d1 | 108 | |
a9c58ad9 JMF |
109 | def test_format_selection(self): |
110 | formats = [ | |
8508557e | 111 | {'format_id': '35', 'ext': 'mp4', 'preference': 1, 'url': TEST_URL}, |
232541df | 112 | {'format_id': 'example-with-dashes', 'ext': 'webm', 'preference': 1, 'url': TEST_URL}, |
8508557e JMF |
113 | {'format_id': '45', 'ext': 'webm', 'preference': 2, 'url': TEST_URL}, |
114 | {'format_id': '47', 'ext': 'webm', 'preference': 3, 'url': TEST_URL}, | |
115 | {'format_id': '2', 'ext': 'flv', 'preference': 4, 'url': TEST_URL}, | |
a9c58ad9 | 116 | ] |
3537b93d | 117 | info_dict = _make_result(formats) |
a9c58ad9 | 118 | |
89087418 | 119 | ydl = YDL({'format': '20/47'}) |
8e3e0322 | 120 | ydl.process_ie_result(info_dict.copy()) |
a9c58ad9 | 121 | downloaded = ydl.downloaded_info_dicts[0] |
89087418 | 122 | self.assertEqual(downloaded['format_id'], '47') |
a9c58ad9 | 123 | |
89087418 | 124 | ydl = YDL({'format': '20/71/worst'}) |
8e3e0322 | 125 | ydl.process_ie_result(info_dict.copy()) |
a9c58ad9 | 126 | downloaded = ydl.downloaded_info_dicts[0] |
89087418 | 127 | self.assertEqual(downloaded['format_id'], '35') |
a9c58ad9 JMF |
128 | |
129 | ydl = YDL() | |
8e3e0322 | 130 | ydl.process_ie_result(info_dict.copy()) |
a9c58ad9 | 131 | downloaded = ydl.downloaded_info_dicts[0] |
89087418 | 132 | self.assertEqual(downloaded['format_id'], '2') |
a9c58ad9 | 133 | |
89087418 | 134 | ydl = YDL({'format': 'webm/mp4'}) |
8e3e0322 | 135 | ydl.process_ie_result(info_dict.copy()) |
49e86983 | 136 | downloaded = ydl.downloaded_info_dicts[0] |
89087418 | 137 | self.assertEqual(downloaded['format_id'], '47') |
49e86983 | 138 | |
89087418 | 139 | ydl = YDL({'format': '3gp/40/mp4'}) |
8e3e0322 | 140 | ydl.process_ie_result(info_dict.copy()) |
49e86983 | 141 | downloaded = ydl.downloaded_info_dicts[0] |
89087418 | 142 | self.assertEqual(downloaded['format_id'], '35') |
49e86983 | 143 | |
232541df JMF |
144 | ydl = YDL({'format': 'example-with-dashes'}) |
145 | ydl.process_ie_result(info_dict.copy()) | |
146 | downloaded = ydl.downloaded_info_dicts[0] | |
147 | self.assertEqual(downloaded['format_id'], 'example-with-dashes') | |
148 | ||
ba7678f9 PH |
149 | def test_format_selection_audio(self): |
150 | formats = [ | |
8508557e JMF |
151 | {'format_id': 'audio-low', 'ext': 'webm', 'preference': 1, 'vcodec': 'none', 'url': TEST_URL}, |
152 | {'format_id': 'audio-mid', 'ext': 'webm', 'preference': 2, 'vcodec': 'none', 'url': TEST_URL}, | |
153 | {'format_id': 'audio-high', 'ext': 'flv', 'preference': 3, 'vcodec': 'none', 'url': TEST_URL}, | |
154 | {'format_id': 'vid', 'ext': 'mp4', 'preference': 4, 'url': TEST_URL}, | |
ba7678f9 | 155 | ] |
3537b93d | 156 | info_dict = _make_result(formats) |
ba7678f9 | 157 | |
89087418 | 158 | ydl = YDL({'format': 'bestaudio'}) |
ba7678f9 PH |
159 | ydl.process_ie_result(info_dict.copy()) |
160 | downloaded = ydl.downloaded_info_dicts[0] | |
89087418 | 161 | self.assertEqual(downloaded['format_id'], 'audio-high') |
ba7678f9 | 162 | |
89087418 | 163 | ydl = YDL({'format': 'worstaudio'}) |
ba7678f9 PH |
164 | ydl.process_ie_result(info_dict.copy()) |
165 | downloaded = ydl.downloaded_info_dicts[0] | |
89087418 | 166 | self.assertEqual(downloaded['format_id'], 'audio-low') |
ba7678f9 PH |
167 | |
168 | formats = [ | |
8508557e JMF |
169 | {'format_id': 'vid-low', 'ext': 'mp4', 'preference': 1, 'url': TEST_URL}, |
170 | {'format_id': 'vid-high', 'ext': 'mp4', 'preference': 2, 'url': TEST_URL}, | |
ba7678f9 | 171 | ] |
3537b93d | 172 | info_dict = _make_result(formats) |
ba7678f9 | 173 | |
89087418 | 174 | ydl = YDL({'format': 'bestaudio/worstaudio/best'}) |
ba7678f9 PH |
175 | ydl.process_ie_result(info_dict.copy()) |
176 | downloaded = ydl.downloaded_info_dicts[0] | |
89087418 | 177 | self.assertEqual(downloaded['format_id'], 'vid-high') |
ba7678f9 | 178 | |
0217c783 PH |
179 | def test_format_selection_audio_exts(self): |
180 | formats = [ | |
181 | {'format_id': 'mp3-64', 'ext': 'mp3', 'abr': 64, 'url': 'http://_', 'vcodec': 'none'}, | |
182 | {'format_id': 'ogg-64', 'ext': 'ogg', 'abr': 64, 'url': 'http://_', 'vcodec': 'none'}, | |
183 | {'format_id': 'aac-64', 'ext': 'aac', 'abr': 64, 'url': 'http://_', 'vcodec': 'none'}, | |
184 | {'format_id': 'mp3-32', 'ext': 'mp3', 'abr': 32, 'url': 'http://_', 'vcodec': 'none'}, | |
185 | {'format_id': 'aac-32', 'ext': 'aac', 'abr': 32, 'url': 'http://_', 'vcodec': 'none'}, | |
186 | ] | |
187 | ||
188 | info_dict = _make_result(formats) | |
189 | ydl = YDL({'format': 'best'}) | |
190 | ie = YoutubeIE(ydl) | |
191 | ie._sort_formats(info_dict['formats']) | |
192 | ydl.process_ie_result(copy.deepcopy(info_dict)) | |
193 | downloaded = ydl.downloaded_info_dicts[0] | |
194 | self.assertEqual(downloaded['format_id'], 'aac-64') | |
195 | ||
196 | ydl = YDL({'format': 'mp3'}) | |
197 | ie = YoutubeIE(ydl) | |
198 | ie._sort_formats(info_dict['formats']) | |
199 | ydl.process_ie_result(copy.deepcopy(info_dict)) | |
200 | downloaded = ydl.downloaded_info_dicts[0] | |
201 | self.assertEqual(downloaded['format_id'], 'mp3-64') | |
202 | ||
203 | ydl = YDL({'prefer_free_formats': True}) | |
204 | ie = YoutubeIE(ydl) | |
205 | ie._sort_formats(info_dict['formats']) | |
206 | ydl.process_ie_result(copy.deepcopy(info_dict)) | |
207 | downloaded = ydl.downloaded_info_dicts[0] | |
208 | self.assertEqual(downloaded['format_id'], 'ogg-64') | |
209 | ||
bc6d5978 JMF |
210 | def test_format_selection_video(self): |
211 | formats = [ | |
8508557e JMF |
212 | {'format_id': 'dash-video-low', 'ext': 'mp4', 'preference': 1, 'acodec': 'none', 'url': TEST_URL}, |
213 | {'format_id': 'dash-video-high', 'ext': 'mp4', 'preference': 2, 'acodec': 'none', 'url': TEST_URL}, | |
214 | {'format_id': 'vid', 'ext': 'mp4', 'preference': 3, 'url': TEST_URL}, | |
bc6d5978 | 215 | ] |
3537b93d | 216 | info_dict = _make_result(formats) |
bc6d5978 JMF |
217 | |
218 | ydl = YDL({'format': 'bestvideo'}) | |
219 | ydl.process_ie_result(info_dict.copy()) | |
220 | downloaded = ydl.downloaded_info_dicts[0] | |
221 | self.assertEqual(downloaded['format_id'], 'dash-video-high') | |
222 | ||
223 | ydl = YDL({'format': 'worstvideo'}) | |
224 | ydl.process_ie_result(info_dict.copy()) | |
225 | downloaded = ydl.downloaded_info_dicts[0] | |
226 | self.assertEqual(downloaded['format_id'], 'dash-video-low') | |
227 | ||
4c3b16d5 S |
228 | ydl = YDL({'format': 'bestvideo[format_id^=dash][format_id$=low]'}) |
229 | ydl.process_ie_result(info_dict.copy()) | |
230 | downloaded = ydl.downloaded_info_dicts[0] | |
231 | self.assertEqual(downloaded['format_id'], 'dash-video-low') | |
232 | ||
b913348d | 233 | formats = [ |
234 | {'format_id': 'vid-vcodec-dot', 'ext': 'mp4', 'preference': 1, 'vcodec': 'avc1.123456', 'acodec': 'none', 'url': TEST_URL}, | |
235 | ] | |
236 | info_dict = _make_result(formats) | |
237 | ||
238 | ydl = YDL({'format': 'bestvideo[vcodec=avc1.123456]'}) | |
239 | ydl.process_ie_result(info_dict.copy()) | |
240 | downloaded = ydl.downloaded_info_dicts[0] | |
241 | self.assertEqual(downloaded['format_id'], 'vid-vcodec-dot') | |
242 | ||
2cc779f4 S |
243 | def test_format_selection_string_ops(self): |
244 | formats = [ | |
245 | {'format_id': 'abc-cba', 'ext': 'mp4', 'url': TEST_URL}, | |
e118a879 | 246 | {'format_id': 'zxc-cxz', 'ext': 'webm', 'url': TEST_URL}, |
2cc779f4 S |
247 | ] |
248 | info_dict = _make_result(formats) | |
249 | ||
250 | # equals (=) | |
251 | ydl = YDL({'format': '[format_id=abc-cba]'}) | |
252 | ydl.process_ie_result(info_dict.copy()) | |
253 | downloaded = ydl.downloaded_info_dicts[0] | |
254 | self.assertEqual(downloaded['format_id'], 'abc-cba') | |
255 | ||
256 | # does not equal (!=) | |
257 | ydl = YDL({'format': '[format_id!=abc-cba]'}) | |
e118a879 S |
258 | ydl.process_ie_result(info_dict.copy()) |
259 | downloaded = ydl.downloaded_info_dicts[0] | |
260 | self.assertEqual(downloaded['format_id'], 'zxc-cxz') | |
261 | ||
262 | ydl = YDL({'format': '[format_id!=abc-cba][format_id!=zxc-cxz]'}) | |
2cc779f4 S |
263 | self.assertRaises(ExtractorError, ydl.process_ie_result, info_dict.copy()) |
264 | ||
265 | # starts with (^=) | |
266 | ydl = YDL({'format': '[format_id^=abc]'}) | |
267 | ydl.process_ie_result(info_dict.copy()) | |
268 | downloaded = ydl.downloaded_info_dicts[0] | |
269 | self.assertEqual(downloaded['format_id'], 'abc-cba') | |
270 | ||
271 | # does not start with (!^=) | |
e118a879 S |
272 | ydl = YDL({'format': '[format_id!^=abc]'}) |
273 | ydl.process_ie_result(info_dict.copy()) | |
274 | downloaded = ydl.downloaded_info_dicts[0] | |
275 | self.assertEqual(downloaded['format_id'], 'zxc-cxz') | |
276 | ||
277 | ydl = YDL({'format': '[format_id!^=abc][format_id!^=zxc]'}) | |
2cc779f4 S |
278 | self.assertRaises(ExtractorError, ydl.process_ie_result, info_dict.copy()) |
279 | ||
280 | # ends with ($=) | |
281 | ydl = YDL({'format': '[format_id$=cba]'}) | |
282 | ydl.process_ie_result(info_dict.copy()) | |
283 | downloaded = ydl.downloaded_info_dicts[0] | |
284 | self.assertEqual(downloaded['format_id'], 'abc-cba') | |
285 | ||
286 | # does not end with (!$=) | |
e118a879 S |
287 | ydl = YDL({'format': '[format_id!$=cba]'}) |
288 | ydl.process_ie_result(info_dict.copy()) | |
289 | downloaded = ydl.downloaded_info_dicts[0] | |
290 | self.assertEqual(downloaded['format_id'], 'zxc-cxz') | |
291 | ||
292 | ydl = YDL({'format': '[format_id!$=cba][format_id!$=cxz]'}) | |
2cc779f4 S |
293 | self.assertRaises(ExtractorError, ydl.process_ie_result, info_dict.copy()) |
294 | ||
295 | # contains (*=) | |
e118a879 | 296 | ydl = YDL({'format': '[format_id*=bc-cb]'}) |
2cc779f4 S |
297 | ydl.process_ie_result(info_dict.copy()) |
298 | downloaded = ydl.downloaded_info_dicts[0] | |
299 | self.assertEqual(downloaded['format_id'], 'abc-cba') | |
300 | ||
301 | # does not contain (!*=) | |
e118a879 S |
302 | ydl = YDL({'format': '[format_id!*=bc-cb]'}) |
303 | ydl.process_ie_result(info_dict.copy()) | |
304 | downloaded = ydl.downloaded_info_dicts[0] | |
305 | self.assertEqual(downloaded['format_id'], 'zxc-cxz') | |
306 | ||
307 | ydl = YDL({'format': '[format_id!*=abc][format_id!*=zxc]'}) | |
308 | self.assertRaises(ExtractorError, ydl.process_ie_result, info_dict.copy()) | |
309 | ||
2cc779f4 S |
310 | ydl = YDL({'format': '[format_id!*=-]'}) |
311 | self.assertRaises(ExtractorError, ydl.process_ie_result, info_dict.copy()) | |
312 | ||
3d4a70b8 PH |
313 | def test_youtube_format_selection(self): |
314 | order = [ | |
86bf2905 | 315 | '38', '37', '46', '22', '45', '35', '44', '18', '34', '43', '6', '5', '17', '36', '13', |
3d4a70b8 PH |
316 | # Apple HTTP Live Streaming |
317 | '96', '95', '94', '93', '92', '132', '151', | |
318 | # 3D | |
319 | '85', '84', '102', '83', '101', '82', '100', | |
320 | # Dash video | |
c11125f9 | 321 | '137', '248', '136', '247', '135', '246', |
3d4a70b8 PH |
322 | '245', '244', '134', '243', '133', '242', '160', |
323 | # Dash audio | |
a053c349 | 324 | '141', '172', '140', '171', '139', |
3d4a70b8 PH |
325 | ] |
326 | ||
67134eab JMF |
327 | def format_info(f_id): |
328 | info = YoutubeIE._formats[f_id].copy() | |
1df41411 | 329 | |
91cb6b50 | 330 | # XXX: In real cases InfoExtractor._parse_mpd_formats() fills up 'acodec' |
1df41411 YCH |
331 | # and 'vcodec', while in tests such information is incomplete since |
332 | # commit a6c2c24479e5f4827ceb06f64d855329c0a6f593 | |
333 | # test_YoutubeDL.test_youtube_format_selection is broken without | |
334 | # this fix | |
335 | if 'acodec' in info and 'vcodec' not in info: | |
336 | info['vcodec'] = 'none' | |
337 | elif 'vcodec' in info and 'acodec' not in info: | |
338 | info['acodec'] = 'none' | |
339 | ||
67134eab JMF |
340 | info['format_id'] = f_id |
341 | info['url'] = 'url:' + f_id | |
342 | return info | |
343 | formats_order = [format_info(f_id) for f_id in order] | |
344 | ||
345 | info_dict = _make_result(list(formats_order), extractor='youtube') | |
346 | ydl = YDL({'format': 'bestvideo+bestaudio'}) | |
347 | yie = YoutubeIE(ydl) | |
348 | yie._sort_formats(info_dict['formats']) | |
349 | ydl.process_ie_result(info_dict) | |
350 | downloaded = ydl.downloaded_info_dicts[0] | |
351 | self.assertEqual(downloaded['format_id'], '137+141') | |
352 | self.assertEqual(downloaded['ext'], 'mp4') | |
3d4a70b8 | 353 | |
cf2ac6df JMF |
354 | info_dict = _make_result(list(formats_order), extractor='youtube') |
355 | ydl = YDL({'format': 'bestvideo[height>=999999]+bestaudio/best'}) | |
356 | yie = YoutubeIE(ydl) | |
357 | yie._sort_formats(info_dict['formats']) | |
358 | ydl.process_ie_result(info_dict) | |
359 | downloaded = ydl.downloaded_info_dicts[0] | |
360 | self.assertEqual(downloaded['format_id'], '38') | |
361 | ||
f5f4a27a JMF |
362 | info_dict = _make_result(list(formats_order), extractor='youtube') |
363 | ydl = YDL({'format': 'bestvideo/best,bestaudio'}) | |
364 | yie = YoutubeIE(ydl) | |
365 | yie._sort_formats(info_dict['formats']) | |
366 | ydl.process_ie_result(info_dict) | |
367 | downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts] | |
368 | self.assertEqual(downloaded_ids, ['137', '141']) | |
369 | ||
0130afb7 JMF |
370 | info_dict = _make_result(list(formats_order), extractor='youtube') |
371 | ydl = YDL({'format': '(bestvideo[ext=mp4],bestvideo[ext=webm])+bestaudio'}) | |
372 | yie = YoutubeIE(ydl) | |
373 | yie._sort_formats(info_dict['formats']) | |
374 | ydl.process_ie_result(info_dict) | |
375 | downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts] | |
376 | self.assertEqual(downloaded_ids, ['137+141', '248+141']) | |
377 | ||
378 | info_dict = _make_result(list(formats_order), extractor='youtube') | |
379 | ydl = YDL({'format': '(bestvideo[ext=mp4],bestvideo[ext=webm])[height<=720]+bestaudio'}) | |
380 | yie = YoutubeIE(ydl) | |
381 | yie._sort_formats(info_dict['formats']) | |
382 | ydl.process_ie_result(info_dict) | |
383 | downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts] | |
384 | self.assertEqual(downloaded_ids, ['136+141', '247+141']) | |
385 | ||
386 | info_dict = _make_result(list(formats_order), extractor='youtube') | |
387 | ydl = YDL({'format': '(bestvideo[ext=none]/bestvideo[ext=webm])+bestaudio'}) | |
388 | yie = YoutubeIE(ydl) | |
389 | yie._sort_formats(info_dict['formats']) | |
390 | ydl.process_ie_result(info_dict) | |
391 | downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts] | |
392 | self.assertEqual(downloaded_ids, ['248+141']) | |
393 | ||
67134eab | 394 | for f1, f2 in zip(formats_order, formats_order[1:]): |
3537b93d | 395 | info_dict = _make_result([f1, f2], extractor='youtube') |
8dd54188 | 396 | ydl = YDL({'format': 'best/bestvideo'}) |
3d4a70b8 PH |
397 | yie = YoutubeIE(ydl) |
398 | yie._sort_formats(info_dict['formats']) | |
399 | ydl.process_ie_result(info_dict) | |
400 | downloaded = ydl.downloaded_info_dicts[0] | |
67134eab | 401 | self.assertEqual(downloaded['format_id'], f1['format_id']) |
3d4a70b8 | 402 | |
3537b93d | 403 | info_dict = _make_result([f2, f1], extractor='youtube') |
8dd54188 | 404 | ydl = YDL({'format': 'best/bestvideo'}) |
3d4a70b8 PH |
405 | yie = YoutubeIE(ydl) |
406 | yie._sort_formats(info_dict['formats']) | |
407 | ydl.process_ie_result(info_dict) | |
408 | downloaded = ydl.downloaded_info_dicts[0] | |
67134eab | 409 | self.assertEqual(downloaded['format_id'], f1['format_id']) |
3d4a70b8 | 410 | |
317f7ab6 S |
411 | def test_audio_only_extractor_format_selection(self): |
412 | # For extractors with incomplete formats (all formats are audio-only or | |
413 | # video-only) best and worst should fallback to corresponding best/worst | |
414 | # video-only or audio-only formats (as per | |
067aa17e | 415 | # https://github.com/ytdl-org/youtube-dl/pull/5556) |
317f7ab6 S |
416 | formats = [ |
417 | {'format_id': 'low', 'ext': 'mp3', 'preference': 1, 'vcodec': 'none', 'url': TEST_URL}, | |
418 | {'format_id': 'high', 'ext': 'mp3', 'preference': 2, 'vcodec': 'none', 'url': TEST_URL}, | |
419 | ] | |
420 | info_dict = _make_result(formats) | |
421 | ||
422 | ydl = YDL({'format': 'best'}) | |
423 | ydl.process_ie_result(info_dict.copy()) | |
424 | downloaded = ydl.downloaded_info_dicts[0] | |
425 | self.assertEqual(downloaded['format_id'], 'high') | |
426 | ||
427 | ydl = YDL({'format': 'worst'}) | |
428 | ydl.process_ie_result(info_dict.copy()) | |
429 | downloaded = ydl.downloaded_info_dicts[0] | |
430 | self.assertEqual(downloaded['format_id'], 'low') | |
431 | ||
432 | def test_format_not_available(self): | |
433 | formats = [ | |
434 | {'format_id': 'regular', 'ext': 'mp4', 'height': 360, 'url': TEST_URL}, | |
435 | {'format_id': 'video', 'ext': 'mp4', 'height': 720, 'acodec': 'none', 'url': TEST_URL}, | |
436 | ] | |
437 | info_dict = _make_result(formats) | |
438 | ||
439 | # This must fail since complete video-audio format does not match filter | |
440 | # and extractor does not provide incomplete only formats (i.e. only | |
441 | # video-only or audio-only). | |
442 | ydl = YDL({'format': 'best[height>360]'}) | |
443 | self.assertRaises(ExtractorError, ydl.process_ie_result, info_dict.copy()) | |
444 | ||
8cda78ef | 445 | def test_format_selection_issue_10083(self): |
067aa17e | 446 | # See https://github.com/ytdl-org/youtube-dl/issues/10083 |
8cda78ef S |
447 | formats = [ |
448 | {'format_id': 'regular', 'height': 360, 'url': TEST_URL}, | |
449 | {'format_id': 'video', 'height': 720, 'acodec': 'none', 'url': TEST_URL}, | |
450 | {'format_id': 'audio', 'vcodec': 'none', 'url': TEST_URL}, | |
451 | ] | |
452 | info_dict = _make_result(formats) | |
453 | ||
454 | ydl = YDL({'format': 'best[height>360]/bestvideo[height>360]+bestaudio'}) | |
455 | ydl.process_ie_result(info_dict.copy()) | |
456 | self.assertEqual(ydl.downloaded_info_dicts[0]['format_id'], 'video+audio') | |
457 | ||
0a31a350 JMF |
458 | def test_invalid_format_specs(self): |
459 | def assert_syntax_error(format_spec): | |
460 | ydl = YDL({'format': format_spec}) | |
461 | info_dict = _make_result([{'format_id': 'foo', 'url': TEST_URL}]) | |
462 | self.assertRaises(SyntaxError, ydl.process_ie_result, info_dict) | |
463 | ||
464 | assert_syntax_error('bestvideo,,best') | |
465 | assert_syntax_error('+bestaudio') | |
466 | assert_syntax_error('bestvideo+') | |
d96d604e | 467 | assert_syntax_error('/') |
0a31a350 | 468 | |
083c9df9 PH |
469 | def test_format_filtering(self): |
470 | formats = [ | |
471 | {'format_id': 'A', 'filesize': 500, 'width': 1000}, | |
472 | {'format_id': 'B', 'filesize': 1000, 'width': 500}, | |
473 | {'format_id': 'C', 'filesize': 1000, 'width': 400}, | |
474 | {'format_id': 'D', 'filesize': 2000, 'width': 600}, | |
475 | {'format_id': 'E', 'filesize': 3000}, | |
476 | {'format_id': 'F'}, | |
477 | {'format_id': 'G', 'filesize': 1000000}, | |
478 | ] | |
479 | for f in formats: | |
480 | f['url'] = 'http://_/' | |
481 | f['ext'] = 'unknown' | |
482 | info_dict = _make_result(formats) | |
483 | ||
484 | ydl = YDL({'format': 'best[filesize<3000]'}) | |
485 | ydl.process_ie_result(info_dict) | |
486 | downloaded = ydl.downloaded_info_dicts[0] | |
487 | self.assertEqual(downloaded['format_id'], 'D') | |
488 | ||
489 | ydl = YDL({'format': 'best[filesize<=3000]'}) | |
490 | ydl.process_ie_result(info_dict) | |
491 | downloaded = ydl.downloaded_info_dicts[0] | |
492 | self.assertEqual(downloaded['format_id'], 'E') | |
493 | ||
494 | ydl = YDL({'format': 'best[filesize <= ? 3000]'}) | |
495 | ydl.process_ie_result(info_dict) | |
496 | downloaded = ydl.downloaded_info_dicts[0] | |
497 | self.assertEqual(downloaded['format_id'], 'F') | |
498 | ||
499 | ydl = YDL({'format': 'best [filesize = 1000] [width>450]'}) | |
500 | ydl.process_ie_result(info_dict) | |
501 | downloaded = ydl.downloaded_info_dicts[0] | |
502 | self.assertEqual(downloaded['format_id'], 'B') | |
503 | ||
504 | ydl = YDL({'format': 'best [filesize = 1000] [width!=450]'}) | |
505 | ydl.process_ie_result(info_dict) | |
506 | downloaded = ydl.downloaded_info_dicts[0] | |
507 | self.assertEqual(downloaded['format_id'], 'C') | |
508 | ||
509 | ydl = YDL({'format': '[filesize>?1]'}) | |
510 | ydl.process_ie_result(info_dict) | |
511 | downloaded = ydl.downloaded_info_dicts[0] | |
512 | self.assertEqual(downloaded['format_id'], 'G') | |
513 | ||
514 | ydl = YDL({'format': '[filesize<1M]'}) | |
515 | ydl.process_ie_result(info_dict) | |
516 | downloaded = ydl.downloaded_info_dicts[0] | |
517 | self.assertEqual(downloaded['format_id'], 'E') | |
518 | ||
519 | ydl = YDL({'format': '[filesize<1MiB]'}) | |
520 | ydl.process_ie_result(info_dict) | |
521 | downloaded = ydl.downloaded_info_dicts[0] | |
522 | self.assertEqual(downloaded['format_id'], 'G') | |
523 | ||
5acfa126 JMF |
524 | ydl = YDL({'format': 'all[width>=400][width<=600]'}) |
525 | ydl.process_ie_result(info_dict) | |
526 | downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts] | |
527 | self.assertEqual(downloaded_ids, ['B', 'C', 'D']) | |
528 | ||
bb8e5536 JMF |
529 | ydl = YDL({'format': 'best[height<40]'}) |
530 | try: | |
531 | ydl.process_ie_result(info_dict) | |
532 | except ExtractorError: | |
533 | pass | |
534 | self.assertEqual(ydl.downloaded_info_dicts, []) | |
535 | ||
0017d9ad S |
536 | def test_default_format_spec(self): |
537 | ydl = YDL({'simulate': True}) | |
538 | self.assertEqual(ydl._default_format_spec({}), 'bestvideo+bestaudio/best') | |
539 | ||
d08dcd2d S |
540 | ydl = YDL({}) |
541 | self.assertEqual(ydl._default_format_spec({'is_live': True}), 'best/bestvideo+bestaudio') | |
af0f7428 | 542 | |
d08dcd2d S |
543 | ydl = YDL({'simulate': True}) |
544 | self.assertEqual(ydl._default_format_spec({'is_live': True}), 'bestvideo+bestaudio/best') | |
af0f7428 | 545 | |
0017d9ad | 546 | ydl = YDL({'outtmpl': '-'}) |
af0f7428 | 547 | self.assertEqual(ydl._default_format_spec({}), 'best/bestvideo+bestaudio') |
0017d9ad S |
548 | |
549 | ydl = YDL({}) | |
550 | self.assertEqual(ydl._default_format_spec({}, download=False), 'bestvideo+bestaudio/best') | |
af0f7428 | 551 | self.assertEqual(ydl._default_format_spec({'is_live': True}), 'best/bestvideo+bestaudio') |
0017d9ad | 552 | |
f20bf146 JMF |
553 | |
554 | class TestYoutubeDL(unittest.TestCase): | |
ab84349b JMF |
555 | def test_subtitles(self): |
556 | def s_formats(lang, autocaption=False): | |
557 | return [{ | |
558 | 'ext': ext, | |
559 | 'url': 'http://localhost/video.%s.%s' % (lang, ext), | |
560 | '_auto': autocaption, | |
561 | } for ext in ['vtt', 'srt', 'ass']] | |
562 | subtitles = dict((l, s_formats(l)) for l in ['en', 'fr', 'es']) | |
563 | auto_captions = dict((l, s_formats(l, True)) for l in ['it', 'pt', 'es']) | |
564 | info_dict = { | |
565 | 'id': 'test', | |
566 | 'title': 'Test', | |
567 | 'url': 'http://localhost/video.mp4', | |
568 | 'subtitles': subtitles, | |
569 | 'automatic_captions': auto_captions, | |
570 | 'extractor': 'TEST', | |
732044af | 571 | 'webpage_url': 'http://example.com/watch?v=shenanigans', |
ab84349b JMF |
572 | } |
573 | ||
574 | def get_info(params={}): | |
575 | params.setdefault('simulate', True) | |
576 | ydl = YDL(params) | |
577 | ydl.report_warning = lambda *args, **kargs: None | |
578 | return ydl.process_video_result(info_dict, download=False) | |
579 | ||
580 | result = get_info() | |
581 | self.assertFalse(result.get('requested_subtitles')) | |
582 | self.assertEqual(result['subtitles'], subtitles) | |
583 | self.assertEqual(result['automatic_captions'], auto_captions) | |
584 | ||
585 | result = get_info({'writesubtitles': True}) | |
586 | subs = result['requested_subtitles'] | |
587 | self.assertTrue(subs) | |
588 | self.assertEqual(set(subs.keys()), set(['en'])) | |
589 | self.assertTrue(subs['en'].get('data') is None) | |
590 | self.assertEqual(subs['en']['ext'], 'ass') | |
591 | ||
592 | result = get_info({'writesubtitles': True, 'subtitlesformat': 'foo/srt'}) | |
593 | subs = result['requested_subtitles'] | |
594 | self.assertEqual(subs['en']['ext'], 'srt') | |
595 | ||
596 | result = get_info({'writesubtitles': True, 'subtitleslangs': ['es', 'fr', 'it']}) | |
597 | subs = result['requested_subtitles'] | |
598 | self.assertTrue(subs) | |
599 | self.assertEqual(set(subs.keys()), set(['es', 'fr'])) | |
600 | ||
601 | result = get_info({'writesubtitles': True, 'writeautomaticsub': True, 'subtitleslangs': ['es', 'pt']}) | |
602 | subs = result['requested_subtitles'] | |
603 | self.assertTrue(subs) | |
604 | self.assertEqual(set(subs.keys()), set(['es', 'pt'])) | |
605 | self.assertFalse(subs['es']['_auto']) | |
606 | self.assertTrue(subs['pt']['_auto']) | |
607 | ||
98c70d6f JMF |
608 | result = get_info({'writeautomaticsub': True, 'subtitleslangs': ['es', 'pt']}) |
609 | subs = result['requested_subtitles'] | |
610 | self.assertTrue(subs) | |
611 | self.assertEqual(set(subs.keys()), set(['es', 'pt'])) | |
612 | self.assertTrue(subs['es']['_auto']) | |
613 | self.assertTrue(subs['pt']['_auto']) | |
614 | ||
b6c45014 JMF |
615 | def test_add_extra_info(self): |
616 | test_dict = { | |
617 | 'extractor': 'Foo', | |
618 | } | |
619 | extra_info = { | |
620 | 'extractor': 'Bar', | |
621 | 'playlist': 'funny videos', | |
622 | } | |
623 | YDL.add_extra_info(test_dict, extra_info) | |
624 | self.assertEqual(test_dict['extractor'], 'Foo') | |
625 | self.assertEqual(test_dict['playlist'], 'funny videos') | |
626 | ||
26e63931 JMF |
627 | def test_prepare_filename(self): |
628 | info = { | |
89087418 PH |
629 | 'id': '1234', |
630 | 'ext': 'mp4', | |
631 | 'width': None, | |
d0d9ade4 | 632 | 'height': 1080, |
15da37c7 S |
633 | 'title1': '$PATH', |
634 | 'title2': '%PATH%', | |
26e63931 | 635 | } |
5f6a1245 | 636 | |
26e63931 JMF |
637 | def fname(templ): |
638 | ydl = YoutubeDL({'outtmpl': templ}) | |
639 | return ydl.prepare_filename(info) | |
89087418 PH |
640 | self.assertEqual(fname('%(id)s.%(ext)s'), '1234.mp4') |
641 | self.assertEqual(fname('%(id)s-%(width)s.%(ext)s'), '1234-NA.mp4') | |
26e63931 | 642 | # Replace missing fields with 'NA' |
89087418 | 643 | self.assertEqual(fname('%(uploader_date)s-%(id)s.%(ext)s'), 'NA-1234.mp4') |
d0d9ade4 S |
644 | self.assertEqual(fname('%(height)d.%(ext)s'), '1080.mp4') |
645 | self.assertEqual(fname('%(height)6d.%(ext)s'), ' 1080.mp4') | |
646 | self.assertEqual(fname('%(height)-6d.%(ext)s'), '1080 .mp4') | |
647 | self.assertEqual(fname('%(height)06d.%(ext)s'), '001080.mp4') | |
648 | self.assertEqual(fname('%(height) 06d.%(ext)s'), ' 01080.mp4') | |
649 | self.assertEqual(fname('%(height) 06d.%(ext)s'), ' 01080.mp4') | |
650 | self.assertEqual(fname('%(height)0 6d.%(ext)s'), ' 01080.mp4') | |
651 | self.assertEqual(fname('%(height)0 6d.%(ext)s'), ' 01080.mp4') | |
652 | self.assertEqual(fname('%(height) 0 6d.%(ext)s'), ' 01080.mp4') | |
15da37c7 S |
653 | self.assertEqual(fname('%%'), '%') |
654 | self.assertEqual(fname('%%%%'), '%%') | |
d0d9ade4 S |
655 | self.assertEqual(fname('%%(height)06d.%(ext)s'), '%(height)06d.mp4') |
656 | self.assertEqual(fname('%(width)06d.%(ext)s'), 'NA.mp4') | |
657 | self.assertEqual(fname('%(width)06d.%%(ext)s'), 'NA.%(ext)s') | |
658 | self.assertEqual(fname('%%(width)06d.%(ext)s'), '%(width)06d.mp4') | |
15da37c7 S |
659 | self.assertEqual(fname('Hello %(title1)s'), 'Hello $PATH') |
660 | self.assertEqual(fname('Hello %(title2)s'), 'Hello %PATH%') | |
26e63931 | 661 | |
c57f7757 PH |
662 | def test_format_note(self): |
663 | ydl = YoutubeDL() | |
664 | self.assertEqual(ydl._format_note({}), '') | |
665 | assertRegexpMatches(self, ydl._format_note({ | |
666 | 'vbr': 10, | |
398dea32 | 667 | }), r'^\s*10k$') |
5d583bdf S |
668 | assertRegexpMatches(self, ydl._format_note({ |
669 | 'fps': 30, | |
398dea32 | 670 | }), r'^30fps$') |
5d583bdf | 671 | |
2b4ecde2 JMF |
672 | def test_postprocessors(self): |
673 | filename = 'post-processor-testfile.mp4' | |
674 | audiofile = filename + '.mp3' | |
675 | ||
676 | class SimplePP(PostProcessor): | |
677 | def run(self, info): | |
2b4ecde2 JMF |
678 | with open(audiofile, 'wt') as f: |
679 | f.write('EXAMPLE') | |
592e97e8 | 680 | return [info['filepath']], info |
2b4ecde2 | 681 | |
592e97e8 | 682 | def run_pp(params, PP): |
2b4ecde2 JMF |
683 | with open(filename, 'wt') as f: |
684 | f.write('EXAMPLE') | |
685 | ydl = YoutubeDL(params) | |
592e97e8 | 686 | ydl.add_post_processor(PP()) |
2b4ecde2 JMF |
687 | ydl.post_process(filename, {'filepath': filename}) |
688 | ||
592e97e8 | 689 | run_pp({'keepvideo': True}, SimplePP) |
2b4ecde2 JMF |
690 | self.assertTrue(os.path.exists(filename), '%s doesn\'t exist' % filename) |
691 | self.assertTrue(os.path.exists(audiofile), '%s doesn\'t exist' % audiofile) | |
692 | os.unlink(filename) | |
693 | os.unlink(audiofile) | |
694 | ||
592e97e8 | 695 | run_pp({'keepvideo': False}, SimplePP) |
2b4ecde2 JMF |
696 | self.assertFalse(os.path.exists(filename), '%s exists' % filename) |
697 | self.assertTrue(os.path.exists(audiofile), '%s doesn\'t exist' % audiofile) | |
698 | os.unlink(audiofile) | |
699 | ||
592e97e8 JMF |
700 | class ModifierPP(PostProcessor): |
701 | def run(self, info): | |
702 | with open(info['filepath'], 'wt') as f: | |
703 | f.write('MODIFIED') | |
704 | return [], info | |
705 | ||
706 | run_pp({'keepvideo': False}, ModifierPP) | |
707 | self.assertTrue(os.path.exists(filename), '%s doesn\'t exist' % filename) | |
708 | os.unlink(filename) | |
709 | ||
531980d8 JMF |
710 | def test_match_filter(self): |
711 | class FilterYDL(YDL): | |
712 | def __init__(self, *args, **kwargs): | |
713 | super(FilterYDL, self).__init__(*args, **kwargs) | |
714 | self.params['simulate'] = True | |
715 | ||
716 | def process_info(self, info_dict): | |
717 | super(YDL, self).process_info(info_dict) | |
718 | ||
719 | def _match_entry(self, info_dict, incomplete): | |
720 | res = super(FilterYDL, self)._match_entry(info_dict, incomplete) | |
721 | if res is None: | |
722 | self.downloaded_info_dicts.append(info_dict) | |
723 | return res | |
724 | ||
725 | first = { | |
726 | 'id': '1', | |
727 | 'url': TEST_URL, | |
728 | 'title': 'one', | |
729 | 'extractor': 'TEST', | |
730 | 'duration': 30, | |
731 | 'filesize': 10 * 1024, | |
e5a088dc | 732 | 'playlist_id': '42', |
db13c16e S |
733 | 'uploader': "變態妍字幕版 太妍 тест", |
734 | 'creator': "тест ' 123 ' тест--", | |
732044af | 735 | 'webpage_url': 'http://example.com/watch?v=shenanigans', |
531980d8 JMF |
736 | } |
737 | second = { | |
738 | 'id': '2', | |
739 | 'url': TEST_URL, | |
740 | 'title': 'two', | |
741 | 'extractor': 'TEST', | |
742 | 'duration': 10, | |
743 | 'description': 'foo', | |
744 | 'filesize': 5 * 1024, | |
e5a088dc | 745 | 'playlist_id': '43', |
db13c16e | 746 | 'uploader': "тест 123", |
732044af | 747 | 'webpage_url': 'http://example.com/watch?v=SHENANIGANS', |
531980d8 JMF |
748 | } |
749 | videos = [first, second] | |
750 | ||
751 | def get_videos(filter_=None): | |
752 | ydl = FilterYDL({'match_filter': filter_}) | |
753 | for v in videos: | |
754 | ydl.process_ie_result(v, download=True) | |
755 | return [v['id'] for v in ydl.downloaded_info_dicts] | |
756 | ||
757 | res = get_videos() | |
758 | self.assertEqual(res, ['1', '2']) | |
759 | ||
760 | def f(v): | |
761 | if v['id'] == '1': | |
762 | return None | |
763 | else: | |
764 | return 'Video id is not 1' | |
765 | res = get_videos(f) | |
766 | self.assertEqual(res, ['1']) | |
767 | ||
768 | f = match_filter_func('duration < 30') | |
769 | res = get_videos(f) | |
770 | self.assertEqual(res, ['2']) | |
771 | ||
772 | f = match_filter_func('description = foo') | |
773 | res = get_videos(f) | |
774 | self.assertEqual(res, ['2']) | |
775 | ||
776 | f = match_filter_func('description =? foo') | |
777 | res = get_videos(f) | |
778 | self.assertEqual(res, ['1', '2']) | |
779 | ||
780 | f = match_filter_func('filesize > 5KiB') | |
781 | res = get_videos(f) | |
782 | self.assertEqual(res, ['1']) | |
783 | ||
e5a088dc S |
784 | f = match_filter_func('playlist_id = 42') |
785 | res = get_videos(f) | |
786 | self.assertEqual(res, ['1']) | |
787 | ||
db13c16e S |
788 | f = match_filter_func('uploader = "變態妍字幕版 太妍 тест"') |
789 | res = get_videos(f) | |
790 | self.assertEqual(res, ['1']) | |
791 | ||
792 | f = match_filter_func('uploader != "變態妍字幕版 太妍 тест"') | |
793 | res = get_videos(f) | |
794 | self.assertEqual(res, ['2']) | |
795 | ||
796 | f = match_filter_func('creator = "тест \' 123 \' тест--"') | |
797 | res = get_videos(f) | |
798 | self.assertEqual(res, ['1']) | |
799 | ||
800 | f = match_filter_func("creator = 'тест \\' 123 \\' тест--'") | |
801 | res = get_videos(f) | |
802 | self.assertEqual(res, ['1']) | |
803 | ||
804 | f = match_filter_func(r"creator = 'тест \' 123 \' тест--' & duration > 30") | |
805 | res = get_videos(f) | |
806 | self.assertEqual(res, []) | |
807 | ||
e9eaf3fb JMF |
808 | def test_playlist_items_selection(self): |
809 | entries = [{ | |
810 | 'id': compat_str(i), | |
811 | 'title': compat_str(i), | |
812 | 'url': TEST_URL, | |
813 | } for i in range(1, 5)] | |
814 | playlist = { | |
815 | '_type': 'playlist', | |
816 | 'id': 'test', | |
817 | 'entries': entries, | |
818 | 'extractor': 'test:playlist', | |
819 | 'extractor_key': 'test:playlist', | |
820 | 'webpage_url': 'http://example.com', | |
821 | } | |
822 | ||
4e9e1e24 | 823 | def get_downloaded_info_dicts(params): |
e9eaf3fb | 824 | ydl = YDL(params) |
e0abaab2 S |
825 | # make a deep copy because the dictionary and nested entries |
826 | # can be modified | |
827 | ydl.process_ie_result(copy.deepcopy(playlist)) | |
4e9e1e24 S |
828 | return ydl.downloaded_info_dicts |
829 | ||
830 | def get_ids(params): | |
831 | return [int(v['id']) for v in get_downloaded_info_dicts(params)] | |
e9eaf3fb JMF |
832 | |
833 | result = get_ids({}) | |
834 | self.assertEqual(result, [1, 2, 3, 4]) | |
835 | ||
836 | result = get_ids({'playlistend': 10}) | |
837 | self.assertEqual(result, [1, 2, 3, 4]) | |
838 | ||
839 | result = get_ids({'playlistend': 2}) | |
840 | self.assertEqual(result, [1, 2]) | |
841 | ||
842 | result = get_ids({'playliststart': 10}) | |
843 | self.assertEqual(result, []) | |
844 | ||
845 | result = get_ids({'playliststart': 2}) | |
846 | self.assertEqual(result, [2, 3, 4]) | |
847 | ||
848 | result = get_ids({'playlist_items': '2-4'}) | |
849 | self.assertEqual(result, [2, 3, 4]) | |
850 | ||
851 | result = get_ids({'playlist_items': '2,4'}) | |
852 | self.assertEqual(result, [2, 4]) | |
853 | ||
854 | result = get_ids({'playlist_items': '10'}) | |
855 | self.assertEqual(result, []) | |
856 | ||
86a15ed6 S |
857 | result = get_ids({'playlist_items': '3-10'}) |
858 | self.assertEqual(result, [3, 4]) | |
859 | ||
cd6fc19e S |
860 | result = get_ids({'playlist_items': '2-4,3-4,3'}) |
861 | self.assertEqual(result, [2, 3, 4]) | |
862 | ||
4e9e1e24 S |
863 | # Tests for https://github.com/ytdl-org/youtube-dl/issues/10591 |
864 | # @{ | |
865 | result = get_downloaded_info_dicts({'playlist_items': '2-4,3-4,3'}) | |
866 | self.assertEqual(result[0]['playlist_index'], 2) | |
867 | self.assertEqual(result[1]['playlist_index'], 3) | |
868 | ||
869 | result = get_downloaded_info_dicts({'playlist_items': '2-4,3-4,3'}) | |
870 | self.assertEqual(result[0]['playlist_index'], 2) | |
871 | self.assertEqual(result[1]['playlist_index'], 3) | |
872 | self.assertEqual(result[2]['playlist_index'], 4) | |
873 | ||
874 | result = get_downloaded_info_dicts({'playlist_items': '4,2'}) | |
875 | self.assertEqual(result[0]['playlist_index'], 4) | |
876 | self.assertEqual(result[1]['playlist_index'], 2) | |
877 | # @} | |
878 | ||
e37afbe0 | 879 | def test_urlopen_no_file_protocol(self): |
067aa17e | 880 | # see https://github.com/ytdl-org/youtube-dl/issues/8227 |
e37afbe0 JMF |
881 | ydl = YDL() |
882 | self.assertRaises(compat_urllib_error.URLError, ydl.urlopen, 'file:///etc/passwd') | |
883 | ||
b286f201 YCH |
884 | def test_do_not_override_ie_key_in_url_transparent(self): |
885 | ydl = YDL() | |
886 | ||
887 | class Foo1IE(InfoExtractor): | |
888 | _VALID_URL = r'foo1:' | |
889 | ||
890 | def _real_extract(self, url): | |
891 | return { | |
892 | '_type': 'url_transparent', | |
893 | 'url': 'foo2:', | |
894 | 'ie_key': 'Foo2', | |
0396806f S |
895 | 'title': 'foo1 title', |
896 | 'id': 'foo1_id', | |
b286f201 YCH |
897 | } |
898 | ||
899 | class Foo2IE(InfoExtractor): | |
900 | _VALID_URL = r'foo2:' | |
901 | ||
902 | def _real_extract(self, url): | |
903 | return { | |
904 | '_type': 'url', | |
905 | 'url': 'foo3:', | |
906 | 'ie_key': 'Foo3', | |
907 | } | |
908 | ||
909 | class Foo3IE(InfoExtractor): | |
910 | _VALID_URL = r'foo3:' | |
911 | ||
912 | def _real_extract(self, url): | |
51350db5 | 913 | return _make_result([{'url': TEST_URL}], title='foo3 title') |
b286f201 YCH |
914 | |
915 | ydl.add_info_extractor(Foo1IE(ydl)) | |
916 | ydl.add_info_extractor(Foo2IE(ydl)) | |
917 | ydl.add_info_extractor(Foo3IE(ydl)) | |
918 | ydl.extract_info('foo1:') | |
919 | downloaded = ydl.downloaded_info_dicts[0] | |
920 | self.assertEqual(downloaded['url'], TEST_URL) | |
51350db5 | 921 | self.assertEqual(downloaded['title'], 'foo1 title') |
0396806f S |
922 | self.assertEqual(downloaded['id'], 'testid') |
923 | self.assertEqual(downloaded['extractor'], 'testex') | |
924 | self.assertEqual(downloaded['extractor_key'], 'TestEx') | |
b286f201 | 925 | |
a0566bbf | 926 | # Test case for https://github.com/ytdl-org/youtube-dl/issues/27064 |
927 | def test_ignoreerrors_for_playlist_with_url_transparent_iterable_entries(self): | |
928 | ||
929 | class _YDL(YDL): | |
930 | def __init__(self, *args, **kwargs): | |
931 | super(_YDL, self).__init__(*args, **kwargs) | |
932 | ||
933 | def trouble(self, s, tb=None): | |
934 | pass | |
935 | ||
936 | ydl = _YDL({ | |
937 | 'format': 'extra', | |
938 | 'ignoreerrors': True, | |
939 | }) | |
940 | ||
941 | class VideoIE(InfoExtractor): | |
942 | _VALID_URL = r'video:(?P<id>\d+)' | |
943 | ||
944 | def _real_extract(self, url): | |
945 | video_id = self._match_id(url) | |
946 | formats = [{ | |
947 | 'format_id': 'default', | |
948 | 'url': 'url:', | |
949 | }] | |
950 | if video_id == '0': | |
951 | raise ExtractorError('foo') | |
952 | if video_id == '2': | |
953 | formats.append({ | |
954 | 'format_id': 'extra', | |
955 | 'url': TEST_URL, | |
956 | }) | |
957 | return { | |
958 | 'id': video_id, | |
959 | 'title': 'Video %s' % video_id, | |
960 | 'formats': formats, | |
961 | } | |
962 | ||
963 | class PlaylistIE(InfoExtractor): | |
964 | _VALID_URL = r'playlist:' | |
965 | ||
966 | def _entries(self): | |
967 | for n in range(3): | |
968 | video_id = compat_str(n) | |
969 | yield { | |
970 | '_type': 'url_transparent', | |
971 | 'ie_key': VideoIE.ie_key(), | |
972 | 'id': video_id, | |
973 | 'url': 'video:%s' % video_id, | |
974 | 'title': 'Video Transparent %s' % video_id, | |
975 | } | |
976 | ||
977 | def _real_extract(self, url): | |
978 | return self.playlist_result(self._entries()) | |
979 | ||
980 | ydl.add_info_extractor(VideoIE(ydl)) | |
981 | ydl.add_info_extractor(PlaylistIE(ydl)) | |
982 | info = ydl.extract_info('playlist:') | |
983 | entries = info['entries'] | |
984 | self.assertEqual(len(entries), 3) | |
985 | self.assertTrue(entries[0] is None) | |
986 | self.assertTrue(entries[1] is None) | |
987 | self.assertEqual(len(ydl.downloaded_info_dicts), 1) | |
988 | downloaded = ydl.downloaded_info_dicts[0] | |
989 | self.assertEqual(entries[2], downloaded) | |
990 | self.assertEqual(downloaded['url'], TEST_URL) | |
991 | self.assertEqual(downloaded['title'], 'Video Transparent 2') | |
992 | self.assertEqual(downloaded['id'], '2') | |
993 | self.assertEqual(downloaded['extractor'], 'Video') | |
994 | self.assertEqual(downloaded['extractor_key'], 'Video') | |
995 | ||
2b4ecde2 | 996 | |
e028d0d1 JMF |
997 | if __name__ == '__main__': |
998 | unittest.main() |