mirror of
https://github.com/yt-dlp/yt-dlp.git
synced 2024-11-27 06:10:12 +01:00
modified filename escaping to a "smarter" one
This commit is contained in:
parent
fe4d68e196
commit
42cb53fcfa
|
@ -22,13 +22,13 @@ def test_sanitize_filename(self):
|
||||||
|
|
||||||
self.assertEqual(sanitize_filename(u'123'), u'123')
|
self.assertEqual(sanitize_filename(u'123'), u'123')
|
||||||
|
|
||||||
self.assertEqual(u'abc_de', sanitize_filename(u'abc/de'))
|
self.assertEqual(u'abc-de', sanitize_filename(u'abc/de'))
|
||||||
self.assertTrue(u'de' in sanitize_filename(u'abc/de'))
|
|
||||||
self.assertFalse(u'/' in sanitize_filename(u'abc/de///'))
|
self.assertFalse(u'/' in sanitize_filename(u'abc/de///'))
|
||||||
|
|
||||||
self.assertEqual(u'abc_de', sanitize_filename(u'abc\\de'))
|
self.assertEqual(u'abc-de', sanitize_filename(u'abc/<>\\*|de'))
|
||||||
self.assertEqual(u'abc_de', sanitize_filename(u'abc\\de'))
|
self.assertEqual(u'xxx', sanitize_filename(u'xxx/<>\\*|'))
|
||||||
self.assertTrue(u'de' in sanitize_filename(u'abc\\de'))
|
self.assertEqual(u'yes no', sanitize_filename(u'yes? no'))
|
||||||
|
self.assertEqual(u'this - that', sanitize_filename(u'this: that'))
|
||||||
|
|
||||||
self.assertEqual(sanitize_filename(u'ä'), u'ä')
|
self.assertEqual(sanitize_filename(u'ä'), u'ä')
|
||||||
self.assertEqual(sanitize_filename(u'кириллица'), u'кириллица')
|
self.assertEqual(sanitize_filename(u'кириллица'), u'кириллица')
|
||||||
|
|
|
@ -194,10 +194,20 @@ def timeconvert(timestr):
|
||||||
def sanitize_filename(s):
|
def sanitize_filename(s):
|
||||||
"""Sanitizes a string so it could be used as part of a filename."""
|
"""Sanitizes a string so it could be used as part of a filename."""
|
||||||
def replace_insane(char):
|
def replace_insane(char):
|
||||||
if char in u' .\\/|?*<>:"' or ord(char) < 32:
|
if char == '?' or ord(char) < 32 or ord(char) == 127:
|
||||||
return '_'
|
return ''
|
||||||
|
elif char == '"':
|
||||||
|
return '\''
|
||||||
|
elif char == ':':
|
||||||
|
return ' -'
|
||||||
|
elif char in '\\/|*<>':
|
||||||
|
return '-'
|
||||||
return char
|
return char
|
||||||
return u''.join(map(replace_insane, s)).strip('_')
|
|
||||||
|
result = u''.join(map(replace_insane, s))
|
||||||
|
while '--' in result:
|
||||||
|
result = result.replace('--', '-')
|
||||||
|
return result.strip('-')
|
||||||
|
|
||||||
def orderedSet(iterable):
|
def orderedSet(iterable):
|
||||||
""" Remove all duplicates from the input iterable """
|
""" Remove all duplicates from the input iterable """
|
||||||
|
|
Loading…
Reference in New Issue
Block a user