From d3d7f015432074a88033c12dd1887ec86c8f722a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Thu, 18 Oct 2018 22:32:03 +0200 Subject: [PATCH] add 'prepare()' step for post-processors This allows post-processors to modify the destination path before checking if a file already exists. --- gallery_dl/job.py | 4 ++++ gallery_dl/postprocessor/classify.py | 13 +++++++++---- gallery_dl/postprocessor/common.py | 3 +++ gallery_dl/postprocessor/ugoira.py | 21 +++++++++++++++------ gallery_dl/util.py | 2 +- 5 files changed, 32 insertions(+), 11 deletions(-) diff --git a/gallery_dl/job.py b/gallery_dl/job.py index 12f1e5c7..3859f7d5 100644 --- a/gallery_dl/job.py +++ b/gallery_dl/job.py @@ -182,6 +182,10 @@ class DownloadJob(Job): # prepare download self.pathfmt.set_keywords(keywords) + if self.postprocessors: + for pp in self.postprocessors: + pp.prepare(self.pathfmt) + if self.pathfmt.exists(self.archive): self.handle_skip() return diff --git a/gallery_dl/postprocessor/classify.py b/gallery_dl/postprocessor/classify.py index 3af73f07..c6ed245d 100644 --- a/gallery_dl/postprocessor/classify.py +++ b/gallery_dl/postprocessor/classify.py @@ -32,13 +32,18 @@ class ClassifyPP(PostProcessor): for ext in exts } - def run(self, pathfmt): + def prepare(self, pathfmt): ext = pathfmt.keywords["extension"] if ext in self.mapping: - path = pathfmt.realdirectory + os.sep + self.mapping[ext] - pathfmt.realpath = path + os.sep + pathfmt.filename - os.makedirs(path, exist_ok=True) + self._dir = pathfmt.realdirectory + os.sep + self.mapping[ext] + pathfmt.realpath = self._dir + os.sep + pathfmt.filename + else: + self._dir = None + + def run(self, pathfmt): + if self._dir: + os.makedirs(self._dir, exist_ok=True) __postprocessor__ = ClassifyPP diff --git a/gallery_dl/postprocessor/common.py b/gallery_dl/postprocessor/common.py index af1c1ef5..c642f0f9 100644 --- a/gallery_dl/postprocessor/common.py +++ b/gallery_dl/postprocessor/common.py @@ -15,6 +15,9 @@ class PostProcessor(): """Base class for postprocessors""" log = log + def prepare(self, pathfmt): + """ """ + def run(self, pathfmt): """Execute the postprocessor for a file""" diff --git a/gallery_dl/postprocessor/ugoira.py b/gallery_dl/postprocessor/ugoira.py index 17823436..bd8c5adc 100644 --- a/gallery_dl/postprocessor/ugoira.py +++ b/gallery_dl/postprocessor/ugoira.py @@ -49,18 +49,27 @@ class UgoiraPP(PostProcessor): else: self.prevent_odd = False - def run(self, pathfmt): + def prepare(self, pathfmt): + self._frames = None + if pathfmt.keywords["extension"] != "zip": return if "frames" in pathfmt.keywords: - framelist = pathfmt.keywords["frames"] + self._frames = pathfmt.keywords["frames"] elif "pixiv_ugoira_frame_data" in pathfmt.keywords: - framelist = pathfmt.keywords["pixiv_ugoira_frame_data"]["data"] + self._frames = pathfmt.keywords["pixiv_ugoira_frame_data"]["data"] else: return - rate_in, rate_out = self.calculate_framerate(framelist) + if self.delete: + pathfmt.set_extension(self.extension) + + def run(self, pathfmt): + if not self._frames: + return + + rate_in, rate_out = self.calculate_framerate(self._frames) with tempfile.TemporaryDirectory() as tempdir: # extract frames @@ -71,13 +80,13 @@ class UgoiraPP(PostProcessor): ffconcat = tempdir + "/ffconcat.txt" with open(ffconcat, "w") as file: file.write("ffconcat version 1.0\n") - for frame in framelist: + for frame in self._frames: file.write("file '{}'\n".format(frame["file"])) file.write("duration {}\n".format(frame["delay"] / 1000)) if self.extension != "gif": # repeat the last frame to prevent it from only being # displayed for a very short amount of time - file.write("file '{}'\n".format(framelist[-1]["file"])) + file.write("file '{}'\n".format(self._frames[-1]["file"])) # collect command-line arguments args = [self.ffmpeg] diff --git a/gallery_dl/util.py b/gallery_dl/util.py index 8185dbaa..d1bba5d4 100644 --- a/gallery_dl/util.py +++ b/gallery_dl/util.py @@ -524,7 +524,7 @@ class PathFormat(): def part_enable(self, part_directory=None): """Enable .part file usage""" if self.has_extension: - self.temppath = self.realpath + ".part" + self.temppath += ".part" else: self.set_extension("part", False) if part_directory: