Make the file timestamp feature optional
authorRicardo Garcia <sarbalap+freshmeat@gmail.com>
Fri, 28 Jan 2011 18:59:18 +0000 (19:59 +0100)
committerRicardo Garcia <sarbalap+freshmeat@gmail.com>
Fri, 28 Jan 2011 18:59:18 +0000 (19:59 +0100)
youtube-dl

index e28788e3c9b463efc694d4ff23fce85c09f138cc..05869004b8fa53e1a537b46a21b726d6b172cc97 100755 (executable)
@@ -283,6 +283,7 @@ class FileDownloader(object):
        logtostderr:      Log messages to stderr instead of stdout.
        consoletitle:     Display progress in console window's titlebar.
        nopart:           Do not use temporary .part files.
+       updatetime:       Use the Last-modified header to set output file timestamps.
        """
 
        params = None
@@ -460,6 +461,23 @@ class FileDownloader(object):
                        os.rename(old_filename, new_filename)
                except (IOError, OSError), err:
                        self.trouble(u'ERROR: unable to rename file')
+       
+       def try_utime(self, filename, last_modified_hdr):
+               """Try to set the last-modified time of the given file."""
+               if last_modified_hdr is None:
+                       return
+               if not os.path.isfile(filename):
+                       return
+               timestr = last_modified_hdr
+               if timestr is None:
+                       return
+               filetime = timeconvert(timestr)
+               if filetime is None:
+                       return
+               try:
+                       os.utime(filename,(time.time(), filetime))
+               except:
+                       pass
 
        def report_destination(self, filename):
                """Report destination filename."""
@@ -757,15 +775,11 @@ class FileDownloader(object):
                if data_len is not None and byte_counter != data_len:
                        raise ContentTooShortError(byte_counter, long(data_len))
                self.try_rename(tmpfilename, filename)
+
                # Update file modification time
-               timestr = data.info().get('last-modified', None)
-               if timestr is not None:
-                       filetime = timeconvert(timestr)
-                       if filetime is not None:
-                               try:
-                                       os.utime(filename,(time.time(), filetime))
-                               except:
-                                       pass
+               if self.params.get('updatetime', True):
+                       self.try_utime(filename, data.info().get('last-modified', None))
+
                return True
 
 class InfoExtractor(object):
@@ -2439,6 +2453,9 @@ if __name__ == '__main__':
                                dest='cookiefile', metavar='FILE', help='file to dump cookie jar to')
                filesystem.add_option('--no-part',
                                action='store_true', dest='nopart', help='do not use .part files', default=False)
+               filesystem.add_option('--no-mtime',
+                               action='store_false', dest='updatetime',
+                               help='do not use the Last-modified header to set the file modification time', default=True)
                parser.add_option_group(filesystem)
 
                (opts, args) = parser.parse_args()
@@ -2563,6 +2580,7 @@ if __name__ == '__main__':
                        'logtostderr': opts.outtmpl == '-',
                        'consoletitle': opts.consoletitle,
                        'nopart': opts.nopart,
+                       'updatetime': opts.updatetime,
                        })
                fd.add_info_extractor(youtube_search_ie)
                fd.add_info_extractor(youtube_pl_ie)