]> gitweb @ CieloNegro.org - youtube-dl.git/blobdiff - youtube-dl
Do not pass URLs around in Unicode form (fixes issue #92)
[youtube-dl.git] / youtube-dl
index 2e9ac62659cc46a929f6ddcaf70163594820c2cc..fe8a591606a38c243cb1214372d90b505f41e207 100755 (executable)
@@ -27,7 +27,7 @@ except ImportError:
        from cgi import parse_qs
 
 std_headers = {
-       'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6'
+       'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6',
        'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
        'Accept': 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',
        'Accept-Language': 'en-us,en;q=0.5',
@@ -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':
-               return re.replace(ur'<>:"\|\?\*', u'-', title)
        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))
@@ -1488,7 +1504,7 @@ if __name__ == '__main__':
                # Parse command line
                parser = optparse.OptionParser(
                        usage='Usage: %prog [options] url...',
-                       version='INTERNAL',
+                       version='2010.02.13',
                        conflict_handler='resolve',
                )
 
@@ -1562,10 +1578,6 @@ if __name__ == '__main__':
                                sys.exit(u'ERROR: batch file could not be read')
                all_urls = batchurls + args
 
-               # Make sure all URLs are in our preferred encoding
-               for i in range(0, len(all_urls)):
-                       all_urls[i] = unicode(all_urls[i], preferredencoding())
-
                # Conflicting, missing and erroneous options
                if opts.usenetrc and (opts.username is not None or opts.password is not None):
                        parser.error(u'using .netrc conflicts with giving username/password')