Set downloaded file's time stamp from last-modified header
authorGergely Imreh <imrehg@gmail.com>
Thu, 27 Jan 2011 05:02:51 +0000 (13:02 +0800)
committerGergely Imreh <imrehg@gmail.com>
Fri, 28 Jan 2011 01:46:06 +0000 (09:46 +0800)
This file stamp setting is very relaxed. If there's any problem
along the way (no last-modified header, bad time string format,
no time set privileges,...) or if nothing is downloaded (e.g. using
resumed download but the file was already complete) then nothing
is done.

youtube-dl

index be859a5a1cfe182d4b61506a4e13f4b2ef414207..e28788e3c9b463efc694d4ff23fce85c09f138cc 100755 (executable)
@@ -9,6 +9,7 @@
 import cookielib
 import ctypes
 import datetime
+import email.utils
 import gzip
 import htmlentitydefs
 import httplib
@@ -117,6 +118,14 @@ def sanitize_open(filename, open_mode):
                stream = open(filename, open_mode)
                return (stream, filename)
 
+def timeconvert(timestr):
+    """Convert RFC 2822 defined time string into system timestamp"""
+    timestamp = None
+    timetuple = email.utils.parsedate_tz(timestr)
+    if timetuple is not None:
+        timestamp = email.utils.mktime_tz(timetuple)
+    return timestamp
+
 class DownloadError(Exception):
        """Download Error exception.
 
@@ -748,6 +757,15 @@ 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
                return True
 
 class InfoExtractor(object):