]>
Commit | Line | Data |
---|---|---|
1 | #!/usr/bin/env python | |
2 | ||
3 | from __future__ import unicode_literals | |
4 | ||
5 | # Allow direct execution | |
6 | import os | |
7 | import sys | |
8 | import unittest | |
9 | sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
10 | ||
11 | import copy | |
12 | ||
13 | from test.helper import FakeYDL, assertRegexpMatches | |
14 | from youtube_dl import YoutubeDL | |
15 | from youtube_dl.compat import compat_str | |
16 | from youtube_dl.extractor import YoutubeIE | |
17 | from youtube_dl.postprocessor.common import PostProcessor | |
18 | from youtube_dl.utils import match_filter_func | |
19 | ||
20 | TEST_URL = 'http://localhost/sample.mp4' | |
21 | ||
22 | ||
23 | class YDL(FakeYDL): | |
24 | def __init__(self, *args, **kwargs): | |
25 | super(YDL, self).__init__(*args, **kwargs) | |
26 | self.downloaded_info_dicts = [] | |
27 | self.msgs = [] | |
28 | ||
29 | def process_info(self, info_dict): | |
30 | self.downloaded_info_dicts.append(info_dict) | |
31 | ||
32 | def to_screen(self, msg): | |
33 | self.msgs.append(msg) | |
34 | ||
35 | ||
36 | def _make_result(formats, **kwargs): | |
37 | res = { | |
38 | 'formats': formats, | |
39 | 'id': 'testid', | |
40 | 'title': 'testttitle', | |
41 | 'extractor': 'testex', | |
42 | } | |
43 | res.update(**kwargs) | |
44 | return res | |
45 | ||
46 | ||
47 | class TestFormatSelection(unittest.TestCase): | |
48 | def test_prefer_free_formats(self): | |
49 | # Same resolution => download webm | |
50 | ydl = YDL() | |
51 | ydl.params['prefer_free_formats'] = True | |
52 | formats = [ | |
53 | {'ext': 'webm', 'height': 460, 'url': TEST_URL}, | |
54 | {'ext': 'mp4', 'height': 460, 'url': TEST_URL}, | |
55 | ] | |
56 | info_dict = _make_result(formats) | |
57 | yie = YoutubeIE(ydl) | |
58 | yie._sort_formats(info_dict['formats']) | |
59 | ydl.process_ie_result(info_dict) | |
60 | downloaded = ydl.downloaded_info_dicts[0] | |
61 | self.assertEqual(downloaded['ext'], 'webm') | |
62 | ||
63 | # Different resolution => download best quality (mp4) | |
64 | ydl = YDL() | |
65 | ydl.params['prefer_free_formats'] = True | |
66 | formats = [ | |
67 | {'ext': 'webm', 'height': 720, 'url': TEST_URL}, | |
68 | {'ext': 'mp4', 'height': 1080, 'url': TEST_URL}, | |
69 | ] | |
70 | info_dict['formats'] = formats | |
71 | yie = YoutubeIE(ydl) | |
72 | yie._sort_formats(info_dict['formats']) | |
73 | ydl.process_ie_result(info_dict) | |
74 | downloaded = ydl.downloaded_info_dicts[0] | |
75 | self.assertEqual(downloaded['ext'], 'mp4') | |
76 | ||
77 | # No prefer_free_formats => prefer mp4 and flv for greater compatibility | |
78 | ydl = YDL() | |
79 | ydl.params['prefer_free_formats'] = False | |
80 | formats = [ | |
81 | {'ext': 'webm', 'height': 720, 'url': TEST_URL}, | |
82 | {'ext': 'mp4', 'height': 720, 'url': TEST_URL}, | |
83 | {'ext': 'flv', 'height': 720, 'url': TEST_URL}, | |
84 | ] | |
85 | info_dict['formats'] = formats | |
86 | yie = YoutubeIE(ydl) | |
87 | yie._sort_formats(info_dict['formats']) | |
88 | ydl.process_ie_result(info_dict) | |
89 | downloaded = ydl.downloaded_info_dicts[0] | |
90 | self.assertEqual(downloaded['ext'], 'mp4') | |
91 | ||
92 | ydl = YDL() | |
93 | ydl.params['prefer_free_formats'] = False | |
94 | formats = [ | |
95 | {'ext': 'flv', 'height': 720, 'url': TEST_URL}, | |
96 | {'ext': 'webm', 'height': 720, 'url': TEST_URL}, | |
97 | ] | |
98 | info_dict['formats'] = formats | |
99 | yie = YoutubeIE(ydl) | |
100 | yie._sort_formats(info_dict['formats']) | |
101 | ydl.process_ie_result(info_dict) | |
102 | downloaded = ydl.downloaded_info_dicts[0] | |
103 | self.assertEqual(downloaded['ext'], 'flv') | |
104 | ||
105 | def test_format_selection(self): | |
106 | formats = [ | |
107 | {'format_id': '35', 'ext': 'mp4', 'preference': 1, 'url': TEST_URL}, | |
108 | {'format_id': '45', 'ext': 'webm', 'preference': 2, 'url': TEST_URL}, | |
109 | {'format_id': '47', 'ext': 'webm', 'preference': 3, 'url': TEST_URL}, | |
110 | {'format_id': '2', 'ext': 'flv', 'preference': 4, 'url': TEST_URL}, | |
111 | ] | |
112 | info_dict = _make_result(formats) | |
113 | ||
114 | ydl = YDL({'format': '20/47'}) | |
115 | ydl.process_ie_result(info_dict.copy()) | |
116 | downloaded = ydl.downloaded_info_dicts[0] | |
117 | self.assertEqual(downloaded['format_id'], '47') | |
118 | ||
119 | ydl = YDL({'format': '20/71/worst'}) | |
120 | ydl.process_ie_result(info_dict.copy()) | |
121 | downloaded = ydl.downloaded_info_dicts[0] | |
122 | self.assertEqual(downloaded['format_id'], '35') | |
123 | ||
124 | ydl = YDL() | |
125 | ydl.process_ie_result(info_dict.copy()) | |
126 | downloaded = ydl.downloaded_info_dicts[0] | |
127 | self.assertEqual(downloaded['format_id'], '2') | |
128 | ||
129 | ydl = YDL({'format': 'webm/mp4'}) | |
130 | ydl.process_ie_result(info_dict.copy()) | |
131 | downloaded = ydl.downloaded_info_dicts[0] | |
132 | self.assertEqual(downloaded['format_id'], '47') | |
133 | ||
134 | ydl = YDL({'format': '3gp/40/mp4'}) | |
135 | ydl.process_ie_result(info_dict.copy()) | |
136 | downloaded = ydl.downloaded_info_dicts[0] | |
137 | self.assertEqual(downloaded['format_id'], '35') | |
138 | ||
139 | def test_format_selection_audio(self): | |
140 | formats = [ | |
141 | {'format_id': 'audio-low', 'ext': 'webm', 'preference': 1, 'vcodec': 'none', 'url': TEST_URL}, | |
142 | {'format_id': 'audio-mid', 'ext': 'webm', 'preference': 2, 'vcodec': 'none', 'url': TEST_URL}, | |
143 | {'format_id': 'audio-high', 'ext': 'flv', 'preference': 3, 'vcodec': 'none', 'url': TEST_URL}, | |
144 | {'format_id': 'vid', 'ext': 'mp4', 'preference': 4, 'url': TEST_URL}, | |
145 | ] | |
146 | info_dict = _make_result(formats) | |
147 | ||
148 | ydl = YDL({'format': 'bestaudio'}) | |
149 | ydl.process_ie_result(info_dict.copy()) | |
150 | downloaded = ydl.downloaded_info_dicts[0] | |
151 | self.assertEqual(downloaded['format_id'], 'audio-high') | |
152 | ||
153 | ydl = YDL({'format': 'worstaudio'}) | |
154 | ydl.process_ie_result(info_dict.copy()) | |
155 | downloaded = ydl.downloaded_info_dicts[0] | |
156 | self.assertEqual(downloaded['format_id'], 'audio-low') | |
157 | ||
158 | formats = [ | |
159 | {'format_id': 'vid-low', 'ext': 'mp4', 'preference': 1, 'url': TEST_URL}, | |
160 | {'format_id': 'vid-high', 'ext': 'mp4', 'preference': 2, 'url': TEST_URL}, | |
161 | ] | |
162 | info_dict = _make_result(formats) | |
163 | ||
164 | ydl = YDL({'format': 'bestaudio/worstaudio/best'}) | |
165 | ydl.process_ie_result(info_dict.copy()) | |
166 | downloaded = ydl.downloaded_info_dicts[0] | |
167 | self.assertEqual(downloaded['format_id'], 'vid-high') | |
168 | ||
169 | def test_format_selection_audio_exts(self): | |
170 | formats = [ | |
171 | {'format_id': 'mp3-64', 'ext': 'mp3', 'abr': 64, 'url': 'http://_', 'vcodec': 'none'}, | |
172 | {'format_id': 'ogg-64', 'ext': 'ogg', 'abr': 64, 'url': 'http://_', 'vcodec': 'none'}, | |
173 | {'format_id': 'aac-64', 'ext': 'aac', 'abr': 64, 'url': 'http://_', 'vcodec': 'none'}, | |
174 | {'format_id': 'mp3-32', 'ext': 'mp3', 'abr': 32, 'url': 'http://_', 'vcodec': 'none'}, | |
175 | {'format_id': 'aac-32', 'ext': 'aac', 'abr': 32, 'url': 'http://_', 'vcodec': 'none'}, | |
176 | ] | |
177 | ||
178 | info_dict = _make_result(formats) | |
179 | ydl = YDL({'format': 'best'}) | |
180 | ie = YoutubeIE(ydl) | |
181 | ie._sort_formats(info_dict['formats']) | |
182 | ydl.process_ie_result(copy.deepcopy(info_dict)) | |
183 | downloaded = ydl.downloaded_info_dicts[0] | |
184 | self.assertEqual(downloaded['format_id'], 'aac-64') | |
185 | ||
186 | ydl = YDL({'format': 'mp3'}) | |
187 | ie = YoutubeIE(ydl) | |
188 | ie._sort_formats(info_dict['formats']) | |
189 | ydl.process_ie_result(copy.deepcopy(info_dict)) | |
190 | downloaded = ydl.downloaded_info_dicts[0] | |
191 | self.assertEqual(downloaded['format_id'], 'mp3-64') | |
192 | ||
193 | ydl = YDL({'prefer_free_formats': True}) | |
194 | ie = YoutubeIE(ydl) | |
195 | ie._sort_formats(info_dict['formats']) | |
196 | ydl.process_ie_result(copy.deepcopy(info_dict)) | |
197 | downloaded = ydl.downloaded_info_dicts[0] | |
198 | self.assertEqual(downloaded['format_id'], 'ogg-64') | |
199 | ||
200 | def test_format_selection_video(self): | |
201 | formats = [ | |
202 | {'format_id': 'dash-video-low', 'ext': 'mp4', 'preference': 1, 'acodec': 'none', 'url': TEST_URL}, | |
203 | {'format_id': 'dash-video-high', 'ext': 'mp4', 'preference': 2, 'acodec': 'none', 'url': TEST_URL}, | |
204 | {'format_id': 'vid', 'ext': 'mp4', 'preference': 3, 'url': TEST_URL}, | |
205 | ] | |
206 | info_dict = _make_result(formats) | |
207 | ||
208 | ydl = YDL({'format': 'bestvideo'}) | |
209 | ydl.process_ie_result(info_dict.copy()) | |
210 | downloaded = ydl.downloaded_info_dicts[0] | |
211 | self.assertEqual(downloaded['format_id'], 'dash-video-high') | |
212 | ||
213 | ydl = YDL({'format': 'worstvideo'}) | |
214 | ydl.process_ie_result(info_dict.copy()) | |
215 | downloaded = ydl.downloaded_info_dicts[0] | |
216 | self.assertEqual(downloaded['format_id'], 'dash-video-low') | |
217 | ||
218 | def test_youtube_format_selection(self): | |
219 | order = [ | |
220 | '38', '37', '46', '22', '45', '35', '44', '18', '34', '43', '6', '5', '36', '17', '13', | |
221 | # Apple HTTP Live Streaming | |
222 | '96', '95', '94', '93', '92', '132', '151', | |
223 | # 3D | |
224 | '85', '84', '102', '83', '101', '82', '100', | |
225 | # Dash video | |
226 | '137', '248', '136', '247', '135', '246', | |
227 | '245', '244', '134', '243', '133', '242', '160', | |
228 | # Dash audio | |
229 | '141', '172', '140', '171', '139', | |
230 | ] | |
231 | ||
232 | def format_info(f_id): | |
233 | info = YoutubeIE._formats[f_id].copy() | |
234 | info['format_id'] = f_id | |
235 | info['url'] = 'url:' + f_id | |
236 | return info | |
237 | formats_order = [format_info(f_id) for f_id in order] | |
238 | ||
239 | info_dict = _make_result(list(formats_order), extractor='youtube') | |
240 | ydl = YDL({'format': 'bestvideo+bestaudio'}) | |
241 | yie = YoutubeIE(ydl) | |
242 | yie._sort_formats(info_dict['formats']) | |
243 | ydl.process_ie_result(info_dict) | |
244 | downloaded = ydl.downloaded_info_dicts[0] | |
245 | self.assertEqual(downloaded['format_id'], '137+141') | |
246 | self.assertEqual(downloaded['ext'], 'mp4') | |
247 | ||
248 | for f1, f2 in zip(formats_order, formats_order[1:]): | |
249 | info_dict = _make_result([f1, f2], extractor='youtube') | |
250 | ydl = YDL({'format': 'best/bestvideo'}) | |
251 | yie = YoutubeIE(ydl) | |
252 | yie._sort_formats(info_dict['formats']) | |
253 | ydl.process_ie_result(info_dict) | |
254 | downloaded = ydl.downloaded_info_dicts[0] | |
255 | self.assertEqual(downloaded['format_id'], f1['format_id']) | |
256 | ||
257 | info_dict = _make_result([f2, f1], extractor='youtube') | |
258 | ydl = YDL({'format': 'best/bestvideo'}) | |
259 | yie = YoutubeIE(ydl) | |
260 | yie._sort_formats(info_dict['formats']) | |
261 | ydl.process_ie_result(info_dict) | |
262 | downloaded = ydl.downloaded_info_dicts[0] | |
263 | self.assertEqual(downloaded['format_id'], f1['format_id']) | |
264 | ||
265 | def test_format_filtering(self): | |
266 | formats = [ | |
267 | {'format_id': 'A', 'filesize': 500, 'width': 1000}, | |
268 | {'format_id': 'B', 'filesize': 1000, 'width': 500}, | |
269 | {'format_id': 'C', 'filesize': 1000, 'width': 400}, | |
270 | {'format_id': 'D', 'filesize': 2000, 'width': 600}, | |
271 | {'format_id': 'E', 'filesize': 3000}, | |
272 | {'format_id': 'F'}, | |
273 | {'format_id': 'G', 'filesize': 1000000}, | |
274 | ] | |
275 | for f in formats: | |
276 | f['url'] = 'http://_/' | |
277 | f['ext'] = 'unknown' | |
278 | info_dict = _make_result(formats) | |
279 | ||
280 | ydl = YDL({'format': 'best[filesize<3000]'}) | |
281 | ydl.process_ie_result(info_dict) | |
282 | downloaded = ydl.downloaded_info_dicts[0] | |
283 | self.assertEqual(downloaded['format_id'], 'D') | |
284 | ||
285 | ydl = YDL({'format': 'best[filesize<=3000]'}) | |
286 | ydl.process_ie_result(info_dict) | |
287 | downloaded = ydl.downloaded_info_dicts[0] | |
288 | self.assertEqual(downloaded['format_id'], 'E') | |
289 | ||
290 | ydl = YDL({'format': 'best[filesize <= ? 3000]'}) | |
291 | ydl.process_ie_result(info_dict) | |
292 | downloaded = ydl.downloaded_info_dicts[0] | |
293 | self.assertEqual(downloaded['format_id'], 'F') | |
294 | ||
295 | ydl = YDL({'format': 'best [filesize = 1000] [width>450]'}) | |
296 | ydl.process_ie_result(info_dict) | |
297 | downloaded = ydl.downloaded_info_dicts[0] | |
298 | self.assertEqual(downloaded['format_id'], 'B') | |
299 | ||
300 | ydl = YDL({'format': 'best [filesize = 1000] [width!=450]'}) | |
301 | ydl.process_ie_result(info_dict) | |
302 | downloaded = ydl.downloaded_info_dicts[0] | |
303 | self.assertEqual(downloaded['format_id'], 'C') | |
304 | ||
305 | ydl = YDL({'format': '[filesize>?1]'}) | |
306 | ydl.process_ie_result(info_dict) | |
307 | downloaded = ydl.downloaded_info_dicts[0] | |
308 | self.assertEqual(downloaded['format_id'], 'G') | |
309 | ||
310 | ydl = YDL({'format': '[filesize<1M]'}) | |
311 | ydl.process_ie_result(info_dict) | |
312 | downloaded = ydl.downloaded_info_dicts[0] | |
313 | self.assertEqual(downloaded['format_id'], 'E') | |
314 | ||
315 | ydl = YDL({'format': '[filesize<1MiB]'}) | |
316 | ydl.process_ie_result(info_dict) | |
317 | downloaded = ydl.downloaded_info_dicts[0] | |
318 | self.assertEqual(downloaded['format_id'], 'G') | |
319 | ||
320 | ||
321 | class TestYoutubeDL(unittest.TestCase): | |
322 | def test_subtitles(self): | |
323 | def s_formats(lang, autocaption=False): | |
324 | return [{ | |
325 | 'ext': ext, | |
326 | 'url': 'http://localhost/video.%s.%s' % (lang, ext), | |
327 | '_auto': autocaption, | |
328 | } for ext in ['vtt', 'srt', 'ass']] | |
329 | subtitles = dict((l, s_formats(l)) for l in ['en', 'fr', 'es']) | |
330 | auto_captions = dict((l, s_formats(l, True)) for l in ['it', 'pt', 'es']) | |
331 | info_dict = { | |
332 | 'id': 'test', | |
333 | 'title': 'Test', | |
334 | 'url': 'http://localhost/video.mp4', | |
335 | 'subtitles': subtitles, | |
336 | 'automatic_captions': auto_captions, | |
337 | 'extractor': 'TEST', | |
338 | } | |
339 | ||
340 | def get_info(params={}): | |
341 | params.setdefault('simulate', True) | |
342 | ydl = YDL(params) | |
343 | ydl.report_warning = lambda *args, **kargs: None | |
344 | return ydl.process_video_result(info_dict, download=False) | |
345 | ||
346 | result = get_info() | |
347 | self.assertFalse(result.get('requested_subtitles')) | |
348 | self.assertEqual(result['subtitles'], subtitles) | |
349 | self.assertEqual(result['automatic_captions'], auto_captions) | |
350 | ||
351 | result = get_info({'writesubtitles': True}) | |
352 | subs = result['requested_subtitles'] | |
353 | self.assertTrue(subs) | |
354 | self.assertEqual(set(subs.keys()), set(['en'])) | |
355 | self.assertTrue(subs['en'].get('data') is None) | |
356 | self.assertEqual(subs['en']['ext'], 'ass') | |
357 | ||
358 | result = get_info({'writesubtitles': True, 'subtitlesformat': 'foo/srt'}) | |
359 | subs = result['requested_subtitles'] | |
360 | self.assertEqual(subs['en']['ext'], 'srt') | |
361 | ||
362 | result = get_info({'writesubtitles': True, 'subtitleslangs': ['es', 'fr', 'it']}) | |
363 | subs = result['requested_subtitles'] | |
364 | self.assertTrue(subs) | |
365 | self.assertEqual(set(subs.keys()), set(['es', 'fr'])) | |
366 | ||
367 | result = get_info({'writesubtitles': True, 'writeautomaticsub': True, 'subtitleslangs': ['es', 'pt']}) | |
368 | subs = result['requested_subtitles'] | |
369 | self.assertTrue(subs) | |
370 | self.assertEqual(set(subs.keys()), set(['es', 'pt'])) | |
371 | self.assertFalse(subs['es']['_auto']) | |
372 | self.assertTrue(subs['pt']['_auto']) | |
373 | ||
374 | result = get_info({'writeautomaticsub': True, 'subtitleslangs': ['es', 'pt']}) | |
375 | subs = result['requested_subtitles'] | |
376 | self.assertTrue(subs) | |
377 | self.assertEqual(set(subs.keys()), set(['es', 'pt'])) | |
378 | self.assertTrue(subs['es']['_auto']) | |
379 | self.assertTrue(subs['pt']['_auto']) | |
380 | ||
381 | def test_add_extra_info(self): | |
382 | test_dict = { | |
383 | 'extractor': 'Foo', | |
384 | } | |
385 | extra_info = { | |
386 | 'extractor': 'Bar', | |
387 | 'playlist': 'funny videos', | |
388 | } | |
389 | YDL.add_extra_info(test_dict, extra_info) | |
390 | self.assertEqual(test_dict['extractor'], 'Foo') | |
391 | self.assertEqual(test_dict['playlist'], 'funny videos') | |
392 | ||
393 | def test_prepare_filename(self): | |
394 | info = { | |
395 | 'id': '1234', | |
396 | 'ext': 'mp4', | |
397 | 'width': None, | |
398 | } | |
399 | ||
400 | def fname(templ): | |
401 | ydl = YoutubeDL({'outtmpl': templ}) | |
402 | return ydl.prepare_filename(info) | |
403 | self.assertEqual(fname('%(id)s.%(ext)s'), '1234.mp4') | |
404 | self.assertEqual(fname('%(id)s-%(width)s.%(ext)s'), '1234-NA.mp4') | |
405 | # Replace missing fields with 'NA' | |
406 | self.assertEqual(fname('%(uploader_date)s-%(id)s.%(ext)s'), 'NA-1234.mp4') | |
407 | ||
408 | def test_format_note(self): | |
409 | ydl = YoutubeDL() | |
410 | self.assertEqual(ydl._format_note({}), '') | |
411 | assertRegexpMatches(self, ydl._format_note({ | |
412 | 'vbr': 10, | |
413 | }), '^\s*10k$') | |
414 | ||
415 | def test_postprocessors(self): | |
416 | filename = 'post-processor-testfile.mp4' | |
417 | audiofile = filename + '.mp3' | |
418 | ||
419 | class SimplePP(PostProcessor): | |
420 | def run(self, info): | |
421 | with open(audiofile, 'wt') as f: | |
422 | f.write('EXAMPLE') | |
423 | return [info['filepath']], info | |
424 | ||
425 | def run_pp(params, PP): | |
426 | with open(filename, 'wt') as f: | |
427 | f.write('EXAMPLE') | |
428 | ydl = YoutubeDL(params) | |
429 | ydl.add_post_processor(PP()) | |
430 | ydl.post_process(filename, {'filepath': filename}) | |
431 | ||
432 | run_pp({'keepvideo': True}, SimplePP) | |
433 | self.assertTrue(os.path.exists(filename), '%s doesn\'t exist' % filename) | |
434 | self.assertTrue(os.path.exists(audiofile), '%s doesn\'t exist' % audiofile) | |
435 | os.unlink(filename) | |
436 | os.unlink(audiofile) | |
437 | ||
438 | run_pp({'keepvideo': False}, SimplePP) | |
439 | self.assertFalse(os.path.exists(filename), '%s exists' % filename) | |
440 | self.assertTrue(os.path.exists(audiofile), '%s doesn\'t exist' % audiofile) | |
441 | os.unlink(audiofile) | |
442 | ||
443 | class ModifierPP(PostProcessor): | |
444 | def run(self, info): | |
445 | with open(info['filepath'], 'wt') as f: | |
446 | f.write('MODIFIED') | |
447 | return [], info | |
448 | ||
449 | run_pp({'keepvideo': False}, ModifierPP) | |
450 | self.assertTrue(os.path.exists(filename), '%s doesn\'t exist' % filename) | |
451 | os.unlink(filename) | |
452 | ||
453 | def test_match_filter(self): | |
454 | class FilterYDL(YDL): | |
455 | def __init__(self, *args, **kwargs): | |
456 | super(FilterYDL, self).__init__(*args, **kwargs) | |
457 | self.params['simulate'] = True | |
458 | ||
459 | def process_info(self, info_dict): | |
460 | super(YDL, self).process_info(info_dict) | |
461 | ||
462 | def _match_entry(self, info_dict, incomplete): | |
463 | res = super(FilterYDL, self)._match_entry(info_dict, incomplete) | |
464 | if res is None: | |
465 | self.downloaded_info_dicts.append(info_dict) | |
466 | return res | |
467 | ||
468 | first = { | |
469 | 'id': '1', | |
470 | 'url': TEST_URL, | |
471 | 'title': 'one', | |
472 | 'extractor': 'TEST', | |
473 | 'duration': 30, | |
474 | 'filesize': 10 * 1024, | |
475 | } | |
476 | second = { | |
477 | 'id': '2', | |
478 | 'url': TEST_URL, | |
479 | 'title': 'two', | |
480 | 'extractor': 'TEST', | |
481 | 'duration': 10, | |
482 | 'description': 'foo', | |
483 | 'filesize': 5 * 1024, | |
484 | } | |
485 | videos = [first, second] | |
486 | ||
487 | def get_videos(filter_=None): | |
488 | ydl = FilterYDL({'match_filter': filter_}) | |
489 | for v in videos: | |
490 | ydl.process_ie_result(v, download=True) | |
491 | return [v['id'] for v in ydl.downloaded_info_dicts] | |
492 | ||
493 | res = get_videos() | |
494 | self.assertEqual(res, ['1', '2']) | |
495 | ||
496 | def f(v): | |
497 | if v['id'] == '1': | |
498 | return None | |
499 | else: | |
500 | return 'Video id is not 1' | |
501 | res = get_videos(f) | |
502 | self.assertEqual(res, ['1']) | |
503 | ||
504 | f = match_filter_func('duration < 30') | |
505 | res = get_videos(f) | |
506 | self.assertEqual(res, ['2']) | |
507 | ||
508 | f = match_filter_func('description = foo') | |
509 | res = get_videos(f) | |
510 | self.assertEqual(res, ['2']) | |
511 | ||
512 | f = match_filter_func('description =? foo') | |
513 | res = get_videos(f) | |
514 | self.assertEqual(res, ['1', '2']) | |
515 | ||
516 | f = match_filter_func('filesize > 5KiB') | |
517 | res = get_videos(f) | |
518 | self.assertEqual(res, ['1']) | |
519 | ||
520 | def test_playlist_items_selection(self): | |
521 | entries = [{ | |
522 | 'id': compat_str(i), | |
523 | 'title': compat_str(i), | |
524 | 'url': TEST_URL, | |
525 | } for i in range(1, 5)] | |
526 | playlist = { | |
527 | '_type': 'playlist', | |
528 | 'id': 'test', | |
529 | 'entries': entries, | |
530 | 'extractor': 'test:playlist', | |
531 | 'extractor_key': 'test:playlist', | |
532 | 'webpage_url': 'http://example.com', | |
533 | } | |
534 | ||
535 | def get_ids(params): | |
536 | ydl = YDL(params) | |
537 | # make a copy because the dictionary can be modified | |
538 | ydl.process_ie_result(playlist.copy()) | |
539 | return [int(v['id']) for v in ydl.downloaded_info_dicts] | |
540 | ||
541 | result = get_ids({}) | |
542 | self.assertEqual(result, [1, 2, 3, 4]) | |
543 | ||
544 | result = get_ids({'playlistend': 10}) | |
545 | self.assertEqual(result, [1, 2, 3, 4]) | |
546 | ||
547 | result = get_ids({'playlistend': 2}) | |
548 | self.assertEqual(result, [1, 2]) | |
549 | ||
550 | result = get_ids({'playliststart': 10}) | |
551 | self.assertEqual(result, []) | |
552 | ||
553 | result = get_ids({'playliststart': 2}) | |
554 | self.assertEqual(result, [2, 3, 4]) | |
555 | ||
556 | result = get_ids({'playlist_items': '2-4'}) | |
557 | self.assertEqual(result, [2, 3, 4]) | |
558 | ||
559 | result = get_ids({'playlist_items': '2,4'}) | |
560 | self.assertEqual(result, [2, 4]) | |
561 | ||
562 | result = get_ids({'playlist_items': '10'}) | |
563 | self.assertEqual(result, []) | |
564 | ||
565 | ||
566 | if __name__ == '__main__': | |
567 | unittest.main() |