From c723eefcf5ec43f44ad353ac11a8a0ff162ce23b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Wed, 8 Oct 2025 20:14:46 +0200 Subject: [PATCH] [path] implement conditional 'base-directory' --- docs/configuration.rst | 19 +++++++++++++++++- gallery_dl/path.py | 45 ++++++++++++++++++++++++++++++------------ 2 files changed, 50 insertions(+), 14 deletions(-) diff --git a/docs/configuration.rst b/docs/configuration.rst index 44fc7fbb..b215fb38 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -188,12 +188,29 @@ Description extractor.*.base-directory -------------------------- Type - |Path|_ + * |Path|_ + * ``object`` (Condition_ → |Path|_) Default ``"./gallery-dl/"`` +Example + .. code:: json + + "~/Downloads/gallery-dl" + + .. code:: json + + { + "score >= 100": "$DL", + "duration" : "$DL/video", + "" : "/tmp/files/" + } Description Directory path used as base for all download destinations. + If this is an ``object``, + it must contain Conditions_ mapping to the |Path|_ to use. + Specifying a default |Path|_ with ``""`` is required. + extractor.*.parent-directory ---------------------------- diff --git a/gallery_dl/path.py b/gallery_dl/path.py index 63816222..763fb555 100644 --- a/gallery_dl/path.py +++ b/gallery_dl/path.py @@ -118,22 +118,32 @@ class PathFormat(): if WINDOWS: self.extended = config("path-extended", True) + self.basedirectory_conditions = None basedir = extractor._parentdir if not basedir: basedir = config("base-directory") - sep = os.sep if basedir is None: - basedir = f".{sep}gallery-dl{sep}" + basedir = self.clean_path(f".{os.sep}gallery-dl{os.sep}") elif basedir: - basedir = util.expand_path(basedir) - altsep = os.altsep - if altsep and altsep in basedir: - basedir = basedir.replace(altsep, sep) - if basedir[-1] != sep: - basedir += sep - basedir = self.clean_path(basedir) + if isinstance(basedir, dict): + self.basedirectory_conditions = conds = [] + for expr, bdir in basedir.items(): + if not expr: + basedir = bdir + continue + conds.append((util.compile_filter(expr), + self._prepare_basedirectory(bdir))) + basedir = self._prepare_basedirectory(basedir) self.basedirectory = basedir + def _prepare_basedirectory(self, basedir): + basedir = util.expand_path(basedir) + if os.altsep and os.altsep in basedir: + basedir = basedir.replace(os.altsep, os.sep) + if basedir[-1] != os.sep: + basedir += os.sep + return self.clean_path(basedir) + def __str__(self): return self.realpath @@ -175,11 +185,20 @@ class PathFormat(): """Build directory path and create it if necessary""" self.kwdict = kwdict - if segments := self.build_directory(kwdict): - self.directory = directory = self.basedirectory + self.clean_path( - os.sep.join(segments) + os.sep) + if self.basedirectory_conditions is None: + basedir = self.basedirectory else: - self.directory = directory = self.basedirectory + for condition, basedir in self.basedirectory_conditions: + if condition(kwdict): + break + else: + basedir = self.basedirectory + + if segments := self.build_directory(kwdict): + self.directory = directory = \ + f"{basedir}{self.clean_path(os.sep.join(segments))}{os.sep}" + else: + self.directory = directory = basedir if WINDOWS and self.extended: directory = self._extended_path(directory)