diff --git a/docs/configuration.rst b/docs/configuration.rst index 17f0cd1b..cc2dd894 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -97,6 +97,28 @@ Description a valid filename extension. +extractor.*.filename-conditions +------------------------------- +Type + ``object`` +Example + .. code:: json + + { + "extension == 'mp4'" : "{id}_video.{extension}", + "extension in ('zip','rar')": "{id}_archive.{extension}", + "'nature' in title" : "{id}_{title}.{extension}" + } +Description + An object containing Python expressions mapping to the + filename format strings to use. + + When none of the given conditions match, `extractor.*.filename`_ is used. + + Expressions are evaluated in the order as specified in Python 3.6+ + and in an undetermined order in Python 3.4 and 3.5. + + extractor.*.directory --------------------- Type diff --git a/gallery_dl/util.py b/gallery_dl/util.py index 5e22eac7..23b0fb1e 100644 --- a/gallery_dl/util.py +++ b/gallery_dl/util.py @@ -756,6 +756,7 @@ class PathFormat(): def __init__(self, extractor): filename_fmt = extractor.config("filename") + filename_conditions = extractor.config("filename-conditions") if filename_fmt is None: filename_fmt = extractor.filename_fmt @@ -770,6 +771,14 @@ class PathFormat(): kwdefault = extractor.config("keywords-default") try: + if filename_conditions: + self.build_filename = self.build_filename_conditional + self.filename_conditions = [ + (compile_expression(expr), + Formatter(fmt, kwdefault).format_map) + for expr, fmt in filename_conditions.items() + ] + self.filename_formatter = Formatter( filename_fmt, kwdefault).format_map except Exception as exc: @@ -933,6 +942,19 @@ class PathFormat(): except Exception as exc: raise exception.FilenameFormatError(exc) + def build_filename_conditional(self): + kwdict = self.kwdict + + try: + for condition, formatter in self.filename_conditions: + if condition(kwdict): + break + else: + formatter = self.filename_formatter + return self.clean_path(self.clean_segment(formatter(kwdict))) + except Exception as exc: + raise exception.FilenameFormatError(exc) + def build_path(self): """Combine directory and filename to full paths""" if self._create_directory: diff --git a/gallery_dl/version.py b/gallery_dl/version.py index 9f593f4a..33538e14 100644 --- a/gallery_dl/version.py +++ b/gallery_dl/version.py @@ -6,4 +6,4 @@ # it under the terms of the GNU General Public License version 2 as # published by the Free Software Foundation. -__version__ = "1.17.6-dev" +__version__ = "1.18.0-dev"