1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-25 12:12:34 +01:00

use output-module during downloads

This commit is contained in:
Mike Fährmann 2015-12-01 21:22:58 +01:00
parent c786843a6f
commit 4b377ccc09
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88
4 changed files with 13 additions and 36 deletions

View File

@ -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="")

View File

@ -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

View File

@ -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):

View File

@ -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():