docstrings and other small fixes for downloaders
This commit is contained in:
@@ -1,21 +1,37 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
# Copyright 2014, 2015 Mike Fährmann
|
||||||
|
#
|
||||||
|
# This program is free software; you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU General Public License version 2 as
|
||||||
|
# published by the Free Software Foundation.
|
||||||
|
|
||||||
|
"""Common classes and constants used by downloader modules."""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
class BasicDownloader():
|
class BasicDownloader():
|
||||||
|
"""Base class for downlaoder modules"""
|
||||||
|
|
||||||
max_tries = 5
|
max_tries = 5
|
||||||
|
|
||||||
def download(self, url, path):
|
def download(self, url, path):
|
||||||
|
"""Download the resource at 'url' and write it to a file given by 'path'"""
|
||||||
with open(path, "wb") as file:
|
with open(path, "wb") as file:
|
||||||
try:
|
try:
|
||||||
return self.download_impl(url, file)
|
return self.download_impl(url, file)
|
||||||
file.close()
|
|
||||||
except:
|
except:
|
||||||
# make sure to remove file if download failed
|
# make sure to remove file if download failed
|
||||||
os.unlink(path)
|
os.unlink(path)
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
def download_impl(self, url, file_handle):
|
||||||
|
"""Actual implementaion of the download process"""
|
||||||
|
pass
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def print_error(file, error, tries, max_tries=5):
|
def print_error(file, error, tries, max_tries=5):
|
||||||
|
"""Print a message indicating an error during download"""
|
||||||
if tries == 1 and hasattr(file, "name"):
|
if tries == 1 and hasattr(file, "name"):
|
||||||
print("\r\033[1;31m", file.name, sep="")
|
print("\r\033[1;31m", file.name, sep="")
|
||||||
print("\033[0;31m[Error]\033[0m ", error, " (", tries, "/", max_tries, ")", sep="")
|
print("\033[0;31m[Error]\033[0m ", error, " (", tries, "/", max_tries, ")", sep="")
|
||||||
|
|||||||
@@ -1,3 +1,13 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
# Copyright 2014, 2015 Mike Fährmann
|
||||||
|
#
|
||||||
|
# This program is free software; you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU General Public License version 2 as
|
||||||
|
# published by the Free Software Foundation.
|
||||||
|
|
||||||
|
"""Downloader module for http urls"""
|
||||||
|
|
||||||
from .common import BasicDownloader
|
from .common import BasicDownloader
|
||||||
import time
|
import time
|
||||||
import requests
|
import requests
|
||||||
@@ -14,9 +24,9 @@ class Downloader(BasicDownloader):
|
|||||||
# try to connect to remote source
|
# try to connect to remote source
|
||||||
try:
|
try:
|
||||||
response = self.session.get(url, stream=True, verify=True)
|
response = self.session.get(url, stream=True, verify=True)
|
||||||
except requests.exceptions.ConnectionError as e:
|
except requests.exceptions.ConnectionError as exptn:
|
||||||
tries += 1
|
tries += 1
|
||||||
self.print_error(file, e, tries, self.max_tries)
|
self.print_error(file, exptn, tries, self.max_tries)
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
if tries == self.max_tries:
|
if tries == self.max_tries:
|
||||||
raise
|
raise
|
||||||
@@ -42,13 +52,16 @@ class Downloader(BasicDownloader):
|
|||||||
return tries
|
return tries
|
||||||
|
|
||||||
def set_headers(self, headers):
|
def set_headers(self, headers):
|
||||||
|
"""Set headers for http requests"""
|
||||||
self.set_dict(self.session.headers, headers)
|
self.set_dict(self.session.headers, headers)
|
||||||
|
|
||||||
def set_cookies(self, cookies):
|
def set_cookies(self, cookies):
|
||||||
|
"""Set cookies for http requests"""
|
||||||
self.set_dict(self.session.cookies, cookies)
|
self.set_dict(self.session.cookies, cookies)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def set_dict(dest, src):
|
def set_dict(dest, src):
|
||||||
|
"""Copy the contents of dictionary 'src' to 'dest'"""
|
||||||
dest.clear()
|
dest.clear()
|
||||||
dest.update(src)
|
dest.update(src)
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,13 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
# Copyright 2014, 2015 Mike Fährmann
|
||||||
|
#
|
||||||
|
# This program is free software; you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU General Public License version 2 as
|
||||||
|
# published by the Free Software Foundation.
|
||||||
|
|
||||||
|
"""Downloader module for text urls"""
|
||||||
|
|
||||||
from .common import BasicDownloader
|
from .common import BasicDownloader
|
||||||
|
|
||||||
class Downloader(BasicDownloader):
|
class Downloader(BasicDownloader):
|
||||||
|
|||||||
Reference in New Issue
Block a user