Skip to content

Commit

Permalink
use Last-Modified header to set file modification time
Browse files Browse the repository at this point in the history
  • Loading branch information
mikf committed Jun 19, 2019
1 parent 179d112 commit f4ba987
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
3 changes: 3 additions & 0 deletions gallery_dl/downloader/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ def _download_impl(self, url, pathfmt):
self.downloading = False
if adj_ext:
pathfmt.set_extension(adj_ext)
filetime = response.headers.get("Last-Modified")
if filetime:
pathfmt.keywords["_filetime"] = filetime
return True

def receive(self, response, file):
Expand Down
28 changes: 18 additions & 10 deletions gallery_dl/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
import os
import sys
import json
import time
import shutil
import string
import _string
import sqlite3
import datetime
import operator
import itertools
import email.utils
import urllib.parse
from . import text, exception

Expand Down Expand Up @@ -613,17 +615,23 @@ def finalize(self):
os.unlink(self.temppath)
return

if self.temppath == self.realpath:
return

try:
os.replace(self.temppath, self.realpath)
return
except OSError:
pass
if self.temppath != self.realpath:
# move temp file to its actual location
try:
os.replace(self.temppath, self.realpath)
except OSError:
shutil.copyfile(self.temppath, self.realpath)
os.unlink(self.temppath)

shutil.copyfile(self.temppath, self.realpath)
os.unlink(self.temppath)
if "_filetime" in self.keywords:
# 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
def adjust_path(path):
Expand Down

0 comments on commit f4ba987

Please sign in to comment.