aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRicardo Garcia <sarbalap+freshmeat@gmail.com>2010-02-13 13:29:25 +0100
committerRicardo Garcia <sarbalap+freshmeat@gmail.com>2010-10-31 11:26:30 +0100
commit31bcb4800152ec3d7a3efb9e59018df989b11153 (patch)
tree8c554bca6aa80e1ce7a10be11f9a0034772d11cc
parentc201ebc915355b0082816844498fa56d6d3e2789 (diff)
Tweak final filename in the open attempt, to be platform and filename-agnostic
-rwxr-xr-xyoutube-dl32
1 files changed, 24 insertions, 8 deletions
diff --git a/youtube-dl b/youtube-dl
index 3533c0f55..c7d3752dc 100755
--- a/youtube-dl
+++ b/youtube-dl
@@ -78,16 +78,32 @@ def htmlentity_transform(matchobj):
return (u'&%s;' % entity)
def sanitize_title(utitle):
- """Sanitizes a video title so it could be used as part of a filename.
-
- This triggers different transformations based on the platform we
- are running.
- """
+ """Sanitizes a video title so it could be used as part of a filename."""
utitle = re.sub(ur'(?u)&(.+?);', htmlentity_transform, utitle)
- if sys.platform == 'win32':
- utitle = re.replace(ur'<>:"\|\?\*\\', u'-', utitle)
return utitle.replace(unicode(os.sep), u'%')
+def sanitize_open(filename, open_mode):
+ """Try to open the given filename, and slightly tweak it if this fails.
+
+ Attempts to open the given filename. If this fails, it tries to change
+ the filename slightly, step by step, until it's either able to open it
+ or it fails and raises a final exception, like the standard open()
+ function.
+
+ It returns the tuple (stream, definitive_file_name).
+ """
+ try:
+ stream = open(filename, open_mode)
+ return (stream, filename)
+ except (IOError, OSError), err:
+ # In case of error, try to remove win32 forbidden chars
+ filename = re.sub(ur'[<>:"\|\?\*]', u'#', filename)
+
+ # An exception here should be caught in the caller
+ stream = open(filename, open_mode)
+ return (stream, filename)
+
+
class DownloadError(Exception):
"""Download Error exception.
@@ -522,7 +538,7 @@ class FileDownloader(object):
# Open file just in time
if stream is None:
try:
- stream = open(filename, open_mode)
+ (stream, filename) = sanitize_open(filename, open_mode)
self.report_destination(filename)
except (OSError, IOError), err:
self.trouble('ERROR: unable to open for writing: %s' % str(err))