combine conditional filenames into filename option (#1394)
This commit is contained in:
@@ -58,12 +58,27 @@ option (see the example below).
|
|||||||
extractor.*.filename
|
extractor.*.filename
|
||||||
--------------------
|
--------------------
|
||||||
Type
|
Type
|
||||||
``string``
|
``string`` or ``object``
|
||||||
Example
|
Example
|
||||||
``"{manga}_c{chapter}_{page:>03}.{extension}"``
|
* .. code::
|
||||||
|
|
||||||
|
"{manga}_c{chapter}_{page:>03}.{extension}"
|
||||||
|
|
||||||
|
* .. code:: json
|
||||||
|
|
||||||
|
{
|
||||||
|
"extension == 'mp4'": "{id}_video.{extension}",
|
||||||
|
"'nature' in title" : "{id}_{title}.{extension}",
|
||||||
|
"" : "{id}_default.{extension}"
|
||||||
|
}
|
||||||
|
|
||||||
Description
|
Description
|
||||||
A `format string`_ to build the resulting filename
|
A `format string`_ to build filenames for downloaded files with.
|
||||||
for a downloaded file.
|
|
||||||
|
If this is an ``object``, it must contain Python expressions mapping to the
|
||||||
|
filename format strings to use.
|
||||||
|
These expressions are evaluated in the order as specified in Python 3.6+
|
||||||
|
and in an undetermined order in Python 3.4 and 3.5.
|
||||||
|
|
||||||
The available replacement keys depend on the extractor used. A list
|
The available replacement keys depend on the extractor used. A list
|
||||||
of keys for a specific one can be acquired by calling *gallery-dl*
|
of keys for a specific one can be acquired by calling *gallery-dl*
|
||||||
@@ -97,28 +112,6 @@ Description
|
|||||||
a valid filename extension.
|
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
|
extractor.*.directory
|
||||||
---------------------
|
---------------------
|
||||||
Type
|
Type
|
||||||
|
|||||||
@@ -756,10 +756,6 @@ class PathFormat():
|
|||||||
|
|
||||||
def __init__(self, extractor):
|
def __init__(self, extractor):
|
||||||
filename_fmt = extractor.config("filename")
|
filename_fmt = extractor.config("filename")
|
||||||
filename_conditions = extractor.config("filename-conditions")
|
|
||||||
if filename_fmt is None:
|
|
||||||
filename_fmt = extractor.filename_fmt
|
|
||||||
|
|
||||||
directory_fmt = extractor.config("directory")
|
directory_fmt = extractor.config("directory")
|
||||||
if directory_fmt is None:
|
if directory_fmt is None:
|
||||||
directory_fmt = extractor.directory_fmt
|
directory_fmt = extractor.directory_fmt
|
||||||
@@ -771,13 +767,16 @@ class PathFormat():
|
|||||||
|
|
||||||
kwdefault = extractor.config("keywords-default")
|
kwdefault = extractor.config("keywords-default")
|
||||||
try:
|
try:
|
||||||
if filename_conditions:
|
if filename_fmt is None:
|
||||||
self.build_filename = self.build_filename_conditional
|
filename_fmt = extractor.filename_fmt
|
||||||
|
elif isinstance(filename_fmt, dict):
|
||||||
self.filename_conditions = [
|
self.filename_conditions = [
|
||||||
(compile_expression(expr),
|
(compile_expression(expr),
|
||||||
Formatter(fmt, kwdefault).format_map)
|
Formatter(fmt, kwdefault).format_map)
|
||||||
for expr, fmt in filename_conditions.items()
|
for expr, fmt in filename_fmt.items() if expr
|
||||||
]
|
]
|
||||||
|
self.build_filename = self.build_filename_conditional
|
||||||
|
filename_fmt = filename_fmt.get("", extractor.filename_fmt)
|
||||||
|
|
||||||
self.filename_formatter = Formatter(
|
self.filename_formatter = Formatter(
|
||||||
filename_fmt, kwdefault).format_map
|
filename_fmt, kwdefault).format_map
|
||||||
|
|||||||
Reference in New Issue
Block a user