Renamed file sanitizing function.
Browse files- youtube/youtube.py +11 -18
youtube/youtube.py
CHANGED
@@ -126,7 +126,7 @@ class YouTube(object):
|
|
126 |
generated based on the name of the video.
|
127 |
"""
|
128 |
if not self._filename:
|
129 |
-
self._filename =
|
130 |
return self._filename
|
131 |
|
132 |
@filename.setter
|
@@ -282,35 +282,28 @@ class YouTube(object):
|
|
282 |
return url[0]
|
283 |
|
284 |
|
285 |
-
def
|
286 |
"""
|
287 |
Sanitizes filenames for many operating systems.
|
288 |
|
289 |
Keyword arguments:
|
290 |
text -- The unsanitized pending filename.
|
291 |
"""
|
292 |
-
#
|
293 |
truncate = lambda text: text[:max_length].rsplit(' ', 0)[0]
|
294 |
|
295 |
-
#
|
|
|
|
|
|
|
|
|
296 |
ntfs = [chr(i) for i in range(0, 31)]
|
297 |
-
|
298 |
-
#
|
|
|
299 |
paranoid = ['\"', '\#', '\$', '\%', '\'', '\*', '\,', '\.', '\/', '\:',
|
300 |
'\;', '\<', '\>', '\?', '\\', '\^', '\|', '\~', '\\\\']
|
301 |
|
302 |
blacklist = re.compile('|'.join(ntfs + paranoid), re.UNICODE)
|
303 |
filename = blacklist.sub('', text)
|
304 |
return truncate(filename)
|
305 |
-
|
306 |
-
|
307 |
-
def slugify(text):
|
308 |
-
"""
|
309 |
-
Santizes the video text, generating a valid filename.
|
310 |
-
|
311 |
-
Keyword arguments:
|
312 |
-
text -- The text corpus to make file name save.
|
313 |
-
"""
|
314 |
-
text = sanitize_filename(text)
|
315 |
-
text = text.replace('_', ' ')
|
316 |
-
return text
|
|
|
126 |
generated based on the name of the video.
|
127 |
"""
|
128 |
if not self._filename:
|
129 |
+
self._filename = mark_save(self.title)
|
130 |
return self._filename
|
131 |
|
132 |
@filename.setter
|
|
|
282 |
return url[0]
|
283 |
|
284 |
|
285 |
+
def safe_filename(text, max_length=200):
|
286 |
"""
|
287 |
Sanitizes filenames for many operating systems.
|
288 |
|
289 |
Keyword arguments:
|
290 |
text -- The unsanitized pending filename.
|
291 |
"""
|
292 |
+
#Quickly truncates long filenames.
|
293 |
truncate = lambda text: text[:max_length].rsplit(' ', 0)[0]
|
294 |
|
295 |
+
#Tidy up ugly formatted filenames.
|
296 |
+
text = text.replace('_', ' ')
|
297 |
+
text = text.replace(':', ' -')
|
298 |
+
|
299 |
+
#NTFS forbids filenames containing characters in range 0-31 (0x00-0x1F)
|
300 |
ntfs = [chr(i) for i in range(0, 31)]
|
301 |
+
|
302 |
+
#Removing these SHOULD make most filename safe for a wide range
|
303 |
+
#of operating systems.
|
304 |
paranoid = ['\"', '\#', '\$', '\%', '\'', '\*', '\,', '\.', '\/', '\:',
|
305 |
'\;', '\<', '\>', '\?', '\\', '\^', '\|', '\~', '\\\\']
|
306 |
|
307 |
blacklist = re.compile('|'.join(ntfs + paranoid), re.UNICODE)
|
308 |
filename = blacklist.sub('', text)
|
309 |
return truncate(filename)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|