use Last-Modified header to set file modification time

(#236, #277)
pull/359/head
Mike Fährmann 5 years ago
parent 179d112083
commit f4ba98771d
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88

@ -151,6 +151,9 @@ class HttpDownloader(DownloaderBase):
self.downloading = False self.downloading = False
if adj_ext: if adj_ext:
pathfmt.set_extension(adj_ext) pathfmt.set_extension(adj_ext)
filetime = response.headers.get("Last-Modified")
if filetime:
pathfmt.keywords["_filetime"] = filetime
return True return True
def receive(self, response, file): def receive(self, response, file):

@ -12,6 +12,7 @@ import re
import os import os
import sys import sys
import json import json
import time
import shutil import shutil
import string import string
import _string import _string
@ -19,6 +20,7 @@ import sqlite3
import datetime import datetime
import operator import operator
import itertools import itertools
import email.utils
import urllib.parse import urllib.parse
from . import text, exception from . import text, exception
@ -613,17 +615,23 @@ class PathFormat():
os.unlink(self.temppath) os.unlink(self.temppath)
return return
if self.temppath == self.realpath: if self.temppath != self.realpath:
return # move temp file to its actual location
try:
try: os.replace(self.temppath, self.realpath)
os.replace(self.temppath, self.realpath) except OSError:
return shutil.copyfile(self.temppath, self.realpath)
except OSError: os.unlink(self.temppath)
pass
shutil.copyfile(self.temppath, self.realpath) if "_filetime" in self.keywords:
os.unlink(self.temppath) # try to set file times
try:
filetime = email.utils.mktime_tz(email.utils.parsedate_tz(
self.keywords["_filetime"]))
if filetime:
os.utime(self.realpath, (time.time(), filetime))
except Exception:
pass
@staticmethod @staticmethod
def adjust_path(path): def adjust_path(path):

Loading…
Cancel
Save