From 639f1cea9285d79c0eef4d2ec332b505c37ef34e Mon Sep 17 00:00:00 2001 From: pukkandan Date: Wed, 9 Jun 2021 14:43:51 +0530 Subject: [PATCH] Fix `%d` and empty default in outtmpl Closes #388 --- test/test_YoutubeDL.py | 7 ++++++- test/test_utils.py | 1 + yt_dlp/YoutubeDL.py | 3 ++- yt_dlp/utils.py | 2 ++ 4 files changed, 11 insertions(+), 2 deletions(-) diff --git a/test/test_YoutubeDL.py b/test/test_YoutubeDL.py index e77597d3c..0e1ab3a4a 100644 --- a/test/test_YoutubeDL.py +++ b/test/test_YoutubeDL.py @@ -735,6 +735,11 @@ class TestYoutubeDL(unittest.TestCase): ydl = YoutubeDL(params) return ydl.prepare_filename(self.outtmpl_info) + self.assertEqual(fname('%(height)06d.%(ext)s'), '001080.mp4') + self.assertEqual(fname('%(foo|)s-%(bar|)s.%(ext)s'), '-.mp4') + # self.assertEqual(fname('%(foo|)s.%(ext)s'), '_.mp4') # fixme + # self.assertEqual(fname('%(foo|)s'), '_') # fixme + self.assertEqual(fname('%%'), '%') self.assertEqual(fname('%%%%'), '%%') self.assertEqual(fname('%%(width)06d.%(ext)s'), '%(width)06d.mp4') @@ -746,7 +751,7 @@ class TestYoutubeDL(unittest.TestCase): self.assertEqual(fname('Hello %(title2)s'), 'Hello %PATH%') self.assertEqual(fname('%(title3)s'), 'foo_bar_test') - self.assertEqual(fname('%(formats.0)s'), "{'id' - 'id1'}") + self.assertEqual(fname('%(formats.0) 15s'), " {'id' - 'id1'}") self.assertEqual(fname('%(id)r %(height)r'), "'1234' 1080") self.assertEqual(fname('%(formats.0)r'), "{'id' - 'id1'}") diff --git a/test/test_utils.py b/test/test_utils.py index 04d355b4f..9ff13a369 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -126,6 +126,7 @@ class TestUtil(unittest.TestCase): self.assertTrue(timeconvert('bougrg') is None) def test_sanitize_filename(self): + self.assertEqual(sanitize_filename(''), '') self.assertEqual(sanitize_filename('abc'), 'abc') self.assertEqual(sanitize_filename('abc_d-e'), 'abc_d-e') diff --git a/yt_dlp/YoutubeDL.py b/yt_dlp/YoutubeDL.py index ad96cebcd..72fc9ad52 100644 --- a/yt_dlp/YoutubeDL.py +++ b/yt_dlp/YoutubeDL.py @@ -936,7 +936,8 @@ class YoutubeDL(object): # If value is an object, sanitize might convert it to a string # So we convert it to repr first value, fmt = repr(value), '%ss' % fmt[:-1] - value = sanitize(key, value) + if fmt[-1] in 'csr': + value = sanitize(key, value) tmpl_dict[key] = value return '%({key}){fmt}'.format(key=key, fmt=fmt) diff --git a/yt_dlp/utils.py b/yt_dlp/utils.py index 6737c1965..96f41ddd4 100644 --- a/yt_dlp/utils.py +++ b/yt_dlp/utils.py @@ -2107,6 +2107,8 @@ def sanitize_filename(s, restricted=False, is_id=False): return '_' return char + if s == '': + return '' # Handle timestamps s = re.sub(r'[0-9]+(?::[0-9]+)+', lambda m: m.group(0).replace(':', '_'), s) result = ''.join(map(replace_insane, s))