1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-22 02:32:33 +01:00

implement 'enumerate' for 'extractor.skip' (#306)

[ci skip]
This commit is contained in:
Mike Fährmann 2019-08-08 18:34:31 +02:00
parent bc5eaf7746
commit 8dc42bb178
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88
3 changed files with 31 additions and 6 deletions

View File

@ -131,8 +131,11 @@ extractor.*.skip
=========== =====
Type ``bool`` or ``string``
Default ``true``
Description Controls the behavior when downloading files whose filename
already exists.
Description Controls the behavior when downloading files that have been
downloaded before, i.e. a file with the same filename already
exists or its ID is in a `download archive`__.
__ `extractor.*.archive`_
* ``true``: Skip downloads
* ``false``: Overwrite already existing files
@ -144,6 +147,9 @@ Description Controls the behavior when downloading files whose filename
* ``"exit"``: Exit the program altogether
* ``"exit:N"``: Skip downloads and exit the program
after ``N`` consecutive skips
* ``"enumerate"``: Append a numeric suffix to the end of the
original filename (``file.ext.1``, ``file.ext.2``, etc)
=========== =====

View File

@ -316,7 +316,9 @@ class DownloadJob(Job):
skip = self.extractor.config("skip", True)
if skip:
self._skipexc = None
if isinstance(skip, str):
if skip == "enumerate":
self.pathfmt.check_file = self.pathfmt._enum_file
elif isinstance(skip, str):
skip, _, smax = skip.partition(":")
if skip == "abort":
self._skipexc = exception.StopExtraction

View File

@ -529,6 +529,7 @@ class PathFormat():
self.filename = ""
self.directory = self.realdirectory = ""
self.path = self.realpath = self.temppath = ""
self.suffix = ""
self.basedirectory = expand_path(
extractor.config("base-directory", (".", "gallery-dl")))
@ -565,9 +566,25 @@ class PathFormat():
if archive and archive.check(self.keywords):
return self.fix_extension()
if self.has_extension and os.path.exists(self.realpath):
return True
return self.check_file()
return False
@staticmethod
def check_file():
return True
def _enum_file(self):
num = 1
while True:
suffix = "." + str(num)
rpath = self.realpath + suffix
if not os.path.exists(rpath):
self.path += suffix
self.realpath = rpath
self.suffix = suffix
return False
num += 1
def set_directory(self, keywords):
"""Build directory path and create it if necessary"""
try:
@ -596,7 +613,7 @@ class PathFormat():
def set_keywords(self, keywords):
"""Set filename keywords"""
self.keywords = keywords
self.temppath = ""
self.temppath = self.suffix = ""
self.has_extension = bool(keywords.get("extension"))
if self.has_extension:
self.build_path()
@ -623,7 +640,7 @@ class PathFormat():
except Exception as exc:
raise exception.FormatError(exc, "filename")
filename = os.sep + self.filename
filename = os.sep + self.filename + self.suffix
self.path = self.directory + filename
self.realpath = self.realdirectory + filename
if not self.temppath: