From 4b377ccc09f5aba729625befcd20a36debb3e036 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Tue, 1 Dec 2015 21:22:58 +0100 Subject: [PATCH] use output-module during downloads --- gallery_dl/downloader/common.py | 7 ------- gallery_dl/downloader/http.py | 11 ++++++----- gallery_dl/downloader/text.py | 2 +- gallery_dl/jobs.py | 29 ++++++----------------------- 4 files changed, 13 insertions(+), 36 deletions(-) diff --git a/gallery_dl/downloader/common.py b/gallery_dl/downloader/common.py index d88641b6..62b25e74 100644 --- a/gallery_dl/downloader/common.py +++ b/gallery_dl/downloader/common.py @@ -28,10 +28,3 @@ class BasicDownloader(): def download_impl(self, url, file_handle): """Actual implementaion of the download process""" pass - - @staticmethod - def print_error(file, error, tries, max_tries=5): - """Print a message indicating an error during download""" - if tries == 1 and hasattr(file, "name"): - print("\r\033[1;31m", file.name, sep="") - print("\033[0;31m[Error]\033[0m ", error, " (", tries, "/", max_tries, ")", sep="") diff --git a/gallery_dl/downloader/http.py b/gallery_dl/downloader/http.py index 0bca5449..ab7e4b4b 100644 --- a/gallery_dl/downloader/http.py +++ b/gallery_dl/downloader/http.py @@ -8,15 +8,16 @@ """Downloader module for http urls""" -from .common import BasicDownloader import time import requests +from .common import BasicDownloader class Downloader(BasicDownloader): - def __init__(self): + def __init__(self, printer): BasicDownloader.__init__(self) self.session = requests.session() + self.printer = printer def download_impl(self, url, file): tries = 0 @@ -26,7 +27,7 @@ class Downloader(BasicDownloader): response = self.session.get(url, stream=True, verify=True) except requests.exceptions.ConnectionError as exptn: tries += 1 - self.print_error(file, exptn, tries, self.max_tries) + self.printer.error(file, exptn, tries, self.max_tries) time.sleep(1) if tries == self.max_tries: raise @@ -35,12 +36,12 @@ class Downloader(BasicDownloader): # reject error-status-codes if response.status_code != requests.codes.ok: tries += 1 - self.print_error(file, 'HTTP status "{} {}"'.format( + self.printer.error(file, 'HTTP status "{} {}"'.format( response.status_code, response.reason), tries, self.max_tries) if response.status_code == 404: return self.max_tries time.sleep(1) - if tries == 5: + if tries == self.max_tries: response.raise_for_status() continue diff --git a/gallery_dl/downloader/text.py b/gallery_dl/downloader/text.py index 75dc9041..e47f3882 100644 --- a/gallery_dl/downloader/text.py +++ b/gallery_dl/downloader/text.py @@ -12,7 +12,7 @@ from .common import BasicDownloader class Downloader(BasicDownloader): - def __init__(self): + def __init__(self, *args): BasicDownloader.__init__(self) def download_impl(self, url, file): diff --git a/gallery_dl/jobs.py b/gallery_dl/jobs.py index 0d3d8763..8a92686e 100644 --- a/gallery_dl/jobs.py +++ b/gallery_dl/jobs.py @@ -9,7 +9,7 @@ import os import sys import tempfile -from . import config, extractor, downloader, text +from . import config, extractor, downloader, text, output from .extractor.message import Message class DownloadJob(): @@ -22,6 +22,7 @@ class DownloadJob(): self.directory = self.get_base_directory() self.downloaders = {} self.queue = None + self.printer = output.select() key = ["extractor", self.extractor.category] if self.extractor.subcategory: key.append(self.extractor.subcategory) @@ -71,12 +72,12 @@ class DownloadJob(): filename = text.clean_path(self.filename_fmt.format(**metadata)) path = os.path.join(self.directory, filename) if os.path.exists(path): - self.print_skip(path) + self.printer.skip(path) return dlinstance = self.get_downloader(url) - self.print_start(path) + self.printer.start(path) tries = dlinstance.download(url, path) - self.print_success(path, tries) + self.printer.success(path, tries) def set_directory(self, msg): """Set and create the target directory for downloads""" @@ -97,7 +98,7 @@ class DownloadJob(): instance = self.downloaders.get(scheme) if instance is None: klass = downloader.find(scheme) - instance = klass() + instance = klass(self.printer) self.downloaders[scheme] = instance return instance @@ -114,24 +115,6 @@ class DownloadJob(): bdir = config.get(("base-directory",), default=tempfile.gettempdir()) return os.path.expanduser(bdir) - @staticmethod - def print_start(path): - """Print a message indicating the start of a download""" - print(path, end="") - sys.stdout.flush() - - @staticmethod - def print_skip(path): - """Print a message indicating that a download has been skipped""" - print("\033[2m", path, "\033[0m", sep="") - - @staticmethod - def print_success(path, tries): - """Print a message indicating the completion of a download""" - if tries == 0: - print("\r", end="") - print("\r\033[1;32m", path, "\033[0m", sep="") - class KeywordJob():