summary refs log tree commit diff
diff options
context:
space:
mode:
authorGergely Imreh <imrehg@gmail.com>2011-01-27 13:02:51 +0800
committerGergely Imreh <imrehg@gmail.com>2011-01-28 09:46:06 +0800
commit09bd408c284771224db5665d937b62d6c36759fb (patch)
tree1b454272364d6409f91b37300a17b09ccfa28658
parent9f7963468bf7f19e0cd2e11ed3ed2829f5c68b78 (diff)
downloadyoutube-dl-09bd408c284771224db5665d937b62d6c36759fb.tar.gz
youtube-dl-09bd408c284771224db5665d937b62d6c36759fb.tar.xz
youtube-dl-09bd408c284771224db5665d937b62d6c36759fb.zip
Set downloaded file's time stamp from last-modified header
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.
-rwxr-xr-xyoutube-dl18
1 files changed, 18 insertions, 0 deletions
diff --git a/youtube-dl b/youtube-dl
index be859a5a1..e28788e3c 100755
--- a/youtube-dl
+++ b/youtube-dl
@@ -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):