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