diff options
author | Philipp Hagemeister <[email protected]> | 2012-12-03 15:36:24 +0100 |
---|---|---|
committer | Philipp Hagemeister <[email protected]> | 2012-12-03 15:36:41 +0100 |
commit | 796173d08b514182eedc704541eb55d5c9e1dc0d (patch) | |
tree | fc25c18ca5724bfdf486106fe58dfa1d1c693d5c /youtube_dl/utils.py | |
parent | e575b6821ec185ef4f14c433ccc018d748c0fb60 (diff) | |
download | youtube-dl-796173d08b514182eedc704541eb55d5c9e1dc0d.tar.gz youtube-dl-796173d08b514182eedc704541eb55d5c9e1dc0d.zip |
Keep video IDs verbatim if possible (Closes #571)
Diffstat (limited to 'youtube_dl/utils.py')
-rw-r--r-- | youtube_dl/utils.py | 20 |
1 files changed, 11 insertions, 9 deletions
diff --git a/youtube_dl/utils.py b/youtube_dl/utils.py index 7f73b8476..4dcf18991 100644 --- a/youtube_dl/utils.py +++ b/youtube_dl/utils.py @@ -317,9 +317,10 @@ def timeconvert(timestr): timestamp = email.utils.mktime_tz(timetuple) return timestamp -def sanitize_filename(s, restricted=False): +def sanitize_filename(s, restricted=False, is_id=False): """Sanitizes a string so it could be used as part of a filename. If restricted is set, use a stricter subset of allowed characters. + Set is_id if this is not an arbitrary string, but an ID that should be kept if possible """ def replace_insane(char): if char == '?' or ord(char) < 32 or ord(char) == 127: @@ -337,14 +338,15 @@ def sanitize_filename(s, restricted=False): return char result = u''.join(map(replace_insane, s)) - 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 = '_' + if not is_id: + 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 def orderedSet(iterable): |