1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-21 18:22:30 +01:00

split 'build_path' from 'set_filename' and 'set_extension'

Do not automatically build a new path
when setting file metadata or updating its extension.
This commit is contained in:
Mike Fährmann 2022-11-08 17:01:10 +01:00
parent 39d9c362e4
commit 8124c16a50
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88
8 changed files with 53 additions and 38 deletions

View File

@ -186,16 +186,19 @@ class HttpDownloader(DownloaderBase):
size, self.maxsize) size, self.maxsize)
return False return False
build_path = False
# set missing filename extension from MIME type # set missing filename extension from MIME type
if not pathfmt.extension: if not pathfmt.extension:
pathfmt.set_extension(self._find_extension(response)) pathfmt.set_extension(self._find_extension(response))
if pathfmt.exists(): build_path = True
pathfmt.temppath = ""
return True
# set metadata from HTTP headers # set metadata from HTTP headers
if self.metadata: if self.metadata:
kwdict[self.metadata] = util.extract_headers(response) kwdict[self.metadata] = util.extract_headers(response)
build_path = True
if build_path:
pathfmt.build_path() pathfmt.build_path()
if pathfmt.exists(): if pathfmt.exists():
pathfmt.temppath = "" pathfmt.temppath = ""
@ -328,6 +331,7 @@ class HttpDownloader(DownloaderBase):
for ext, check in SIGNATURE_CHECKS.items(): for ext, check in SIGNATURE_CHECKS.items():
if check(file_header): if check(file_header):
pathfmt.set_extension(ext) pathfmt.set_extension(ext)
pathfmt.build_path()
return True return True
return False return False

View File

@ -98,6 +98,7 @@ class YoutubeDLDownloader(DownloaderBase):
pathfmt.realdirectory + filename) pathfmt.realdirectory + filename)
else: else:
pathfmt.set_extension(info_dict["ext"]) pathfmt.set_extension(info_dict["ext"])
pathfmt.build_path()
if pathfmt.exists(): if pathfmt.exists():
pathfmt.temppath = "" pathfmt.temppath = ""
@ -118,6 +119,7 @@ class YoutubeDLDownloader(DownloaderBase):
def _download_playlist(self, ytdl_instance, pathfmt, info_dict): def _download_playlist(self, ytdl_instance, pathfmt, info_dict):
pathfmt.set_extension("%(playlist_index)s.%(ext)s") pathfmt.set_extension("%(playlist_index)s.%(ext)s")
pathfmt.build_path()
self._set_outtmpl(ytdl_instance, pathfmt.realpath) self._set_outtmpl(ytdl_instance, pathfmt.realpath)
for entry in info_dict["entries"]: for entry in info_dict["entries"]:

View File

@ -232,11 +232,14 @@ class DownloadJob(Job):
self.handle_skip() self.handle_skip()
return return
if pathfmt.exists(): if pathfmt.extension and not self.metadata_http:
if archive: pathfmt.build_path()
archive.add(kwdict)
self.handle_skip() if pathfmt.exists():
return if archive:
archive.add(kwdict)
self.handle_skip()
return
if self.sleep: if self.sleep:
self.extractor.sleep(self.sleep(), "download") self.extractor.sleep(self.sleep(), "download")
@ -536,12 +539,11 @@ class SimulationJob(DownloadJob):
def handle_url(self, url, kwdict): def handle_url(self, url, kwdict):
if not kwdict["extension"]: if not kwdict["extension"]:
kwdict["extension"] = "jpg" kwdict["extension"] = "jpg"
self.pathfmt.set_filename(kwdict)
if self.sleep: if self.sleep:
self.extractor.sleep(self.sleep(), "download") self.extractor.sleep(self.sleep(), "download")
if self.archive: if self.archive:
self.archive.add(kwdict) self.archive.add(kwdict)
self.out.skip(self.pathfmt.path) self.out.skip(self.pathfmt.build_filename(kwdict))
def handle_directory(self, kwdict): def handle_directory(self, kwdict):
if not self.pathfmt: if not self.pathfmt:

