A feature-rich command-line audio/video downloader
[YoutubeDL] Don't expand env variables in meta fields (closes #13637)
Sergey M․15da37c7dc8c
diff --git a/test/test_YoutubeDL.py b/test/test_YoutubeDL.pyindex 75945e38f8..70989e2322 100644--- a/test/test_YoutubeDL.py+++ b/test/test_YoutubeDL.py@@ -527,6 +527,8 @@ class TestYoutubeDL(unittest.TestCase):'ext': 'mp4','width': None,'height': 1080,+ 'title1': '$PATH',+ 'title2': '%PATH%',}def fname(templ):@@ -545,10 +547,14 @@ class TestYoutubeDL(unittest.TestCase):self.assertEqual(fname('%(height)0 6d.%(ext)s'), ' 01080.mp4')self.assertEqual(fname('%(height)0 6d.%(ext)s'), ' 01080.mp4')self.assertEqual(fname('%(height) 0 6d.%(ext)s'), ' 01080.mp4')+ self.assertEqual(fname('%%'), '%')+ self.assertEqual(fname('%%%%'), '%%')self.assertEqual(fname('%%(height)06d.%(ext)s'), '%(height)06d.mp4')self.assertEqual(fname('%(width)06d.%(ext)s'), 'NA.mp4')self.assertEqual(fname('%(width)06d.%%(ext)s'), 'NA.%(ext)s')self.assertEqual(fname('%%(width)06d.%(ext)s'), '%(width)06d.mp4')+ self.assertEqual(fname('Hello %(title1)s'), 'Hello $PATH')+ self.assertEqual(fname('Hello %(title2)s'), 'Hello %PATH%')def test_format_note(self):ydl = YoutubeDL()diff --git a/youtube_dl/YoutubeDL.py b/youtube_dl/YoutubeDL.pyindex 60ee4b7d81..8730d32efc 100755--- a/youtube_dl/YoutubeDL.py+++ b/youtube_dl/YoutubeDL.py@@ -20,6 +20,7 @@ import reimport shutilimport subprocessimport socket+import stringimport sysimport timeimport tokenize@@ -674,7 +675,19 @@ class YoutubeDL(object):FORMAT_RE.format(numeric_field),r'%({0})s'.format(numeric_field), outtmpl)- filename = expand_path(outtmpl % template_dict)+ # expand_path translates '%%' into '%' and '$$' into '$'+ # correspondingly that is not what we want since we need to keep+ # '%%' intact for template dict substitution step. Working around+ # with boundary-alike separator hack.+ sep = ''.join([random.choice(string.ascii_letters) for _ in range(32)])+ outtmpl = outtmpl.replace('%%', '%{0}%'.format(sep)).replace('$$', '${0}$'.format(sep))++ # outtmpl should be expand_path'ed before template dict substitution+ # because meta fields may contain env variables we don't want to+ # be expanded. For example, for outtmpl "%(title)s.%(ext)s" and+ # title "Hello $PATH", we don't want `$PATH` to be expanded.+ filename = expand_path(outtmpl).replace(sep, '') % template_dict+# Temporary fix for #4787# 'Treat' all problem characters by passing filename through preferredencoding# to workaround encoding issues with subprocess on python2 @ Windows