From ce2fab9c7e979419eee0952c5ec04378b1a5ce81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Thu, 9 Apr 2015 16:40:54 +0200 Subject: [PATCH] allow user-specified formats for filenames --- gallery_dl/download.py | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/gallery_dl/download.py b/gallery_dl/download.py index f73cbb72..39b2d2d9 100644 --- a/gallery_dl/download.py +++ b/gallery_dl/download.py @@ -15,11 +15,11 @@ from .extractor.common import Message class DownloadManager(): - def __init__(self, opts, conf): + def __init__(self, opts, config): self.opts = opts - self.conf = conf + self.config = config self.modules = {} - self.extractors = ExtractorFinder(conf) + self.extractors = ExtractorFinder(config) def add(self, url): job = DownloadJob(self, url) @@ -37,7 +37,7 @@ class DownloadManager(): if self.opts.dest: return self.opts.dest else: - return self.conf["general"].get("destination", "/tmp/") + return self.config.get("general", "destination", fallback="/tmp/") class DownloadJob(): @@ -47,6 +47,17 @@ class DownloadJob(): self.extractor, self.info = mngr.extractors.get_for_url(url) self.directory = mngr.get_base_directory() self.downloaders = {} + self.filename_fmt = mngr.config.get( + self.info["category"], "filename", + fallback=self.info["filename"] + ) + try: + segments = mngr.config.get( + self.info["category"], "directory" + ).split("/") + except Exception: + segments = self.info["directory"] + self.directory_fmt = os.path.join(*segments) def run(self): """Execute/Run the downlaod job""" @@ -76,7 +87,7 @@ class DownloadJob(): def download(self, msg): """Download the resource specified in 'msg'""" _, url, metadata = msg - filename = self.info["filename"].format(**metadata) + filename = self.filename_fmt.format(**metadata) path = os.path.join(self.directory, filename) if os.path.exists(path): self.print_skip(path) @@ -88,12 +99,9 @@ class DownloadJob(): def set_directory(self, msg): """Set and create the target directory for downloads""" - path = [] - for segment in self.info["directory"]: - path.append(segment.format(**msg[1])) self.directory = os.path.join( self.mngr.get_base_directory(), - *path + self.directory_fmt.format(**msg[1]) ) os.makedirs(self.directory, exist_ok=True)