Skip to content

Commit

Permalink
[postprocessor:mtime] add 'value' option (#2739)
Browse files Browse the repository at this point in the history
  • Loading branch information
bradenhilton committed Jul 8, 2022
1 parent 90ae48c commit 117eeef
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
18 changes: 18 additions & 0 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3464,6 +3464,24 @@ Description
This value must either be a UNIX timestamp or a
|datetime|_ object.

Note: This option gets ignored if `mtime.value`_ is set.


mtime.value
-----------
Type
``string``
Default
``null``
Example
* ``"{status[date]}"``
* ``"{content[0:6]:R22/2022/D%Y%m%d/}"``
Description
A `format string`_ whose value should be used.

The resulting value must either be a UNIX timestamp or a
|datetime|_ object.


ugoira.extension
----------------
Expand Down
11 changes: 8 additions & 3 deletions gallery_dl/postprocessor/mtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@
"""Use metadata as file modification time"""

from .common import PostProcessor
from .. import text, util
from .. import text, util, formatter
from datetime import datetime


class MtimePP(PostProcessor):

def __init__(self, job, options):
PostProcessor.__init__(self, job)
self.key = options.get("key", "date")
value = options.get("value")
if value:
self._get = formatter.parse(value, None, util.identity).format_map
else:
key = options.get("key", "date")
self._get = lambda kwdict: kwdict.get(key)

events = options.get("event")
if events is None:
Expand All @@ -27,7 +32,7 @@ def __init__(self, job, options):
job.register_hooks({event: self.run for event in events}, options)

def run(self, pathfmt):
mtime = pathfmt.kwdict.get(self.key)
mtime = self._get(pathfmt.kwdict)
pathfmt.kwdict["_mtime"] = (
util.datetime_to_timestamp(mtime)
if isinstance(mtime, datetime) else
Expand Down
11 changes: 6 additions & 5 deletions test/test_postprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,10 +350,6 @@ def _output(mock):

class MtimeTest(BasePostprocessorTest):

def test_mtime_default(self):
pp = self._create()
self.assertEqual(pp.key, "date")

def test_mtime_datetime(self):
self._create(None, {"date": datetime(1980, 1, 1)})
self._trigger()
Expand All @@ -364,11 +360,16 @@ def test_mtime_timestamp(self):
self._trigger()
self.assertEqual(self.pathfmt.kwdict["_mtime"], 315532800)

def test_mtime_custom(self):
def test_mtime_key(self):
self._create({"key": "foo"}, {"foo": 315532800})
self._trigger()
self.assertEqual(self.pathfmt.kwdict["_mtime"], 315532800)

def test_mtime_value(self):
self._create({"value": "{foo}"}, {"foo": 315532800})
self._trigger()
self.assertEqual(self.pathfmt.kwdict["_mtime"], 315532800)


class ZipTest(BasePostprocessorTest):

Expand Down

3 comments on commit 117eeef

@ImportTaste
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This broke mtime for itaku, I haven't tried setting the new value setting yet, but if that works then the default for it needs to be changed.

@mikf
Copy link
Owner

@mikf mikf commented on 117eeef Jul 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this wasn't it.

date parsing (and mtime) never worked in the first place because I forgot a Z at the end of

post["date_added"], "%Y-%m-%dT%H:%M:%S.%f")
and at the same time didn't add a test to get notified that I made a mistake.

@mikf
Copy link
Owner

@mikf mikf commented on 117eeef Jul 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to use "value": "{date_added:D%Y-%m-%dT%H:%M:%S.%fZ}" to get it to work.

Please sign in to comment.