Minor filename encoding improvement in a common case
authorPhilipp Hagemeister <phihag@phihag.de>
Tue, 27 Nov 2012 14:07:10 +0000 (15:07 +0100)
committerPhilipp Hagemeister <phihag@phihag.de>
Tue, 27 Nov 2012 14:07:10 +0000 (15:07 +0100)
test/test_utils.py
youtube_dl/utils.py

index 4208ee653243562b53fc011329907d471e76b712..e806a10c5309dea3a4bb679de6a6ac3f22c4dd91 100644 (file)
@@ -61,6 +61,13 @@ class TestUtil(unittest.TestCase):
                        for fbc in forbidden:
                                self.assertTrue(fbc not in sanitize_filename(fc, restricted=True))
 
+               # Handle a common case more neatly
+               self.assertEqual(sanitize_filename(u'大声带 - Song', restricted=True), u'Song')
+               self.assertEqual(sanitize_filename(u'总统: Speech', restricted=True), u'Speech')
+               # .. but make sure the file name is never empty
+               self.assertTrue(sanitize_filename(u'-', restricted=True) != u'')
+               self.assertTrue(sanitize_filename(u':', restricted=True) != u'')
+
        def test_ordered_set(self):
                self.assertEqual(orderedSet([1,1,2,3,4,4,5,6,7,3,5]), [1,2,3,4,5,6,7])
                self.assertEqual(orderedSet([]), [])
index 3339f56ec114fab13b39afeef2fcef60506c8ec5..4ace22c2fc232ecacef491fd6ac6ecbd0ca3df01 100644 (file)
@@ -218,6 +218,9 @@ def sanitize_filename(s, restricted=False):
        while '__' in result:
                result = result.replace('__', '_')
        result = result.strip('_')
+       # Common case of "Foreign band name - English song title"
+       if restricted and result.startswith('-_'):
+               result = result[2:]
        if not result:
                result = '_'
        return result