View File

@ -15,16 +15,16 @@ import functools
from . import util, formatter, exception from . import util, formatter, exception
WINDOWS = util.WINDOWS WINDOWS = util.WINDOWS
EXTENSION_MAP = {
"jpeg": "jpg",
"jpe" : "jpg",
"jfif": "jpg",
"jif" : "jpg",
"jfi" : "jpg",
}
class PathFormat(): class PathFormat():
EXTENSION_MAP = {
"jpeg": "jpg",
"jpe" : "jpg",
"jfif": "jpg",
"jif" : "jpg",
"jfi" : "jpg",
}
def __init__(self, extractor): def __init__(self, extractor):
config = extractor.config config = extractor.config
@ -78,7 +78,7 @@ class PathFormat():
extension_map = config("extension-map") extension_map = config("extension-map")
if extension_map is None: if extension_map is None:
extension_map = self.EXTENSION_MAP extension_map = EXTENSION_MAP
self.extension_map = extension_map.get self.extension_map = extension_map.get
restrict = config("path-restrict", "auto") restrict = config("path-restrict", "auto")
@ -161,12 +161,14 @@ class PathFormat():
num = 1 num = 1
try: try:
while True: while True:
self.prefix = str(num) + "." prefix = format(num) + "."
self.set_extension(self.extension, False) self.kwdict["extension"] = prefix + self.extension
self.build_path()
os.stat(self.realpath) # raises OSError if file doesn't exist os.stat(self.realpath) # raises OSError if file doesn't exist
num += 1 num += 1
except OSError: except OSError:
pass pass
self.prefix = prefix
return False return False
def set_directory(self, kwdict): def set_directory(self, kwdict):
@ -198,28 +200,21 @@ class PathFormat():
def set_filename(self, kwdict): def set_filename(self, kwdict):
"""Set general filename data""" """Set general filename data"""
self.kwdict = kwdict self.kwdict = kwdict
self.temppath = self.prefix = "" self.filename = self.temppath = self.prefix = ""
ext = kwdict["extension"] ext = kwdict["extension"]
kwdict["extension"] = self.extension = self.extension_map(ext, ext) kwdict["extension"] = self.extension = self.extension_map(ext, ext)
if self.extension:
self.build_path()
else:
self.filename = ""
def set_extension(self, extension, real=True): def set_extension(self, extension, real=True):
"""Set filename extension""" """Set filename extension"""
extension = self.extension_map(extension, extension) self.extension = extension = self.extension_map(extension, extension)
if real:
self.extension = extension
self.kwdict["extension"] = self.prefix + extension self.kwdict["extension"] = self.prefix + extension
self.build_path()
def fix_extension(self, _=None): def fix_extension(self, _=None):
"""Fix filenames without a given filename extension""" """Fix filenames without a given filename extension"""
if not self.extension: if not self.extension:
self.set_extension("", False) self.kwdict["extension"] = self.prefix + self.extension_map("", "")
self.build_path()
if self.path[-1] == ".": if self.path[-1] == ".":
self.path = self.path[:-1] self.path = self.path[:-1]
self.temppath = self.realpath = self.realpath[:-1] self.temppath = self.realpath = self.realpath[:-1]
@ -296,7 +291,9 @@ class PathFormat():
if self.extension: if self.extension:
self.temppath += ".part" self.temppath += ".part"
else: else:
self.set_extension("part", False) self.kwdict["extension"] = self.prefix + self.extension_map(
"part", "part")
self.build_path()
if part_directory: if part_directory:
self.temppath = os.path.join( self.temppath = os.path.join(
part_directory, part_directory,

View File

@ -51,8 +51,9 @@ class ComparePP(PostProcessor):
num = 1 num = 1
try: try:
while not self._compare(pathfmt.realpath, pathfmt.temppath): while not self._compare(pathfmt.realpath, pathfmt.temppath):
pathfmt.prefix = str(num) + "." pathfmt.prefix = prefix = format(num) + "."
pathfmt.set_extension(pathfmt.extension, False) pathfmt.kwdict["extension"] = prefix + pathfmt.extension
pathfmt.build_path()
num += 1 num += 1
return self._equal(pathfmt) return self._equal(pathfmt)
except OSError: except OSError:

View File

@ -90,15 +90,17 @@ class UgoiraPP(PostProcessor):
if pathfmt.extension != "zip": if pathfmt.extension != "zip":
return return
if "frames" in pathfmt.kwdict: kwdict = pathfmt.kwdict
self._frames = pathfmt.kwdict["frames"] if "frames" in kwdict:
elif "pixiv_ugoira_frame_data" in pathfmt.kwdict: self._frames = kwdict["frames"]
self._frames = pathfmt.kwdict["pixiv_ugoira_frame_data"]["data"] elif "pixiv_ugoira_frame_data" in kwdict:
self._frames = kwdict["pixiv_ugoira_frame_data"]["data"]
else: else:
return return
if self.delete: if self.delete:
pathfmt.set_extension(self.extension) pathfmt.set_extension(self.extension)
pathfmt.build_path()
def convert(self, pathfmt): def convert(self, pathfmt):
if not self._frames: if not self._frames:
@ -115,6 +117,8 @@ class UgoiraPP(PostProcessor):
# process frames and collect command-line arguments # process frames and collect command-line arguments
pathfmt.set_extension(self.extension) pathfmt.set_extension(self.extension)
pathfmt.build_path()
args = self._process(pathfmt, tempdir) args = self._process(pathfmt, tempdir)
if self.args: if self.args:
args += self.args args += self.args
@ -151,6 +155,7 @@ class UgoiraPP(PostProcessor):
pathfmt.delete = True pathfmt.delete = True
else: else:
pathfmt.set_extension("zip") pathfmt.set_extension("zip")
pathfmt.build_path()
def _exec(self, args): def _exec(self, args):
self.log.debug(args) self.log.debug(args)

View File

@ -131,6 +131,7 @@ class TestDownloaderBase(unittest.TestCase):
pathfmt = cls.job.pathfmt pathfmt = cls.job.pathfmt
pathfmt.set_directory(kwdict) pathfmt.set_directory(kwdict)
pathfmt.set_filename(kwdict) pathfmt.set_filename(kwdict)
pathfmt.build_path()
if content: if content:
mode = "w" + ("b" if isinstance(content, bytes) else "") mode = "w" + ("b" if isinstance(content, bytes) else "")

View File

@ -97,6 +97,7 @@ class BasePostprocessorTest(unittest.TestCase):
self.pathfmt = self.job.pathfmt self.pathfmt = self.job.pathfmt
self.pathfmt.set_directory(kwdict) self.pathfmt.set_directory(kwdict)
self.pathfmt.set_filename(kwdict) self.pathfmt.set_filename(kwdict)
self.pathfmt.build_path()
pp = postprocessor.find(self.__class__.__name__[:-4].lower()) pp = postprocessor.find(self.__class__.__name__[:-4].lower())
return pp(self.job, options) return pp(self.job, options)
@ -118,6 +119,7 @@ class ClassifyTest(BasePostprocessorTest):
for ext in exts for ext in exts
}) })
self.pathfmt.set_extension("jpg") self.pathfmt.set_extension("jpg")
self.pathfmt.build_path()
pp.prepare(self.pathfmt) pp.prepare(self.pathfmt)
path = os.path.join(self.dir.name, "test", "Pictures") path = os.path.join(self.dir.name, "test", "Pictures")
@ -150,6 +152,7 @@ class ClassifyTest(BasePostprocessorTest):
"bar": "foo/bar", "bar": "foo/bar",
}) })
self.pathfmt.set_extension("foo") self.pathfmt.set_extension("foo")
self.pathfmt.build_path()
pp.prepare(self.pathfmt) pp.prepare(self.pathfmt)
path = os.path.join(self.dir.name, "test", "foo", "bar") path = os.path.join(self.dir.name, "test", "foo", "bar")