Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mikf
GitHub Repository: mikf/gallery-dl
Path: blob/master/gallery_dl/postprocessor/mtime.py
8753 views
1
# -*- coding: utf-8 -*-
2
3
# Copyright 2019-2026 Mike Fährmann
4
#
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License version 2 as
7
# published by the Free Software Foundation.
8
9
"""Use metadata as file modification time"""
10
11
from .common import PostProcessor
12
from .. import text, util, dt, formatter
13
14
15
class MtimePP(PostProcessor):
16
17
def __init__(self, job, options):
18
PostProcessor.__init__(self, job)
19
if value := options.get("value"):
20
self._get = formatter.parse(value, None, util.identity).format_map
21
else:
22
key = options.get("key", "date")
23
self._get = lambda kwdict: kwdict.get(key)
24
25
events = options.get("event")
26
if events is None:
27
events = ("file",)
28
elif isinstance(events, str):
29
events = events.split(",")
30
job.register_hooks({event: self.run for event in events}, options)
31
32
def run(self, pathfmt):
33
if mtime := self._get(pathfmt.kwdict):
34
if isinstance(mtime, dt.datetime):
35
mtime = dt.to_ts(mtime)
36
else:
37
mtime = text.parse_int(mtime)
38
else:
39
mtime = None
40
pathfmt.kwdict["_mtime_meta"] = mtime
41
42
43
__postprocessor__ = MtimePP
44
45