]>
Commit | Line | Data |
---|---|---|
cc52de43 | 1 | #!/usr/bin/env python3 |
54007a45 | 2 | |
5d254f77 PH |
3 | # Allow direct execution |
4 | import os | |
e028d0d1 JMF |
5 | import sys |
6 | import unittest | |
f8271158 | 7 | |
5d254f77 | 8 | sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
e028d0d1 | 9 | |
54007a45 | 10 | |
0217c783 | 11 | import copy |
7d1eb38a | 12 | import json |
f8271158 | 13 | |
6c5211ce | 14 | from test.helper import FakeYDL, assertRegexpMatches, try_rm |
7a5c1cfe | 15 | from yt_dlp import YoutubeDL |
14f25df2 | 16 | from yt_dlp.compat import compat_os_name |
7a5c1cfe P |
17 | from yt_dlp.extractor import YoutubeIE |
18 | from yt_dlp.extractor.common import InfoExtractor | |
19 | from yt_dlp.postprocessor.common import PostProcessor | |
f8271158 | 20 | from yt_dlp.utils import ( |
21 | ExtractorError, | |
22 | LazyList, | |
7e88d7d7 | 23 | OnDemandPagedList, |
f8271158 | 24 | int_or_none, |
25 | match_filter_func, | |
26 | ) | |
6c5211ce | 27 | from yt_dlp.utils.traversal import traverse_obj |
28 | ||
8508557e JMF |
29 | TEST_URL = 'http://localhost/sample.mp4' |
30 | ||
e028d0d1 JMF |
31 | |
32 | class YDL(FakeYDL): | |
f4d96df0 | 33 | def __init__(self, *args, **kwargs): |
86e5f3ed | 34 | super().__init__(*args, **kwargs) |
e028d0d1 | 35 | self.downloaded_info_dicts = [] |
f4d96df0 | 36 | self.msgs = [] |
5d254f77 | 37 | |
e028d0d1 | 38 | def process_info(self, info_dict): |
09b49e1f | 39 | self.downloaded_info_dicts.append(info_dict.copy()) |
e028d0d1 | 40 | |
f0500bd1 | 41 | def to_screen(self, msg, *args, **kwargs): |
f4d96df0 PH |
42 | self.msgs.append(msg) |
43 | ||
b5ac45b1 | 44 | def dl(self, *args, **kwargs): |
45 | assert False, 'Downloader must not be invoked for test_YoutubeDL' | |
46 | ||
5d254f77 | 47 | |
3537b93d PH |
48 | def _make_result(formats, **kwargs): |
49 | res = { | |
50 | 'formats': formats, | |
51 | 'id': 'testid', | |
52 | 'title': 'testttitle', | |
53 | 'extractor': 'testex', | |
0396806f | 54 | 'extractor_key': 'TestEx', |
732044af | 55 | 'webpage_url': 'http://example.com/watch?v=shenanigans', |
3537b93d PH |
56 | } |
57 | res.update(**kwargs) | |
58 | return res | |
59 | ||
60 | ||
e028d0d1 JMF |
61 | class TestFormatSelection(unittest.TestCase): |
62 | def test_prefer_free_formats(self): | |
63 | # Same resolution => download webm | |
64 | ydl = YDL() | |
65 | ydl.params['prefer_free_formats'] = True | |
5d254f77 | 66 | formats = [ |
8508557e JMF |
67 | {'ext': 'webm', 'height': 460, 'url': TEST_URL}, |
68 | {'ext': 'mp4', 'height': 460, 'url': TEST_URL}, | |
5d254f77 | 69 | ] |
3537b93d | 70 | info_dict = _make_result(formats) |
9f14daf2 | 71 | ydl.sort_formats(info_dict) |
e028d0d1 JMF |
72 | ydl.process_ie_result(info_dict) |
73 | downloaded = ydl.downloaded_info_dicts[0] | |
89087418 | 74 | self.assertEqual(downloaded['ext'], 'webm') |
e028d0d1 JMF |
75 | |
76 | # Different resolution => download best quality (mp4) | |
77 | ydl = YDL() | |
78 | ydl.params['prefer_free_formats'] = True | |
5d254f77 | 79 | formats = [ |
8508557e JMF |
80 | {'ext': 'webm', 'height': 720, 'url': TEST_URL}, |
81 | {'ext': 'mp4', 'height': 1080, 'url': TEST_URL}, | |
5d254f77 | 82 | ] |
89087418 | 83 | info_dict['formats'] = formats |
9f14daf2 | 84 | ydl.sort_formats(info_dict) |
e028d0d1 JMF |
85 | ydl.process_ie_result(info_dict) |
86 | downloaded = ydl.downloaded_info_dicts[0] | |
89087418 | 87 | self.assertEqual(downloaded['ext'], 'mp4') |
e028d0d1 | 88 | |
5d0c5371 | 89 | # No prefer_free_formats => prefer mp4 and webm |
e028d0d1 JMF |
90 | ydl = YDL() |
91 | ydl.params['prefer_free_formats'] = False | |
5d254f77 | 92 | formats = [ |
8508557e JMF |
93 | {'ext': 'webm', 'height': 720, 'url': TEST_URL}, |
94 | {'ext': 'mp4', 'height': 720, 'url': TEST_URL}, | |
95 | {'ext': 'flv', 'height': 720, 'url': TEST_URL}, | |
5d254f77 | 96 | ] |
89087418 | 97 | info_dict['formats'] = formats |
9f14daf2 | 98 | ydl.sort_formats(info_dict) |
3d4a70b8 PH |
99 | ydl.process_ie_result(info_dict) |
100 | downloaded = ydl.downloaded_info_dicts[0] | |
89087418 | 101 | self.assertEqual(downloaded['ext'], 'mp4') |
3d4a70b8 PH |
102 | |
103 | ydl = YDL() | |
104 | ydl.params['prefer_free_formats'] = False | |
105 | formats = [ | |
8508557e JMF |
106 | {'ext': 'flv', 'height': 720, 'url': TEST_URL}, |
107 | {'ext': 'webm', 'height': 720, 'url': TEST_URL}, | |
3d4a70b8 | 108 | ] |
89087418 | 109 | info_dict['formats'] = formats |
9f14daf2 | 110 | ydl.sort_formats(info_dict) |
e028d0d1 JMF |
111 | ydl.process_ie_result(info_dict) |
112 | downloaded = ydl.downloaded_info_dicts[0] | |
5d0c5371 | 113 | self.assertEqual(downloaded['ext'], 'webm') |
e028d0d1 | 114 | |
a9c58ad9 JMF |
115 | def test_format_selection(self): |
116 | formats = [ | |
9f14daf2 | 117 | {'format_id': '35', 'ext': 'mp4', 'preference': 0, 'url': TEST_URL}, |
232541df | 118 | {'format_id': 'example-with-dashes', 'ext': 'webm', 'preference': 1, 'url': TEST_URL}, |
8508557e JMF |
119 | {'format_id': '45', 'ext': 'webm', 'preference': 2, 'url': TEST_URL}, |
120 | {'format_id': '47', 'ext': 'webm', 'preference': 3, 'url': TEST_URL}, | |
121 | {'format_id': '2', 'ext': 'flv', 'preference': 4, 'url': TEST_URL}, | |
a9c58ad9 | 122 | ] |
3537b93d | 123 | info_dict = _make_result(formats) |
a9c58ad9 | 124 | |
b5ac45b1 | 125 | def test(inp, *expected, multi=False): |
126 | ydl = YDL({ | |
127 | 'format': inp, | |
128 | 'allow_multiple_video_streams': multi, | |
129 | 'allow_multiple_audio_streams': multi, | |
130 | }) | |
131 | ydl.process_ie_result(info_dict.copy()) | |
132 | downloaded = map(lambda x: x['format_id'], ydl.downloaded_info_dicts) | |
133 | self.assertEqual(list(downloaded), list(expected)) | |
134 | ||
135 | test('20/47', '47') | |
136 | test('20/71/worst', '35') | |
137 | test(None, '2') | |
138 | test('webm/mp4', '47') | |
139 | test('3gp/40/mp4', '35') | |
140 | test('example-with-dashes', 'example-with-dashes') | |
dd2a987d | 141 | test('all', '2', '47', '45', 'example-with-dashes', '35') |
b5ac45b1 | 142 | test('mergeall', '2+47+45+example-with-dashes+35', multi=True) |
00cdda4f SS |
143 | # See: https://github.com/yt-dlp/yt-dlp/pulls/8797 |
144 | test('7_a/worst', '35') | |
232541df | 145 | |
ba7678f9 PH |
146 | def test_format_selection_audio(self): |
147 | formats = [ | |
8508557e JMF |
148 | {'format_id': 'audio-low', 'ext': 'webm', 'preference': 1, 'vcodec': 'none', 'url': TEST_URL}, |
149 | {'format_id': 'audio-mid', 'ext': 'webm', 'preference': 2, 'vcodec': 'none', 'url': TEST_URL}, | |
150 | {'format_id': 'audio-high', 'ext': 'flv', 'preference': 3, 'vcodec': 'none', 'url': TEST_URL}, | |
151 | {'format_id': 'vid', 'ext': 'mp4', 'preference': 4, 'url': TEST_URL}, | |
ba7678f9 | 152 | ] |
3537b93d | 153 | info_dict = _make_result(formats) |
ba7678f9 | 154 | |
89087418 | 155 | ydl = YDL({'format': 'bestaudio'}) |
ba7678f9 PH |
156 | ydl.process_ie_result(info_dict.copy()) |
157 | downloaded = ydl.downloaded_info_dicts[0] | |
89087418 | 158 | self.assertEqual(downloaded['format_id'], 'audio-high') |
ba7678f9 | 159 | |
89087418 | 160 | ydl = YDL({'format': 'worstaudio'}) |
ba7678f9 PH |
161 | ydl.process_ie_result(info_dict.copy()) |
162 | downloaded = ydl.downloaded_info_dicts[0] | |
89087418 | 163 | self.assertEqual(downloaded['format_id'], 'audio-low') |
ba7678f9 PH |
164 | |
165 | formats = [ | |
8508557e JMF |
166 | {'format_id': 'vid-low', 'ext': 'mp4', 'preference': 1, 'url': TEST_URL}, |
167 | {'format_id': 'vid-high', 'ext': 'mp4', 'preference': 2, 'url': TEST_URL}, | |
ba7678f9 | 168 | ] |
3537b93d | 169 | info_dict = _make_result(formats) |
ba7678f9 | 170 | |
89087418 | 171 | ydl = YDL({'format': 'bestaudio/worstaudio/best'}) |
ba7678f9 PH |
172 | ydl.process_ie_result(info_dict.copy()) |
173 | downloaded = ydl.downloaded_info_dicts[0] | |
89087418 | 174 | self.assertEqual(downloaded['format_id'], 'vid-high') |
ba7678f9 | 175 | |
0217c783 PH |
176 | def test_format_selection_audio_exts(self): |
177 | formats = [ | |
178 | {'format_id': 'mp3-64', 'ext': 'mp3', 'abr': 64, 'url': 'http://_', 'vcodec': 'none'}, | |
179 | {'format_id': 'ogg-64', 'ext': 'ogg', 'abr': 64, 'url': 'http://_', 'vcodec': 'none'}, | |
180 | {'format_id': 'aac-64', 'ext': 'aac', 'abr': 64, 'url': 'http://_', 'vcodec': 'none'}, | |
181 | {'format_id': 'mp3-32', 'ext': 'mp3', 'abr': 32, 'url': 'http://_', 'vcodec': 'none'}, | |
182 | {'format_id': 'aac-32', 'ext': 'aac', 'abr': 32, 'url': 'http://_', 'vcodec': 'none'}, | |
183 | ] | |
184 | ||
185 | info_dict = _make_result(formats) | |
186 | ydl = YDL({'format': 'best'}) | |
9f14daf2 | 187 | ydl.sort_formats(info_dict) |
0217c783 PH |
188 | ydl.process_ie_result(copy.deepcopy(info_dict)) |
189 | downloaded = ydl.downloaded_info_dicts[0] | |
190 | self.assertEqual(downloaded['format_id'], 'aac-64') | |
191 | ||
192 | ydl = YDL({'format': 'mp3'}) | |
9f14daf2 | 193 | ydl.sort_formats(info_dict) |
0217c783 PH |
194 | ydl.process_ie_result(copy.deepcopy(info_dict)) |
195 | downloaded = ydl.downloaded_info_dicts[0] | |
196 | self.assertEqual(downloaded['format_id'], 'mp3-64') | |
197 | ||
198 | ydl = YDL({'prefer_free_formats': True}) | |
9f14daf2 | 199 | ydl.sort_formats(info_dict) |
0217c783 PH |
200 | ydl.process_ie_result(copy.deepcopy(info_dict)) |
201 | downloaded = ydl.downloaded_info_dicts[0] | |
202 | self.assertEqual(downloaded['format_id'], 'ogg-64') | |
203 | ||
bc6d5978 JMF |
204 | def test_format_selection_video(self): |
205 | formats = [ | |
8508557e JMF |
206 | {'format_id': 'dash-video-low', 'ext': 'mp4', 'preference': 1, 'acodec': 'none', 'url': TEST_URL}, |
207 | {'format_id': 'dash-video-high', 'ext': 'mp4', 'preference': 2, 'acodec': 'none', 'url': TEST_URL}, | |
208 | {'format_id': 'vid', 'ext': 'mp4', 'preference': 3, 'url': TEST_URL}, | |
bc6d5978 | 209 | ] |
3537b93d | 210 | info_dict = _make_result(formats) |
bc6d5978 JMF |
211 | |
212 | ydl = YDL({'format': 'bestvideo'}) | |
213 | ydl.process_ie_result(info_dict.copy()) | |
214 | downloaded = ydl.downloaded_info_dicts[0] | |
215 | self.assertEqual(downloaded['format_id'], 'dash-video-high') | |
216 | ||
217 | ydl = YDL({'format': 'worstvideo'}) | |
218 | ydl.process_ie_result(info_dict.copy()) | |
219 | downloaded = ydl.downloaded_info_dicts[0] | |
220 | self.assertEqual(downloaded['format_id'], 'dash-video-low') | |
221 | ||
4c3b16d5 S |
222 | ydl = YDL({'format': 'bestvideo[format_id^=dash][format_id$=low]'}) |
223 | ydl.process_ie_result(info_dict.copy()) | |
224 | downloaded = ydl.downloaded_info_dicts[0] | |
225 | self.assertEqual(downloaded['format_id'], 'dash-video-low') | |
226 | ||
b913348d | 227 | formats = [ |
228 | {'format_id': 'vid-vcodec-dot', 'ext': 'mp4', 'preference': 1, 'vcodec': 'avc1.123456', 'acodec': 'none', 'url': TEST_URL}, | |
229 | ] | |
230 | info_dict = _make_result(formats) | |
231 | ||
232 | ydl = YDL({'format': 'bestvideo[vcodec=avc1.123456]'}) | |
233 | ydl.process_ie_result(info_dict.copy()) | |
234 | downloaded = ydl.downloaded_info_dicts[0] | |
235 | self.assertEqual(downloaded['format_id'], 'vid-vcodec-dot') | |
236 | ||
2cc779f4 S |
237 | def test_format_selection_string_ops(self): |
238 | formats = [ | |
239 | {'format_id': 'abc-cba', 'ext': 'mp4', 'url': TEST_URL}, | |
e118a879 | 240 | {'format_id': 'zxc-cxz', 'ext': 'webm', 'url': TEST_URL}, |
2cc779f4 S |
241 | ] |
242 | info_dict = _make_result(formats) | |
243 | ||
244 | # equals (=) | |
245 | ydl = YDL({'format': '[format_id=abc-cba]'}) | |
246 | ydl.process_ie_result(info_dict.copy()) | |
247 | downloaded = ydl.downloaded_info_dicts[0] | |
248 | self.assertEqual(downloaded['format_id'], 'abc-cba') | |
249 | ||
250 | # does not equal (!=) | |
251 | ydl = YDL({'format': '[format_id!=abc-cba]'}) | |
e118a879 S |
252 | ydl.process_ie_result(info_dict.copy()) |
253 | downloaded = ydl.downloaded_info_dicts[0] | |
254 | self.assertEqual(downloaded['format_id'], 'zxc-cxz') | |
255 | ||
256 | ydl = YDL({'format': '[format_id!=abc-cba][format_id!=zxc-cxz]'}) | |
2cc779f4 S |
257 | self.assertRaises(ExtractorError, ydl.process_ie_result, info_dict.copy()) |
258 | ||
259 | # starts with (^=) | |
260 | ydl = YDL({'format': '[format_id^=abc]'}) | |
261 | ydl.process_ie_result(info_dict.copy()) | |
262 | downloaded = ydl.downloaded_info_dicts[0] | |
263 | self.assertEqual(downloaded['format_id'], 'abc-cba') | |
264 | ||
265 | # does not start with (!^=) | |
e118a879 S |
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'], 'zxc-cxz') | |
270 | ||
271 | ydl = YDL({'format': '[format_id!^=abc][format_id!^=zxc]'}) | |
2cc779f4 S |
272 | self.assertRaises(ExtractorError, ydl.process_ie_result, info_dict.copy()) |
273 | ||
274 | # ends with ($=) | |
275 | ydl = YDL({'format': '[format_id$=cba]'}) | |
276 | ydl.process_ie_result(info_dict.copy()) | |
277 | downloaded = ydl.downloaded_info_dicts[0] | |
278 | self.assertEqual(downloaded['format_id'], 'abc-cba') | |
279 | ||
280 | # does not end with (!$=) | |
e118a879 S |
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'], 'zxc-cxz') | |
285 | ||
286 | ydl = YDL({'format': '[format_id!$=cba][format_id!$=cxz]'}) | |
2cc779f4 S |
287 | self.assertRaises(ExtractorError, ydl.process_ie_result, info_dict.copy()) |
288 | ||
289 | # contains (*=) | |
e118a879 | 290 | ydl = YDL({'format': '[format_id*=bc-cb]'}) |
2cc779f4 S |
291 | ydl.process_ie_result(info_dict.copy()) |
292 | downloaded = ydl.downloaded_info_dicts[0] | |
293 | self.assertEqual(downloaded['format_id'], 'abc-cba') | |
294 | ||
295 | # does not contain (!*=) | |
e118a879 S |
296 | ydl = YDL({'format': '[format_id!*=bc-cb]'}) |
297 | ydl.process_ie_result(info_dict.copy()) | |
298 | downloaded = ydl.downloaded_info_dicts[0] | |
299 | self.assertEqual(downloaded['format_id'], 'zxc-cxz') | |
300 | ||
301 | ydl = YDL({'format': '[format_id!*=abc][format_id!*=zxc]'}) | |
302 | self.assertRaises(ExtractorError, ydl.process_ie_result, info_dict.copy()) | |
303 | ||
2cc779f4 S |
304 | ydl = YDL({'format': '[format_id!*=-]'}) |
305 | self.assertRaises(ExtractorError, ydl.process_ie_result, info_dict.copy()) | |
306 | ||
3d4a70b8 | 307 | def test_youtube_format_selection(self): |
a7191c6f | 308 | # FIXME: Rewrite in accordance with the new format sorting options |
5d0c5371 | 309 | return |
5d0c5371 | 310 | |
3d4a70b8 | 311 | order = [ |
86bf2905 | 312 | '38', '37', '46', '22', '45', '35', '44', '18', '34', '43', '6', '5', '17', '36', '13', |
3d4a70b8 PH |
313 | # Apple HTTP Live Streaming |
314 | '96', '95', '94', '93', '92', '132', '151', | |
315 | # 3D | |
316 | '85', '84', '102', '83', '101', '82', '100', | |
317 | # Dash video | |
c11125f9 | 318 | '137', '248', '136', '247', '135', '246', |
3d4a70b8 PH |
319 | '245', '244', '134', '243', '133', '242', '160', |
320 | # Dash audio | |
a053c349 | 321 | '141', '172', '140', '171', '139', |
3d4a70b8 PH |
322 | ] |
323 | ||
67134eab JMF |
324 | def format_info(f_id): |
325 | info = YoutubeIE._formats[f_id].copy() | |
1df41411 | 326 | |
91cb6b50 | 327 | # XXX: In real cases InfoExtractor._parse_mpd_formats() fills up 'acodec' |
1df41411 YCH |
328 | # and 'vcodec', while in tests such information is incomplete since |
329 | # commit a6c2c24479e5f4827ceb06f64d855329c0a6f593 | |
330 | # test_YoutubeDL.test_youtube_format_selection is broken without | |
331 | # this fix | |
332 | if 'acodec' in info and 'vcodec' not in info: | |
333 | info['vcodec'] = 'none' | |
334 | elif 'vcodec' in info and 'acodec' not in info: | |
335 | info['acodec'] = 'none' | |
336 | ||
67134eab JMF |
337 | info['format_id'] = f_id |
338 | info['url'] = 'url:' + f_id | |
339 | return info | |
340 | formats_order = [format_info(f_id) for f_id in order] | |
341 | ||
342 | info_dict = _make_result(list(formats_order), extractor='youtube') | |
343 | ydl = YDL({'format': 'bestvideo+bestaudio'}) | |
9f14daf2 | 344 | ydl.sort_formats(info_dict) |
67134eab JMF |
345 | ydl.process_ie_result(info_dict) |
346 | downloaded = ydl.downloaded_info_dicts[0] | |
5d0c5371 | 347 | self.assertEqual(downloaded['format_id'], '248+172') |
67134eab | 348 | self.assertEqual(downloaded['ext'], 'mp4') |
3d4a70b8 | 349 | |
cf2ac6df JMF |
350 | info_dict = _make_result(list(formats_order), extractor='youtube') |
351 | ydl = YDL({'format': 'bestvideo[height>=999999]+bestaudio/best'}) | |
9f14daf2 | 352 | ydl.sort_formats(info_dict) |
cf2ac6df JMF |
353 | ydl.process_ie_result(info_dict) |
354 | downloaded = ydl.downloaded_info_dicts[0] | |
355 | self.assertEqual(downloaded['format_id'], '38') | |
356 | ||
f5f4a27a JMF |
357 | info_dict = _make_result(list(formats_order), extractor='youtube') |
358 | ydl = YDL({'format': 'bestvideo/best,bestaudio'}) | |
9f14daf2 | 359 | ydl.sort_formats(info_dict) |
f5f4a27a JMF |
360 | ydl.process_ie_result(info_dict) |
361 | downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts] | |
362 | self.assertEqual(downloaded_ids, ['137', '141']) | |
363 | ||
0130afb7 JMF |
364 | info_dict = _make_result(list(formats_order), extractor='youtube') |
365 | ydl = YDL({'format': '(bestvideo[ext=mp4],bestvideo[ext=webm])+bestaudio'}) | |
9f14daf2 | 366 | ydl.sort_formats(info_dict) |
0130afb7 JMF |
367 | ydl.process_ie_result(info_dict) |
368 | downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts] | |
369 | self.assertEqual(downloaded_ids, ['137+141', '248+141']) | |
370 | ||
371 | info_dict = _make_result(list(formats_order), extractor='youtube') | |
372 | ydl = YDL({'format': '(bestvideo[ext=mp4],bestvideo[ext=webm])[height<=720]+bestaudio'}) | |
9f14daf2 | 373 | ydl.sort_formats(info_dict) |
0130afb7 JMF |
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, ['136+141', '247+141']) | |
377 | ||
378 | info_dict = _make_result(list(formats_order), extractor='youtube') | |
379 | ydl = YDL({'format': '(bestvideo[ext=none]/bestvideo[ext=webm])+bestaudio'}) | |
9f14daf2 | 380 | ydl.sort_formats(info_dict) |
0130afb7 JMF |
381 | ydl.process_ie_result(info_dict) |
382 | downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts] | |
383 | self.assertEqual(downloaded_ids, ['248+141']) | |
384 | ||
67134eab | 385 | for f1, f2 in zip(formats_order, formats_order[1:]): |
3537b93d | 386 | info_dict = _make_result([f1, f2], extractor='youtube') |
8dd54188 | 387 | ydl = YDL({'format': 'best/bestvideo'}) |
9f14daf2 | 388 | ydl.sort_formats(info_dict) |
3d4a70b8 PH |
389 | ydl.process_ie_result(info_dict) |
390 | downloaded = ydl.downloaded_info_dicts[0] | |
67134eab | 391 | self.assertEqual(downloaded['format_id'], f1['format_id']) |
3d4a70b8 | 392 | |
3537b93d | 393 | info_dict = _make_result([f2, f1], extractor='youtube') |
8dd54188 | 394 | ydl = YDL({'format': 'best/bestvideo'}) |
9f14daf2 | 395 | ydl.sort_formats(info_dict) |
3d4a70b8 PH |
396 | ydl.process_ie_result(info_dict) |
397 | downloaded = ydl.downloaded_info_dicts[0] | |
67134eab | 398 | self.assertEqual(downloaded['format_id'], f1['format_id']) |
3d4a70b8 | 399 | |
317f7ab6 S |
400 | def test_audio_only_extractor_format_selection(self): |
401 | # For extractors with incomplete formats (all formats are audio-only or | |
402 | # video-only) best and worst should fallback to corresponding best/worst | |
403 | # video-only or audio-only formats (as per | |
067aa17e | 404 | # https://github.com/ytdl-org/youtube-dl/pull/5556) |
317f7ab6 S |
405 | formats = [ |
406 | {'format_id': 'low', 'ext': 'mp3', 'preference': 1, 'vcodec': 'none', 'url': TEST_URL}, | |
407 | {'format_id': 'high', 'ext': 'mp3', 'preference': 2, 'vcodec': 'none', 'url': TEST_URL}, | |
408 | ] | |
409 | info_dict = _make_result(formats) | |
410 | ||
411 | ydl = YDL({'format': 'best'}) | |
412 | ydl.process_ie_result(info_dict.copy()) | |
413 | downloaded = ydl.downloaded_info_dicts[0] | |
414 | self.assertEqual(downloaded['format_id'], 'high') | |
415 | ||
416 | ydl = YDL({'format': 'worst'}) | |
417 | ydl.process_ie_result(info_dict.copy()) | |
418 | downloaded = ydl.downloaded_info_dicts[0] | |
419 | self.assertEqual(downloaded['format_id'], 'low') | |
420 | ||
421 | def test_format_not_available(self): | |
422 | formats = [ | |
423 | {'format_id': 'regular', 'ext': 'mp4', 'height': 360, 'url': TEST_URL}, | |
424 | {'format_id': 'video', 'ext': 'mp4', 'height': 720, 'acodec': 'none', 'url': TEST_URL}, | |
425 | ] | |
426 | info_dict = _make_result(formats) | |
427 | ||
428 | # This must fail since complete video-audio format does not match filter | |
429 | # and extractor does not provide incomplete only formats (i.e. only | |
430 | # video-only or audio-only). | |
431 | ydl = YDL({'format': 'best[height>360]'}) | |
432 | self.assertRaises(ExtractorError, ydl.process_ie_result, info_dict.copy()) | |
433 | ||
8cda78ef | 434 | def test_format_selection_issue_10083(self): |
067aa17e | 435 | # See https://github.com/ytdl-org/youtube-dl/issues/10083 |
8cda78ef S |
436 | formats = [ |
437 | {'format_id': 'regular', 'height': 360, 'url': TEST_URL}, | |
438 | {'format_id': 'video', 'height': 720, 'acodec': 'none', 'url': TEST_URL}, | |
439 | {'format_id': 'audio', 'vcodec': 'none', 'url': TEST_URL}, | |
440 | ] | |
441 | info_dict = _make_result(formats) | |
442 | ||
443 | ydl = YDL({'format': 'best[height>360]/bestvideo[height>360]+bestaudio'}) | |
444 | ydl.process_ie_result(info_dict.copy()) | |
445 | self.assertEqual(ydl.downloaded_info_dicts[0]['format_id'], 'video+audio') | |
446 | ||
0a31a350 JMF |
447 | def test_invalid_format_specs(self): |
448 | def assert_syntax_error(format_spec): | |
187986a8 | 449 | self.assertRaises(SyntaxError, YDL, {'format': format_spec}) |
0a31a350 JMF |
450 | |
451 | assert_syntax_error('bestvideo,,best') | |
452 | assert_syntax_error('+bestaudio') | |
453 | assert_syntax_error('bestvideo+') | |
d96d604e | 454 | assert_syntax_error('/') |
187986a8 | 455 | assert_syntax_error('[720<height]') |
0a31a350 | 456 | |
083c9df9 PH |
457 | def test_format_filtering(self): |
458 | formats = [ | |
459 | {'format_id': 'A', 'filesize': 500, 'width': 1000}, | |
460 | {'format_id': 'B', 'filesize': 1000, 'width': 500}, | |
461 | {'format_id': 'C', 'filesize': 1000, 'width': 400}, | |
462 | {'format_id': 'D', 'filesize': 2000, 'width': 600}, | |
463 | {'format_id': 'E', 'filesize': 3000}, | |
464 | {'format_id': 'F'}, | |
465 | {'format_id': 'G', 'filesize': 1000000}, | |
466 | ] | |
467 | for f in formats: | |
468 | f['url'] = 'http://_/' | |
469 | f['ext'] = 'unknown' | |
9f14daf2 | 470 | info_dict = _make_result(formats, _format_sort_fields=('id', )) |
083c9df9 PH |
471 | |
472 | ydl = YDL({'format': 'best[filesize<3000]'}) | |
473 | ydl.process_ie_result(info_dict) | |
474 | downloaded = ydl.downloaded_info_dicts[0] | |
475 | self.assertEqual(downloaded['format_id'], 'D') | |
476 | ||
477 | ydl = YDL({'format': 'best[filesize<=3000]'}) | |
478 | ydl.process_ie_result(info_dict) | |
479 | downloaded = ydl.downloaded_info_dicts[0] | |
480 | self.assertEqual(downloaded['format_id'], 'E') | |
481 | ||
482 | ydl = YDL({'format': 'best[filesize <= ? 3000]'}) | |
483 | ydl.process_ie_result(info_dict) | |
484 | downloaded = ydl.downloaded_info_dicts[0] | |
485 | self.assertEqual(downloaded['format_id'], 'F') | |
486 | ||
487 | ydl = YDL({'format': 'best [filesize = 1000] [width>450]'}) | |
488 | ydl.process_ie_result(info_dict) | |
489 | downloaded = ydl.downloaded_info_dicts[0] | |
490 | self.assertEqual(downloaded['format_id'], 'B') | |
491 | ||
492 | ydl = YDL({'format': 'best [filesize = 1000] [width!=450]'}) | |
493 | ydl.process_ie_result(info_dict) | |
494 | downloaded = ydl.downloaded_info_dicts[0] | |
495 | self.assertEqual(downloaded['format_id'], 'C') | |
496 | ||
497 | ydl = YDL({'format': '[filesize>?1]'}) | |
498 | ydl.process_ie_result(info_dict) | |
499 | downloaded = ydl.downloaded_info_dicts[0] | |
500 | self.assertEqual(downloaded['format_id'], 'G') | |
501 | ||
502 | ydl = YDL({'format': '[filesize<1M]'}) | |
503 | ydl.process_ie_result(info_dict) | |
504 | downloaded = ydl.downloaded_info_dicts[0] | |
505 | self.assertEqual(downloaded['format_id'], 'E') | |
506 | ||
507 | ydl = YDL({'format': '[filesize<1MiB]'}) | |
508 | ydl.process_ie_result(info_dict) | |
509 | downloaded = ydl.downloaded_info_dicts[0] | |
510 | self.assertEqual(downloaded['format_id'], 'G') | |
511 | ||
5acfa126 JMF |
512 | ydl = YDL({'format': 'all[width>=400][width<=600]'}) |
513 | ydl.process_ie_result(info_dict) | |
514 | downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts] | |
dd2a987d | 515 | self.assertEqual(downloaded_ids, ['D', 'C', 'B']) |
5acfa126 | 516 | |
bb8e5536 JMF |
517 | ydl = YDL({'format': 'best[height<40]'}) |
518 | try: | |
519 | ydl.process_ie_result(info_dict) | |
520 | except ExtractorError: | |
521 | pass | |
522 | self.assertEqual(ydl.downloaded_info_dicts, []) | |
523 | ||
0017d9ad S |
524 | def test_default_format_spec(self): |
525 | ydl = YDL({'simulate': True}) | |
5d0c5371 | 526 | self.assertEqual(ydl._default_format_spec({}), 'bestvideo*+bestaudio/best') |
0017d9ad | 527 | |
d08dcd2d S |
528 | ydl = YDL({}) |
529 | self.assertEqual(ydl._default_format_spec({'is_live': True}), 'best/bestvideo+bestaudio') | |
af0f7428 | 530 | |
d08dcd2d | 531 | ydl = YDL({'simulate': True}) |
5d0c5371 | 532 | self.assertEqual(ydl._default_format_spec({'is_live': True}), 'bestvideo*+bestaudio/best') |
af0f7428 | 533 | |
0017d9ad | 534 | ydl = YDL({'outtmpl': '-'}) |
af0f7428 | 535 | self.assertEqual(ydl._default_format_spec({}), 'best/bestvideo+bestaudio') |
0017d9ad S |
536 | |
537 | ydl = YDL({}) | |
5d0c5371 | 538 | self.assertEqual(ydl._default_format_spec({}, download=False), 'bestvideo*+bestaudio/best') |
af0f7428 | 539 | self.assertEqual(ydl._default_format_spec({'is_live': True}), 'best/bestvideo+bestaudio') |
0017d9ad | 540 | |
f20bf146 JMF |
541 | |
542 | class TestYoutubeDL(unittest.TestCase): | |
ab84349b JMF |
543 | def test_subtitles(self): |
544 | def s_formats(lang, autocaption=False): | |
545 | return [{ | |
546 | 'ext': ext, | |
86e5f3ed | 547 | 'url': f'http://localhost/video.{lang}.{ext}', |
ab84349b JMF |
548 | '_auto': autocaption, |
549 | } for ext in ['vtt', 'srt', 'ass']] | |
86e5f3ed | 550 | subtitles = {l: s_formats(l) for l in ['en', 'fr', 'es']} |
551 | auto_captions = {l: s_formats(l, True) for l in ['it', 'pt', 'es']} | |
ab84349b JMF |
552 | info_dict = { |
553 | 'id': 'test', | |
554 | 'title': 'Test', | |
555 | 'url': 'http://localhost/video.mp4', | |
556 | 'subtitles': subtitles, | |
557 | 'automatic_captions': auto_captions, | |
558 | 'extractor': 'TEST', | |
732044af | 559 | 'webpage_url': 'http://example.com/watch?v=shenanigans', |
ab84349b JMF |
560 | } |
561 | ||
562 | def get_info(params={}): | |
563 | params.setdefault('simulate', True) | |
564 | ydl = YDL(params) | |
565 | ydl.report_warning = lambda *args, **kargs: None | |
566 | return ydl.process_video_result(info_dict, download=False) | |
567 | ||
568 | result = get_info() | |
569 | self.assertFalse(result.get('requested_subtitles')) | |
570 | self.assertEqual(result['subtitles'], subtitles) | |
571 | self.assertEqual(result['automatic_captions'], auto_captions) | |
572 | ||
573 | result = get_info({'writesubtitles': True}) | |
574 | subs = result['requested_subtitles'] | |
575 | self.assertTrue(subs) | |
86e5f3ed | 576 | self.assertEqual(set(subs.keys()), {'en'}) |
ab84349b JMF |
577 | self.assertTrue(subs['en'].get('data') is None) |
578 | self.assertEqual(subs['en']['ext'], 'ass') | |
579 | ||
580 | result = get_info({'writesubtitles': True, 'subtitlesformat': 'foo/srt'}) | |
581 | subs = result['requested_subtitles'] | |
582 | self.assertEqual(subs['en']['ext'], 'srt') | |
583 | ||
584 | result = get_info({'writesubtitles': True, 'subtitleslangs': ['es', 'fr', 'it']}) | |
585 | subs = result['requested_subtitles'] | |
586 | self.assertTrue(subs) | |
86e5f3ed | 587 | self.assertEqual(set(subs.keys()), {'es', 'fr'}) |
ab84349b | 588 | |
c32b0aab | 589 | result = get_info({'writesubtitles': True, 'subtitleslangs': ['all', '-en']}) |
590 | subs = result['requested_subtitles'] | |
591 | self.assertTrue(subs) | |
86e5f3ed | 592 | self.assertEqual(set(subs.keys()), {'es', 'fr'}) |
c32b0aab | 593 | |
594 | result = get_info({'writesubtitles': True, 'subtitleslangs': ['en', 'fr', '-en']}) | |
595 | subs = result['requested_subtitles'] | |
596 | self.assertTrue(subs) | |
86e5f3ed | 597 | self.assertEqual(set(subs.keys()), {'fr'}) |
c32b0aab | 598 | |
599 | result = get_info({'writesubtitles': True, 'subtitleslangs': ['-en', 'en']}) | |
600 | subs = result['requested_subtitles'] | |
601 | self.assertTrue(subs) | |
86e5f3ed | 602 | self.assertEqual(set(subs.keys()), {'en'}) |
c32b0aab | 603 | |
604 | result = get_info({'writesubtitles': True, 'subtitleslangs': ['e.+']}) | |
605 | subs = result['requested_subtitles'] | |
606 | self.assertTrue(subs) | |
86e5f3ed | 607 | self.assertEqual(set(subs.keys()), {'es', 'en'}) |
c32b0aab | 608 | |
ab84349b JMF |
609 | result = get_info({'writesubtitles': True, 'writeautomaticsub': True, 'subtitleslangs': ['es', 'pt']}) |
610 | subs = result['requested_subtitles'] | |
611 | self.assertTrue(subs) | |
86e5f3ed | 612 | self.assertEqual(set(subs.keys()), {'es', 'pt'}) |
ab84349b JMF |
613 | self.assertFalse(subs['es']['_auto']) |
614 | self.assertTrue(subs['pt']['_auto']) | |
615 | ||
98c70d6f JMF |
616 | result = get_info({'writeautomaticsub': True, 'subtitleslangs': ['es', 'pt']}) |
617 | subs = result['requested_subtitles'] | |
618 | self.assertTrue(subs) | |
86e5f3ed | 619 | self.assertEqual(set(subs.keys()), {'es', 'pt'}) |
98c70d6f JMF |
620 | self.assertTrue(subs['es']['_auto']) |
621 | self.assertTrue(subs['pt']['_auto']) | |
622 | ||
b6c45014 JMF |
623 | def test_add_extra_info(self): |
624 | test_dict = { | |
625 | 'extractor': 'Foo', | |
626 | } | |
627 | extra_info = { | |
628 | 'extractor': 'Bar', | |
629 | 'playlist': 'funny videos', | |
630 | } | |
631 | YDL.add_extra_info(test_dict, extra_info) | |
632 | self.assertEqual(test_dict['extractor'], 'Foo') | |
633 | self.assertEqual(test_dict['playlist'], 'funny videos') | |
634 | ||
752cda38 | 635 | outtmpl_info = { |
636 | 'id': '1234', | |
637 | 'ext': 'mp4', | |
638 | 'width': None, | |
639 | 'height': 1080, | |
abbeeebc | 640 | 'filesize': 1024, |
752cda38 | 641 | 'title1': '$PATH', |
642 | 'title2': '%PATH%', | |
87ea7dfc | 643 | 'title3': 'foo/bar\\test', |
7d1eb38a | 644 | 'title4': 'foo "bar" test', |
524e2e4f | 645 | 'title5': 'áéí 𝐀', |
752cda38 | 646 | 'timestamp': 1618488000, |
647 | 'duration': 100000, | |
648 | 'playlist_index': 1, | |
e6f21b3d | 649 | 'playlist_autonumber': 2, |
0a5a191a | 650 | '__last_playlist_index': 100, |
752cda38 | 651 | 'n_entries': 10, |
07a1250e | 652 | 'formats': [ |
653 | {'id': 'id 1', 'height': 1080, 'width': 1920}, | |
654 | {'id': 'id 2', 'height': 720}, | |
655 | {'id': 'id 3'} | |
656 | ] | |
752cda38 | 657 | } |
658 | ||
5c6542ce | 659 | def test_prepare_outtmpl_and_filename(self): |
9fea350f | 660 | def test(tmpl, expected, *, info=None, **params): |
752cda38 | 661 | params['outtmpl'] = tmpl |
d2c8aadf | 662 | ydl = FakeYDL(params) |
752cda38 | 663 | ydl._num_downloads = 1 |
5c6542ce | 664 | self.assertEqual(ydl.validate_outtmpl(tmpl), None) |
752cda38 | 665 | |
819e0531 | 666 | out = ydl.evaluate_outtmpl(tmpl, info or self.outtmpl_info) |
9fea350f | 667 | fname = ydl.prepare_filename(info or self.outtmpl_info) |
5c6542ce | 668 | |
2b8a2973 | 669 | if not isinstance(expected, (list, tuple)): |
670 | expected = (expected, expected) | |
671 | for (name, got), expect in zip((('outtmpl', out), ('filename', fname)), expected): | |
672 | if callable(expect): | |
673 | self.assertTrue(expect(got), f'Wrong {name} from {tmpl}') | |
ad54c913 | 674 | elif expect is not None: |
2b8a2973 | 675 | self.assertEqual(got, expect, f'Wrong {name} from {tmpl}') |
5c6542ce | 676 | |
6e84b215 | 677 | # Side-effects |
678 | original_infodict = dict(self.outtmpl_info) | |
679 | test('foo.bar', 'foo.bar') | |
680 | original_infodict['epoch'] = self.outtmpl_info.get('epoch') | |
681 | self.assertTrue(isinstance(original_infodict['epoch'], int)) | |
682 | test('%(epoch)d', int_or_none) | |
683 | self.assertEqual(original_infodict, self.outtmpl_info) | |
684 | ||
5c6542ce | 685 | # Auto-generated fields |
686 | test('%(id)s.%(ext)s', '1234.mp4') | |
687 | test('%(duration_string)s', ('27:46:40', '27-46-40')) | |
5c6542ce | 688 | test('%(resolution)s', '1080p') |
47bcd437 | 689 | test('%(playlist_index|)s', '001') |
62b5c94c | 690 | test('%(playlist_index&{}!)s', '1!') |
e6f21b3d | 691 | test('%(playlist_autonumber)s', '02') |
5c6542ce | 692 | test('%(autonumber)s', '00001') |
693 | test('%(autonumber+2)03d', '005', autonumber_start=3) | |
694 | test('%(autonumber)s', '001', autonumber_size=3) | |
695 | ||
696 | # Escaping % | |
901130bb | 697 | test('%', '%') |
5c6542ce | 698 | test('%%', '%') |
699 | test('%%%%', '%%') | |
901130bb | 700 | test('%s', '%s') |
701 | test('%%%s', '%%s') | |
702 | test('%d', '%d') | |
703 | test('%abc%', '%abc%') | |
5c6542ce | 704 | test('%%(width)06d.%(ext)s', '%(width)06d.mp4') |
901130bb | 705 | test('%%%(height)s', '%1080') |
5c6542ce | 706 | test('%(width)06d.%(ext)s', 'NA.mp4') |
707 | test('%(width)06d.%%(ext)s', 'NA.%(ext)s') | |
708 | test('%%(width)06d.%(ext)s', '%(width)06d.mp4') | |
709 | ||
9fea350f | 710 | # ID sanitization |
711 | test('%(id)s', '_abcd', info={'id': '_abcd'}) | |
712 | test('%(some_id)s', '_abcd', info={'some_id': '_abcd'}) | |
713 | test('%(formats.0.id)s', '_abcd', info={'formats': [{'id': '_abcd'}]}) | |
714 | test('%(id)s', '-abcd', info={'id': '-abcd'}) | |
715 | test('%(id)s', '.abcd', info={'id': '.abcd'}) | |
716 | test('%(id)s', 'ab__cd', info={'id': 'ab__cd'}) | |
97d9c79e | 717 | test('%(id)s', ('ab:cd', 'ab:cd'), info={'id': 'ab:cd'}) |
e0fd9573 | 718 | test('%(id.0)s', '-', info={'id': '--'}) |
9fea350f | 719 | |
5c6542ce | 720 | # Invalid templates |
5c6542ce | 721 | self.assertTrue(isinstance(YoutubeDL.validate_outtmpl('%(title)'), ValueError)) |
722 | test('%(invalid@tmpl|def)s', 'none', outtmpl_na_placeholder='none') | |
2b8a2973 | 723 | test('%(..)s', 'NA') |
07a1250e | 724 | test('%(formats.{id)s', 'NA') |
2b8a2973 | 725 | |
726 | # Entire info_dict | |
727 | def expect_same_infodict(out): | |
728 | got_dict = json.loads(out) | |
729 | for info_field, expected in self.outtmpl_info.items(): | |
730 | self.assertEqual(got_dict.get(info_field), expected, info_field) | |
731 | return True | |
732 | ||
f9fb3ce8 | 733 | test('%()j', (expect_same_infodict, None)) |
5c6542ce | 734 | |
735 | # NA placeholder | |
752cda38 | 736 | NA_TEST_OUTTMPL = '%(uploader_date)s-%(width)d-%(x|def)s-%(id)s.%(ext)s' |
5c6542ce | 737 | test(NA_TEST_OUTTMPL, 'NA-NA-def-1234.mp4') |
738 | test(NA_TEST_OUTTMPL, 'none-none-def-1234.mp4', outtmpl_na_placeholder='none') | |
739 | test(NA_TEST_OUTTMPL, '--def-1234.mp4', outtmpl_na_placeholder='') | |
582fad70 | 740 | test('%(non_existent.0)s', 'NA') |
752cda38 | 741 | |
5c6542ce | 742 | # String formatting |
752cda38 | 743 | FMT_TEST_OUTTMPL = '%%(height)%s.%%(ext)s' |
5c6542ce | 744 | test(FMT_TEST_OUTTMPL % 's', '1080.mp4') |
745 | test(FMT_TEST_OUTTMPL % 'd', '1080.mp4') | |
746 | test(FMT_TEST_OUTTMPL % '6d', ' 1080.mp4') | |
747 | test(FMT_TEST_OUTTMPL % '-6d', '1080 .mp4') | |
748 | test(FMT_TEST_OUTTMPL % '06d', '001080.mp4') | |
749 | test(FMT_TEST_OUTTMPL % ' 06d', ' 01080.mp4') | |
750 | test(FMT_TEST_OUTTMPL % ' 06d', ' 01080.mp4') | |
751 | test(FMT_TEST_OUTTMPL % '0 6d', ' 01080.mp4') | |
752 | test(FMT_TEST_OUTTMPL % '0 6d', ' 01080.mp4') | |
753 | test(FMT_TEST_OUTTMPL % ' 0 6d', ' 01080.mp4') | |
754 | ||
755 | # Type casting | |
756 | test('%(id)d', '1234') | |
757 | test('%(height)c', '1') | |
758 | test('%(ext)c', 'm') | |
759 | test('%(id)d %(id)r', "1234 '1234'") | |
760 | test('%(id)r %(height)r', "'1234' 1080") | |
1619ab3e | 761 | test('%(title5)a %(height)a', (R"'\xe1\xe9\xed \U0001d400' 1080", None)) |
5c6542ce | 762 | test('%(ext)s-%(ext|def)d', 'mp4-def') |
ebe1b4e3 | 763 | test('%(width|0)04d', '0') |
6f2287cb | 764 | test('a%(width|b)d', 'ab', outtmpl_na_placeholder='none') |
5c6542ce | 765 | |
752cda38 | 766 | FORMATS = self.outtmpl_info['formats'] |
7d1eb38a | 767 | |
768 | # Custom type casting | |
4476d2c7 | 769 | test('%(formats.:.id)l', 'id 1, id 2, id 3') |
770 | test('%(formats.:.id)#l', ('id 1\nid 2\nid 3', 'id 1 id 2 id 3')) | |
7d1eb38a | 771 | test('%(ext)l', 'mp4') |
4476d2c7 | 772 | test('%(formats.:.id) 18l', ' id 1, id 2, id 3') |
ad54c913 | 773 | test('%(formats)j', (json.dumps(FORMATS), None)) |
774 | test('%(formats)#j', ( | |
775 | json.dumps(FORMATS, indent=4), | |
776 | json.dumps(FORMATS, indent=4).replace(':', ':').replace('"', """).replace('\n', ' ') | |
777 | )) | |
f5aa5cfb | 778 | test('%(title5).3B', 'á') |
524e2e4f | 779 | test('%(title5)U', 'áéí 𝐀') |
780 | test('%(title5)#U', 'a\u0301e\u0301i\u0301 𝐀') | |
781 | test('%(title5)+U', 'áéí A') | |
782 | test('%(title5)+#U', 'a\u0301e\u0301i\u0301 A') | |
abbeeebc | 783 | test('%(height)D', '1k') |
784 | test('%(filesize)#D', '1Ki') | |
785 | test('%(height)5.2D', ' 1.08k') | |
37893bb0 | 786 | test('%(title4)#S', 'foo_bar_test') |
97d9c79e | 787 | test('%(title4).10S', ('foo "bar" ', 'foo "bar"' + ('#' if compat_os_name == 'nt' else ' '))) |
7d1eb38a | 788 | if compat_os_name == 'nt': |
de015e93 SS |
789 | test('%(title4)q', ('"foo ""bar"" test"', None)) |
790 | test('%(formats.:.id)#q', ('"id 1" "id 2" "id 3"', None)) | |
791 | test('%(formats.0.id)#q', ('"id 1"', None)) | |
7d1eb38a | 792 | else: |
97d9c79e | 793 | test('%(title4)q', ('\'foo "bar" test\'', '\'foo "bar" test\'')) |
4476d2c7 | 794 | test('%(formats.:.id)#q', "'id 1' 'id 2' 'id 3'") |
795 | test('%(formats.0.id)#q', "'id 1'") | |
7d1eb38a | 796 | |
797 | # Internal formatting | |
5c6542ce | 798 | test('%(timestamp-1000>%H-%M-%S)s', '11-43-20') |
901130bb | 799 | test('%(title|%)s %(title|%%)s', '% %%') |
5c6542ce | 800 | test('%(id+1-height+3)05d', '00158') |
801 | test('%(width+100)05d', 'NA') | |
993edd3f | 802 | test('%(filesize*8)d', '8192') |
ad54c913 | 803 | test('%(formats.0) 15s', ('% 15s' % FORMATS[0], None)) |
804 | test('%(formats.0)r', (repr(FORMATS[0]), None)) | |
5c6542ce | 805 | test('%(height.0)03d', '001') |
806 | test('%(-height.0)04d', '-001') | |
807 | test('%(formats.-1.id)s', FORMATS[-1]['id']) | |
808 | test('%(formats.0.id.-1)d', FORMATS[0]['id'][-1]) | |
809 | test('%(formats.3)s', 'NA') | |
810 | test('%(formats.:2:-1)r', repr(FORMATS[:2:-1])) | |
811 | test('%(formats.0.id.-1+id)f', '1235.000000') | |
385a27fa | 812 | test('%(formats.0.id.-1+formats.1.id.-1)d', '3') |
07a1250e | 813 | out = json.dumps([{'id': f['id'], 'height.:2': str(f['height'])[:2]} |
814 | if 'height' in f else {'id': f['id']} | |
815 | for f in FORMATS]) | |
ad54c913 | 816 | test('%(formats.:.{id,height.:2})j', (out, None)) |
07a1250e | 817 | test('%(formats.:.{id,height}.id)l', ', '.join(f['id'] for f in FORMATS)) |
818 | test('%(.{id,title})j', ('{"id": "1234"}', '{"id": "1234"}')) | |
5c6542ce | 819 | |
7c37ff97 | 820 | # Alternates |
821 | test('%(title,id)s', '1234') | |
822 | test('%(width-100,height+20|def)d', '1100') | |
823 | test('%(width-100,height+width|def)s', 'def') | |
824 | test('%(timestamp-x>%H\\,%M\\,%S,timestamp>%H\\,%M\\,%S)s', '12,00,00') | |
825 | ||
e0fd9573 | 826 | # Replacement |
827 | test('%(id&foo)s.bar', 'foo.bar') | |
828 | test('%(title&foo)s.bar', 'NA.bar') | |
829 | test('%(title&foo|baz)s.bar', 'baz.bar') | |
34baa9fd | 830 | test('%(x,id&foo|baz)s.bar', 'foo.bar') |
831 | test('%(x,title&foo|baz)s.bar', 'baz.bar') | |
ec9311c4 | 832 | test('%(id&a\nb|)s', ('a\nb', 'a b')) |
833 | test('%(id&hi {:>10} {}|)s', 'hi 1234 1234') | |
834 | test(R'%(id&{0} {}|)s', 'NA') | |
835 | test(R'%(id&{0.1}|)s', 'NA') | |
6148833f | 836 | test('%(height&{:,d})S', '1,080') |
e0fd9573 | 837 | |
6e84b215 | 838 | # Laziness |
839 | def gen(): | |
840 | yield from range(5) | |
841 | raise self.assertTrue(False, 'LazyList should not be evaluated till here') | |
842 | test('%(key.4)s', '4', info={'key': LazyList(gen())}) | |
843 | ||
5c6542ce | 844 | # Empty filename |
845 | test('%(foo|)s-%(bar|)s.%(ext)s', '-.mp4') | |
846 | # test('%(foo|)s.%(ext)s', ('.mp4', '_.mp4')) # fixme | |
847 | # test('%(foo|)s', ('', '_')) # fixme | |
848 | ||
901130bb | 849 | # Environment variable expansion for prepare_filename |
ac668111 | 850 | os.environ['__yt_dlp_var'] = 'expanded' |
901130bb | 851 | envvar = '%__yt_dlp_var%' if compat_os_name == 'nt' else '$__yt_dlp_var' |
852 | test(envvar, (envvar, 'expanded')) | |
b836dc94 | 853 | if compat_os_name == 'nt': |
854 | test('%s%', ('%s%', '%s%')) | |
ac668111 | 855 | os.environ['s'] = 'expanded' |
b836dc94 | 856 | test('%s%', ('%s%', 'expanded')) # %s% should be expanded before escaping %s |
ac668111 | 857 | os.environ['(test)s'] = 'expanded' |
b836dc94 | 858 | test('%(test)s%', ('NA%', 'expanded')) # Environment should take priority over template |
901130bb | 859 | |
5c6542ce | 860 | # Path expansion and escaping |
861 | test('Hello %(title1)s', 'Hello $PATH') | |
862 | test('Hello %(title2)s', 'Hello %PATH%') | |
97d9c79e | 863 | test('%(title3)s', ('foo/bar\\test', 'foo⧸bar⧹test')) |
864 | test('folder/%(title3)s', ('folder/foo/bar\\test', 'folder%sfoo⧸bar⧹test' % os.path.sep)) | |
26e63931 | 865 | |
c57f7757 PH |
866 | def test_format_note(self): |
867 | ydl = YoutubeDL() | |
868 | self.assertEqual(ydl._format_note({}), '') | |
869 | assertRegexpMatches(self, ydl._format_note({ | |
870 | 'vbr': 10, | |
398dea32 | 871 | }), r'^\s*10k$') |
5d583bdf S |
872 | assertRegexpMatches(self, ydl._format_note({ |
873 | 'fps': 30, | |
398dea32 | 874 | }), r'^30fps$') |
5d583bdf | 875 | |
2b4ecde2 JMF |
876 | def test_postprocessors(self): |
877 | filename = 'post-processor-testfile.mp4' | |
878 | audiofile = filename + '.mp3' | |
879 | ||
880 | class SimplePP(PostProcessor): | |
881 | def run(self, info): | |
6f2287cb | 882 | with open(audiofile, 'w') as f: |
2b4ecde2 | 883 | f.write('EXAMPLE') |
592e97e8 | 884 | return [info['filepath']], info |
2b4ecde2 | 885 | |
592e97e8 | 886 | def run_pp(params, PP): |
6f2287cb | 887 | with open(filename, 'w') as f: |
2b4ecde2 JMF |
888 | f.write('EXAMPLE') |
889 | ydl = YoutubeDL(params) | |
592e97e8 | 890 | ydl.add_post_processor(PP()) |
2b4ecde2 JMF |
891 | ydl.post_process(filename, {'filepath': filename}) |
892 | ||
592e97e8 | 893 | run_pp({'keepvideo': True}, SimplePP) |
2b4ecde2 JMF |
894 | self.assertTrue(os.path.exists(filename), '%s doesn\'t exist' % filename) |
895 | self.assertTrue(os.path.exists(audiofile), '%s doesn\'t exist' % audiofile) | |
896 | os.unlink(filename) | |
897 | os.unlink(audiofile) | |
898 | ||
592e97e8 | 899 | run_pp({'keepvideo': False}, SimplePP) |
2b4ecde2 JMF |
900 | self.assertFalse(os.path.exists(filename), '%s exists' % filename) |
901 | self.assertTrue(os.path.exists(audiofile), '%s doesn\'t exist' % audiofile) | |
902 | os.unlink(audiofile) | |
903 | ||
592e97e8 JMF |
904 | class ModifierPP(PostProcessor): |
905 | def run(self, info): | |
6f2287cb | 906 | with open(info['filepath'], 'w') as f: |
592e97e8 JMF |
907 | f.write('MODIFIED') |
908 | return [], info | |
909 | ||
910 | run_pp({'keepvideo': False}, ModifierPP) | |
911 | self.assertTrue(os.path.exists(filename), '%s doesn\'t exist' % filename) | |
912 | os.unlink(filename) | |
913 | ||
531980d8 | 914 | def test_match_filter(self): |
531980d8 JMF |
915 | first = { |
916 | 'id': '1', | |
917 | 'url': TEST_URL, | |
918 | 'title': 'one', | |
919 | 'extractor': 'TEST', | |
920 | 'duration': 30, | |
921 | 'filesize': 10 * 1024, | |
e5a088dc | 922 | 'playlist_id': '42', |
db13c16e S |
923 | 'uploader': "變態妍字幕版 太妍 тест", |
924 | 'creator': "тест ' 123 ' тест--", | |
732044af | 925 | 'webpage_url': 'http://example.com/watch?v=shenanigans', |
531980d8 JMF |
926 | } |
927 | second = { | |
928 | 'id': '2', | |
929 | 'url': TEST_URL, | |
930 | 'title': 'two', | |
931 | 'extractor': 'TEST', | |
932 | 'duration': 10, | |
933 | 'description': 'foo', | |
934 | 'filesize': 5 * 1024, | |
e5a088dc | 935 | 'playlist_id': '43', |
db13c16e | 936 | 'uploader': "тест 123", |
732044af | 937 | 'webpage_url': 'http://example.com/watch?v=SHENANIGANS', |
531980d8 JMF |
938 | } |
939 | videos = [first, second] | |
940 | ||
941 | def get_videos(filter_=None): | |
09b49e1f | 942 | ydl = YDL({'match_filter': filter_, 'simulate': True}) |
531980d8 | 943 | for v in videos: |
104a7b5a | 944 | ydl.process_ie_result(v.copy(), download=True) |
531980d8 JMF |
945 | return [v['id'] for v in ydl.downloaded_info_dicts] |
946 | ||
947 | res = get_videos() | |
948 | self.assertEqual(res, ['1', '2']) | |
949 | ||
6db9c4d5 | 950 | def f(v, incomplete): |
531980d8 JMF |
951 | if v['id'] == '1': |
952 | return None | |
953 | else: | |
954 | return 'Video id is not 1' | |
955 | res = get_videos(f) | |
956 | self.assertEqual(res, ['1']) | |
957 | ||
958 | f = match_filter_func('duration < 30') | |
959 | res = get_videos(f) | |
960 | self.assertEqual(res, ['2']) | |
961 | ||
962 | f = match_filter_func('description = foo') | |
963 | res = get_videos(f) | |
964 | self.assertEqual(res, ['2']) | |
965 | ||
966 | f = match_filter_func('description =? foo') | |
967 | res = get_videos(f) | |
968 | self.assertEqual(res, ['1', '2']) | |
969 | ||
970 | f = match_filter_func('filesize > 5KiB') | |
971 | res = get_videos(f) | |
972 | self.assertEqual(res, ['1']) | |
973 | ||
e5a088dc S |
974 | f = match_filter_func('playlist_id = 42') |
975 | res = get_videos(f) | |
976 | self.assertEqual(res, ['1']) | |
977 | ||
db13c16e S |
978 | f = match_filter_func('uploader = "變態妍字幕版 太妍 тест"') |
979 | res = get_videos(f) | |
980 | self.assertEqual(res, ['1']) | |
981 | ||
982 | f = match_filter_func('uploader != "變態妍字幕版 太妍 тест"') | |
983 | res = get_videos(f) | |
984 | self.assertEqual(res, ['2']) | |
985 | ||
986 | f = match_filter_func('creator = "тест \' 123 \' тест--"') | |
987 | res = get_videos(f) | |
988 | self.assertEqual(res, ['1']) | |
989 | ||
990 | f = match_filter_func("creator = 'тест \\' 123 \\' тест--'") | |
991 | res = get_videos(f) | |
992 | self.assertEqual(res, ['1']) | |
993 | ||
994 | f = match_filter_func(r"creator = 'тест \' 123 \' тест--' & duration > 30") | |
995 | res = get_videos(f) | |
996 | self.assertEqual(res, []) | |
997 | ||
e9eaf3fb | 998 | def test_playlist_items_selection(self): |
7e88d7d7 | 999 | INDICES, PAGE_SIZE = list(range(1, 11)), 3 |
1000 | ||
1001 | def entry(i, evaluated): | |
1002 | evaluated.append(i) | |
1003 | return { | |
1004 | 'id': str(i), | |
1005 | 'title': str(i), | |
1006 | 'url': TEST_URL, | |
1007 | } | |
1008 | ||
1009 | def pagedlist_entries(evaluated): | |
1010 | def page_func(n): | |
1011 | start = PAGE_SIZE * n | |
1012 | for i in INDICES[start: start + PAGE_SIZE]: | |
1013 | yield entry(i, evaluated) | |
1014 | return OnDemandPagedList(page_func, PAGE_SIZE) | |
1015 | ||
1016 | def page_num(i): | |
1017 | return (i + PAGE_SIZE - 1) // PAGE_SIZE | |
e9eaf3fb | 1018 | |
7e88d7d7 | 1019 | def generator_entries(evaluated): |
1020 | for i in INDICES: | |
1021 | yield entry(i, evaluated) | |
1022 | ||
1023 | def list_entries(evaluated): | |
1024 | return list(generator_entries(evaluated)) | |
1025 | ||
1026 | def lazylist_entries(evaluated): | |
1027 | return LazyList(generator_entries(evaluated)) | |
1028 | ||
1029 | def get_downloaded_info_dicts(params, entries): | |
e9eaf3fb | 1030 | ydl = YDL(params) |
7e88d7d7 | 1031 | ydl.process_ie_result({ |
1032 | '_type': 'playlist', | |
1033 | 'id': 'test', | |
1034 | 'extractor': 'test:playlist', | |
1035 | 'extractor_key': 'test:playlist', | |
1036 | 'webpage_url': 'http://example.com', | |
1037 | 'entries': entries, | |
1038 | }) | |
4e9e1e24 S |
1039 | return ydl.downloaded_info_dicts |
1040 | ||
7e88d7d7 | 1041 | def test_selection(params, expected_ids, evaluate_all=False): |
1042 | expected_ids = list(expected_ids) | |
1043 | if evaluate_all: | |
1044 | generator_eval = pagedlist_eval = INDICES | |
1045 | elif not expected_ids: | |
1046 | generator_eval = pagedlist_eval = [] | |
1047 | else: | |
1048 | generator_eval = INDICES[0: max(expected_ids)] | |
1049 | pagedlist_eval = INDICES[PAGE_SIZE * page_num(min(expected_ids)) - PAGE_SIZE: | |
1050 | PAGE_SIZE * page_num(max(expected_ids))] | |
1051 | ||
1052 | for name, func, expected_eval in ( | |
1053 | ('list', list_entries, INDICES), | |
1054 | ('Generator', generator_entries, generator_eval), | |
7e9a6125 | 1055 | # ('LazyList', lazylist_entries, generator_eval), # Generator and LazyList follow the exact same code path |
7e88d7d7 | 1056 | ('PagedList', pagedlist_entries, pagedlist_eval), |
1057 | ): | |
1058 | evaluated = [] | |
1059 | entries = func(evaluated) | |
1060 | results = [(v['playlist_autonumber'] - 1, (int(v['id']), v['playlist_index'])) | |
1061 | for v in get_downloaded_info_dicts(params, entries)] | |
1062 | self.assertEqual(results, list(enumerate(zip(expected_ids, expected_ids))), f'Entries of {name} for {params}') | |
1063 | self.assertEqual(sorted(evaluated), expected_eval, f'Evaluation of {name} for {params}') | |
f2df4071 | 1064 | |
7e88d7d7 | 1065 | test_selection({}, INDICES) |
1066 | test_selection({'playlistend': 20}, INDICES, True) | |
1067 | test_selection({'playlistend': 2}, INDICES[:2]) | |
1068 | test_selection({'playliststart': 11}, [], True) | |
1069 | test_selection({'playliststart': 2}, INDICES[1:]) | |
1070 | test_selection({'playlist_items': '2-4'}, INDICES[1:4]) | |
9e598870 | 1071 | test_selection({'playlist_items': '2,4'}, [2, 4]) |
7e88d7d7 | 1072 | test_selection({'playlist_items': '20'}, [], True) |
ff1c7fc9 | 1073 | test_selection({'playlist_items': '0'}, []) |
cd6fc19e | 1074 | |
4e9e1e24 | 1075 | # Tests for https://github.com/ytdl-org/youtube-dl/issues/10591 |
9e598870 | 1076 | test_selection({'playlist_items': '2-4,3-4,3'}, [2, 3, 4]) |
1077 | test_selection({'playlist_items': '4,2'}, [4, 2]) | |
1078 | ||
1079 | # Tests for https://github.com/yt-dlp/yt-dlp/issues/720 | |
1080 | # https://github.com/yt-dlp/yt-dlp/issues/302 | |
7e88d7d7 | 1081 | test_selection({'playlistreverse': True}, INDICES[::-1]) |
1082 | test_selection({'playliststart': 2, 'playlistreverse': True}, INDICES[:0:-1]) | |
9e598870 | 1083 | test_selection({'playlist_items': '2,4', 'playlistreverse': True}, [4, 2]) |
1084 | test_selection({'playlist_items': '4,2'}, [4, 2]) | |
4e9e1e24 | 1085 | |
7e88d7d7 | 1086 | # Tests for --playlist-items start:end:step |
1087 | test_selection({'playlist_items': ':'}, INDICES, True) | |
1088 | test_selection({'playlist_items': '::1'}, INDICES, True) | |
1089 | test_selection({'playlist_items': '::-1'}, INDICES[::-1], True) | |
1090 | test_selection({'playlist_items': ':6'}, INDICES[:6]) | |
1091 | test_selection({'playlist_items': ':-6'}, INDICES[:-5], True) | |
1092 | test_selection({'playlist_items': '-1:6:-2'}, INDICES[:4:-2], True) | |
1093 | test_selection({'playlist_items': '9:-6:-2'}, INDICES[8:3:-2], True) | |
1094 | ||
1095 | test_selection({'playlist_items': '1:inf:2'}, INDICES[::2], True) | |
1096 | test_selection({'playlist_items': '-2:inf'}, INDICES[-2:], True) | |
1097 | test_selection({'playlist_items': ':inf:-1'}, [], True) | |
1098 | test_selection({'playlist_items': '0-2:2'}, [2]) | |
1099 | test_selection({'playlist_items': '1-:2'}, INDICES[::2], True) | |
1100 | test_selection({'playlist_items': '0--2:2'}, INDICES[1:-1:2], True) | |
1101 | ||
1102 | test_selection({'playlist_items': '10::3'}, [10], True) | |
1103 | test_selection({'playlist_items': '-1::3'}, [10], True) | |
1104 | test_selection({'playlist_items': '11::3'}, [], True) | |
1105 | test_selection({'playlist_items': '-15::2'}, INDICES[1::2], True) | |
1106 | test_selection({'playlist_items': '-15::15'}, [], True) | |
1107 | ||
b286f201 YCH |
1108 | def test_do_not_override_ie_key_in_url_transparent(self): |
1109 | ydl = YDL() | |
1110 | ||
1111 | class Foo1IE(InfoExtractor): | |
1112 | _VALID_URL = r'foo1:' | |
1113 | ||
1114 | def _real_extract(self, url): | |
1115 | return { | |
1116 | '_type': 'url_transparent', | |
1117 | 'url': 'foo2:', | |
1118 | 'ie_key': 'Foo2', | |
0396806f S |
1119 | 'title': 'foo1 title', |
1120 | 'id': 'foo1_id', | |
b286f201 YCH |
1121 | } |
1122 | ||
1123 | class Foo2IE(InfoExtractor): | |
1124 | _VALID_URL = r'foo2:' | |
1125 | ||
1126 | def _real_extract(self, url): | |
1127 | return { | |
1128 | '_type': 'url', | |
1129 | 'url': 'foo3:', | |
1130 | 'ie_key': 'Foo3', | |
1131 | } | |
1132 | ||
1133 | class Foo3IE(InfoExtractor): | |
1134 | _VALID_URL = r'foo3:' | |
1135 | ||
1136 | def _real_extract(self, url): | |
51350db5 | 1137 | return _make_result([{'url': TEST_URL}], title='foo3 title') |
b286f201 YCH |
1138 | |
1139 | ydl.add_info_extractor(Foo1IE(ydl)) | |
1140 | ydl.add_info_extractor(Foo2IE(ydl)) | |
1141 | ydl.add_info_extractor(Foo3IE(ydl)) | |
1142 | ydl.extract_info('foo1:') | |
1143 | downloaded = ydl.downloaded_info_dicts[0] | |
1144 | self.assertEqual(downloaded['url'], TEST_URL) | |
51350db5 | 1145 | self.assertEqual(downloaded['title'], 'foo1 title') |
0396806f S |
1146 | self.assertEqual(downloaded['id'], 'testid') |
1147 | self.assertEqual(downloaded['extractor'], 'testex') | |
1148 | self.assertEqual(downloaded['extractor_key'], 'TestEx') | |
b286f201 | 1149 | |
a0566bbf | 1150 | # Test case for https://github.com/ytdl-org/youtube-dl/issues/27064 |
1151 | def test_ignoreerrors_for_playlist_with_url_transparent_iterable_entries(self): | |
1152 | ||
1153 | class _YDL(YDL): | |
1154 | def __init__(self, *args, **kwargs): | |
86e5f3ed | 1155 | super().__init__(*args, **kwargs) |
a0566bbf | 1156 | |
1157 | def trouble(self, s, tb=None): | |
1158 | pass | |
1159 | ||
1160 | ydl = _YDL({ | |
1161 | 'format': 'extra', | |
1162 | 'ignoreerrors': True, | |
1163 | }) | |
1164 | ||
1165 | class VideoIE(InfoExtractor): | |
1166 | _VALID_URL = r'video:(?P<id>\d+)' | |
1167 | ||
1168 | def _real_extract(self, url): | |
1169 | video_id = self._match_id(url) | |
1170 | formats = [{ | |
1171 | 'format_id': 'default', | |
1172 | 'url': 'url:', | |
1173 | }] | |
1174 | if video_id == '0': | |
1175 | raise ExtractorError('foo') | |
1176 | if video_id == '2': | |
1177 | formats.append({ | |
1178 | 'format_id': 'extra', | |
1179 | 'url': TEST_URL, | |
1180 | }) | |
1181 | return { | |
1182 | 'id': video_id, | |
1183 | 'title': 'Video %s' % video_id, | |
1184 | 'formats': formats, | |
1185 | } | |
1186 | ||
1187 | class PlaylistIE(InfoExtractor): | |
1188 | _VALID_URL = r'playlist:' | |
1189 | ||
1190 | def _entries(self): | |
1191 | for n in range(3): | |
14f25df2 | 1192 | video_id = str(n) |
a0566bbf | 1193 | yield { |
1194 | '_type': 'url_transparent', | |
1195 | 'ie_key': VideoIE.ie_key(), | |
1196 | 'id': video_id, | |
1197 | 'url': 'video:%s' % video_id, | |
1198 | 'title': 'Video Transparent %s' % video_id, | |
1199 | } | |
1200 | ||
1201 | def _real_extract(self, url): | |
1202 | return self.playlist_result(self._entries()) | |
1203 | ||
1204 | ydl.add_info_extractor(VideoIE(ydl)) | |
1205 | ydl.add_info_extractor(PlaylistIE(ydl)) | |
1206 | info = ydl.extract_info('playlist:') | |
1207 | entries = info['entries'] | |
1208 | self.assertEqual(len(entries), 3) | |
1209 | self.assertTrue(entries[0] is None) | |
1210 | self.assertTrue(entries[1] is None) | |
1211 | self.assertEqual(len(ydl.downloaded_info_dicts), 1) | |
1212 | downloaded = ydl.downloaded_info_dicts[0] | |
9e907ebd | 1213 | entries[2].pop('requested_downloads', None) |
a0566bbf | 1214 | self.assertEqual(entries[2], downloaded) |
1215 | self.assertEqual(downloaded['url'], TEST_URL) | |
1216 | self.assertEqual(downloaded['title'], 'Video Transparent 2') | |
1217 | self.assertEqual(downloaded['id'], '2') | |
1218 | self.assertEqual(downloaded['extractor'], 'Video') | |
1219 | self.assertEqual(downloaded['extractor_key'], 'Video') | |
1220 | ||
31215122 SS |
1221 | def test_header_cookies(self): |
1222 | from http.cookiejar import Cookie | |
1223 | ||
1224 | ydl = FakeYDL() | |
1225 | ydl.report_warning = lambda *_, **__: None | |
1226 | ||
1227 | def cookie(name, value, version=None, domain='', path='', secure=False, expires=None): | |
1228 | return Cookie( | |
1229 | version or 0, name, value, None, False, | |
1230 | domain, bool(domain), bool(domain), path, bool(path), | |
1231 | secure, expires, False, None, None, rest={}) | |
1232 | ||
1233 | _test_url = 'https://yt.dlp/test' | |
1234 | ||
6c5211ce | 1235 | def test(encoded_cookies, cookies, *, headers=False, round_trip=None, error_re=None): |
31215122 SS |
1236 | def _test(): |
1237 | ydl.cookiejar.clear() | |
6c5211ce | 1238 | ydl._load_cookies(encoded_cookies, autoscope=headers) |
31215122 SS |
1239 | if headers: |
1240 | ydl._apply_header_cookies(_test_url) | |
1241 | data = {'url': _test_url} | |
1242 | ydl._calc_headers(data) | |
1243 | self.assertCountEqual( | |
1244 | map(vars, ydl.cookiejar), map(vars, cookies), | |
1245 | 'Extracted cookiejar.Cookie is not the same') | |
1246 | if not headers: | |
1247 | self.assertEqual( | |
1248 | data.get('cookies'), round_trip or encoded_cookies, | |
1249 | 'Cookie is not the same as round trip') | |
1250 | ydl.__dict__['_YoutubeDL__header_cookies'] = [] | |
1251 | ||
1252 | with self.subTest(msg=encoded_cookies): | |
6c5211ce | 1253 | if not error_re: |
31215122 SS |
1254 | _test() |
1255 | return | |
6c5211ce | 1256 | with self.assertRaisesRegex(Exception, error_re): |
31215122 SS |
1257 | _test() |
1258 | ||
1259 | test('test=value; Domain=.yt.dlp', [cookie('test', 'value', domain='.yt.dlp')]) | |
6c5211ce | 1260 | test('test=value', [cookie('test', 'value')], error_re=r'Unscoped cookies are not allowed') |
31215122 SS |
1261 | test('cookie1=value1; Domain=.yt.dlp; Path=/test; cookie2=value2; Domain=.yt.dlp; Path=/', [ |
1262 | cookie('cookie1', 'value1', domain='.yt.dlp', path='/test'), | |
1263 | cookie('cookie2', 'value2', domain='.yt.dlp', path='/')]) | |
1264 | test('test=value; Domain=.yt.dlp; Path=/test; Secure; Expires=9999999999', [ | |
1265 | cookie('test', 'value', domain='.yt.dlp', path='/test', secure=True, expires=9999999999)]) | |
1266 | test('test="value; "; path=/test; domain=.yt.dlp', [ | |
1267 | cookie('test', 'value; ', domain='.yt.dlp', path='/test')], | |
1268 | round_trip='test="value\\073 "; Domain=.yt.dlp; Path=/test') | |
1269 | test('name=; Domain=.yt.dlp', [cookie('name', '', domain='.yt.dlp')], | |
1270 | round_trip='name=""; Domain=.yt.dlp') | |
1271 | ||
1272 | test('test=value', [cookie('test', 'value', domain='.yt.dlp')], headers=True) | |
6c5211ce | 1273 | test('cookie1=value; Domain=.yt.dlp; cookie2=value', [], headers=True, error_re=r'Invalid syntax') |
31215122 | 1274 | ydl.deprecated_feature = ydl.report_error |
6c5211ce | 1275 | test('test=value', [], headers=True, error_re=r'Passing cookies as a header is a potential security risk') |
1276 | ||
1277 | def test_infojson_cookies(self): | |
1278 | TEST_FILE = 'test_infojson_cookies.info.json' | |
1279 | TEST_URL = 'https://example.com/example.mp4' | |
1280 | COOKIES = 'a=b; Domain=.example.com; c=d; Domain=.example.com' | |
1281 | COOKIE_HEADER = {'Cookie': 'a=b; c=d'} | |
1282 | ||
1283 | ydl = FakeYDL() | |
1284 | ydl.process_info = lambda x: ydl._write_info_json('test', x, TEST_FILE) | |
1285 | ||
1286 | def make_info(info_header_cookies=False, fmts_header_cookies=False, cookies_field=False): | |
1287 | fmt = {'url': TEST_URL} | |
1288 | if fmts_header_cookies: | |
1289 | fmt['http_headers'] = COOKIE_HEADER | |
1290 | if cookies_field: | |
1291 | fmt['cookies'] = COOKIES | |
1292 | return _make_result([fmt], http_headers=COOKIE_HEADER if info_header_cookies else None) | |
1293 | ||
1294 | def test(initial_info, note): | |
1295 | result = {} | |
1296 | result['processed'] = ydl.process_ie_result(initial_info) | |
1297 | self.assertTrue(ydl.cookiejar.get_cookies_for_url(TEST_URL), | |
1298 | msg=f'No cookies set in cookiejar after initial process when {note}') | |
1299 | ydl.cookiejar.clear() | |
1300 | with open(TEST_FILE) as infojson: | |
1301 | result['loaded'] = ydl.sanitize_info(json.load(infojson), True) | |
1302 | result['final'] = ydl.process_ie_result(result['loaded'].copy(), download=False) | |
1303 | self.assertTrue(ydl.cookiejar.get_cookies_for_url(TEST_URL), | |
1304 | msg=f'No cookies set in cookiejar after final process when {note}') | |
1305 | ydl.cookiejar.clear() | |
1306 | for key in ('processed', 'loaded', 'final'): | |
1307 | info = result[key] | |
1308 | self.assertIsNone( | |
1309 | traverse_obj(info, ((None, ('formats', 0)), 'http_headers', 'Cookie'), casesense=False, get_all=False), | |
1310 | msg=f'Cookie header not removed in {key} result when {note}') | |
1311 | self.assertEqual( | |
1312 | traverse_obj(info, ((None, ('formats', 0)), 'cookies'), get_all=False), COOKIES, | |
1313 | msg=f'No cookies field found in {key} result when {note}') | |
1314 | ||
1315 | test({'url': TEST_URL, 'http_headers': COOKIE_HEADER, 'id': '1', 'title': 'x'}, 'no formats field') | |
1316 | test(make_info(info_header_cookies=True), 'info_dict header cokies') | |
1317 | test(make_info(fmts_header_cookies=True), 'format header cookies') | |
1318 | test(make_info(info_header_cookies=True, fmts_header_cookies=True), 'info_dict and format header cookies') | |
1319 | test(make_info(info_header_cookies=True, fmts_header_cookies=True, cookies_field=True), 'all cookies fields') | |
1320 | test(make_info(cookies_field=True), 'cookies format field') | |
1321 | test({'url': TEST_URL, 'cookies': COOKIES, 'id': '1', 'title': 'x'}, 'info_dict cookies field only') | |
1322 | ||
1323 | try_rm(TEST_FILE) | |
1324 | ||
1325 | def test_add_headers_cookie(self): | |
1326 | def check_for_cookie_header(result): | |
1327 | return traverse_obj(result, ((None, ('formats', 0)), 'http_headers', 'Cookie'), casesense=False, get_all=False) | |
1328 | ||
1329 | ydl = FakeYDL({'http_headers': {'Cookie': 'a=b'}}) | |
1330 | ydl._apply_header_cookies(_make_result([])['webpage_url']) # Scope to input webpage URL: .example.com | |
1331 | ||
1332 | fmt = {'url': 'https://example.com/video.mp4'} | |
1333 | result = ydl.process_ie_result(_make_result([fmt]), download=False) | |
1334 | self.assertIsNone(check_for_cookie_header(result), msg='http_headers cookies in result info_dict') | |
1335 | self.assertEqual(result.get('cookies'), 'a=b; Domain=.example.com', msg='No cookies were set in cookies field') | |
1336 | self.assertIn('a=b', ydl.cookiejar.get_cookie_header(fmt['url']), msg='No cookies were set in cookiejar') | |
1337 | ||
1338 | fmt = {'url': 'https://wrong.com/video.mp4'} | |
1339 | result = ydl.process_ie_result(_make_result([fmt]), download=False) | |
1340 | self.assertIsNone(check_for_cookie_header(result), msg='http_headers cookies for wrong domain') | |
1341 | self.assertFalse(result.get('cookies'), msg='Cookies set in cookies field for wrong domain') | |
1342 | self.assertFalse(ydl.cookiejar.get_cookie_header(fmt['url']), msg='Cookies set in cookiejar for wrong domain') | |
31215122 | 1343 | |
2b4ecde2 | 1344 | |
e028d0d1 JMF |
1345 | if __name__ == '__main__': |
1346 | unittest.main() |