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