Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[postprocessor:mtime] add 'value' option #2739

Merged
merged 1 commit into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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