From 56f1c9616852e60c51fbf57968045c4765f20849 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Wed, 29 Jan 2020 18:32:37 +0100 Subject: [PATCH] implement 'parent-directory' option (#551) --- docs/configuration.rst | 11 +++++++++++ gallery_dl/extractor/common.py | 1 + gallery_dl/extractor/reddit.py | 3 +++ gallery_dl/job.py | 9 ++++++++- gallery_dl/util.py | 14 ++++++++------ 5 files changed, 31 insertions(+), 7 deletions(-) diff --git a/docs/configuration.rst b/docs/configuration.rst index c053c82f..309f4dd2 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -108,6 +108,17 @@ Description Directory path used as the base for all download destinations. =========== ===== +extractor.*.parent-directory +---------------------------- +=========== ===== +Type ``bool`` +Default ``false`` +Description Use an extractor's current target directory as + `base-directory `__ + for any spawned child extractors. +=========== ===== + + extractor.*.path-restrict ------------------------- =========== ===== diff --git a/gallery_dl/extractor/common.py b/gallery_dl/extractor/common.py index 80a336de..fc1bfcaf 100644 --- a/gallery_dl/extractor/common.py +++ b/gallery_dl/extractor/common.py @@ -39,6 +39,7 @@ class Extractor(): self._cookiefile = None self._cookiejar = self.session.cookies + self._parentdir = "" self._retries = self.config("retries", 4) self._timeout = self.config("timeout", 30) self._verify = self.config("verify", True) diff --git a/gallery_dl/extractor/reddit.py b/gallery_dl/extractor/reddit.py index 44627d5a..4df955b2 100644 --- a/gallery_dl/extractor/reddit.py +++ b/gallery_dl/extractor/reddit.py @@ -31,6 +31,7 @@ class RedditExtractor(Extractor): match_subreddit = RedditSubredditExtractor.pattern.match match_user = RedditUserExtractor.pattern.match + parentdir = self.config("parent-directory") submissions = self.submissions() visited = set() depth = 0 @@ -56,6 +57,8 @@ class RedditExtractor(Extractor): yield Message.Url, "ytdl:" + url, submission elif not submission["is_self"]: urls.append((url, submission)) + elif parentdir: + yield Message.Directory, comments[0] if self.api.comments: if submission: diff --git a/gallery_dl/job.py b/gallery_dl/job.py index c717dc23..6ba2572a 100644 --- a/gallery_dl/job.py +++ b/gallery_dl/job.py @@ -182,7 +182,14 @@ class DownloadJob(Job): self.downloaders = {} self.postprocessors = None self.out = output.select() - self.visited = parent.visited if parent else set() + + if parent: + self.visited = parent.visited + pfmt = parent.pathfmt + if pfmt and parent.extractor.config("parent-directory"): + self.extractor._parentdir = pfmt.directory + else: + self.visited = set() def handle_url(self, url, kwdict, fallback=None): """Download the resource specified in 'url'""" diff --git a/gallery_dl/util.py b/gallery_dl/util.py index cfa4b070..1c51ea5c 100644 --- a/gallery_dl/util.py +++ b/gallery_dl/util.py @@ -627,12 +627,14 @@ class PathFormat(): self.delete = False self.path = self.realpath = self.temppath = "" - basedir = expand_path( - extractor.config("base-directory", (".", "gallery-dl"))) - if os.altsep and os.altsep in basedir: - basedir = basedir.replace(os.altsep, os.sep) - if basedir[-1] != os.sep: - basedir += os.sep + basedir = extractor._parentdir + if not basedir: + basedir = expand_path( + extractor.config("base-directory", (".", "gallery-dl"))) + if os.altsep and os.altsep in basedir: + basedir = basedir.replace(os.altsep, os.sep) + if basedir[-1] != os.sep: + basedir += os.sep self.basedirectory = basedir restrict = extractor.config("path-restrict", "auto")