fix Last-Modified mtime overwriting post processor mtime (#7529)

https://github.com/mikf/gallery-dl/issues/7529#issuecomment-2989955455

- split '_mtime' into '_mtime_http' and '_mtime_meta'
- add PathFormat.set_mtime() method
This commit is contained in:
Mike Fährmann
2025-06-20 16:19:37 +02:00
parent 74c9356442
commit 9e56d81292
6 changed files with 18 additions and 19 deletions

View File

@@ -349,11 +349,11 @@ class HttpDownloader(DownloaderBase):
self.downloading = False self.downloading = False
if self.mtime: if self.mtime:
if "_http_lastmodified" in kwdict: if "_http_lastmodified" in kwdict:
kwdict["_mtime"] = kwdict["_http_lastmodified"] kwdict["_mtime_http"] = kwdict["_http_lastmodified"]
else: else:
kwdict["_mtime"] = response.headers.get("Last-Modified") kwdict["_mtime_http"] = response.headers.get("Last-Modified")
else: else:
kwdict["_mtime"] = None kwdict["_mtime_http"] = None
return True return True

View File

@@ -348,6 +348,11 @@ class PathFormat():
pass pass
return 0 return 0
def set_mtime(self, path=None):
if (mtime := (self.kwdict.get("_mtime_meta") or
self.kwdict.get("_mtime_http"))):
util.set_mtime(self.realpath if path is None else path, mtime)
def finalize(self): def finalize(self):
"""Move tempfile to its target location""" """Move tempfile to its target location"""
if self.delete: if self.delete:
@@ -381,6 +386,4 @@ class PathFormat():
os.unlink(self.temppath) os.unlink(self.temppath)
break break
mtime = self.kwdict.get("_mtime") self.set_mtime()
if mtime:
util.set_mtime(self.realpath, mtime)

View File

@@ -139,9 +139,7 @@ class MetadataPP(PostProcessor):
archive.add(pathfmt.kwdict) archive.add(pathfmt.kwdict)
if self.mtime: if self.mtime:
mtime = pathfmt.kwdict.get("_mtime") pathfmt.set_mtime(path)
if mtime:
util.set_mtime(path, mtime)
def _run_stdout(self, pathfmt): def _run_stdout(self, pathfmt):
self.write(sys.stdout, pathfmt.kwdict) self.write(sys.stdout, pathfmt.kwdict)

View File

@@ -36,7 +36,7 @@ class MtimePP(PostProcessor):
if mtime is None: if mtime is None:
return return
pathfmt.kwdict["_mtime"] = ( pathfmt.kwdict["_mtime_meta"] = (
util.datetime_to_timestamp(mtime) util.datetime_to_timestamp(mtime)
if isinstance(mtime, datetime) else if isinstance(mtime, datetime) else
text.parse_int(mtime) text.parse_int(mtime)

View File

@@ -236,9 +236,7 @@ class UgoiraPP(PostProcessor):
pathfmt.realpath = pathfmt.temppath pathfmt.realpath = pathfmt.temppath
else: else:
if self.mtime: if self.mtime:
mtime = pathfmt.kwdict.get("_mtime") pathfmt.set_mtime()
if mtime:
util.set_mtime(pathfmt.realpath, mtime)
return True return True
def convert_to_archive(self, pathfmt, tempdir): def convert_to_archive(self, pathfmt, tempdir):

View File

@@ -756,32 +756,32 @@ class MtimeTest(BasePostprocessorTest):
def test_mtime_datetime(self): def test_mtime_datetime(self):
self._create(None, {"date": datetime(1980, 1, 1)}) self._create(None, {"date": datetime(1980, 1, 1)})
self._trigger() self._trigger()
self.assertEqual(self.pathfmt.kwdict["_mtime"], 315532800) self.assertEqual(self.pathfmt.kwdict["_mtime_meta"], 315532800)
def test_mtime_timestamp(self): def test_mtime_timestamp(self):
self._create(None, {"date": 315532800}) self._create(None, {"date": 315532800})
self._trigger() self._trigger()
self.assertEqual(self.pathfmt.kwdict["_mtime"], 315532800) self.assertEqual(self.pathfmt.kwdict["_mtime_meta"], 315532800)
def test_mtime_none(self): def test_mtime_none(self):
self._create(None, {"date": None}) self._create(None, {"date": None})
self._trigger() self._trigger()
self.assertNotIn("_mtime", self.pathfmt.kwdict) self.assertNotIn("_mtime_meta", self.pathfmt.kwdict)
def test_mtime_undefined(self): def test_mtime_undefined(self):
self._create(None, {}) self._create(None, {})
self._trigger() self._trigger()
self.assertNotIn("_mtime", self.pathfmt.kwdict) self.assertNotIn("_mtime_meta", self.pathfmt.kwdict)
def test_mtime_key(self): def test_mtime_key(self):
self._create({"key": "foo"}, {"foo": 315532800}) self._create({"key": "foo"}, {"foo": 315532800})
self._trigger() self._trigger()
self.assertEqual(self.pathfmt.kwdict["_mtime"], 315532800) self.assertEqual(self.pathfmt.kwdict["_mtime_meta"], 315532800)
def test_mtime_value(self): def test_mtime_value(self):
self._create({"value": "{foo}"}, {"foo": 315532800}) self._create({"value": "{foo}"}, {"foo": 315532800})
self._trigger() self._trigger()
self.assertEqual(self.pathfmt.kwdict["_mtime"], 315532800) self.assertEqual(self.pathfmt.kwdict["_mtime_meta"], 315532800)
class PythonTest(BasePostprocessorTest): class PythonTest(BasePostprocessorTest):