15 from threading
import Thread
19 from os.path
import exists, dirname
22 _running_downloads = {}
26 tmp_base = dest +
'.part' 27 if not exists(tmp_base):
32 tmp = tmp_base +
'.' + str(i)
41 Downloader is a thread based downloader instance when instanciated 42 it will download the provided url to a file on disk. 44 When the download is complete or failed the `.done` property will 45 be set to true and the `.status` will indicate the status code. 50 dest: Path to save data to 51 complet_action: Function to run when download is complete. 55 def __init__(self, url, dest, complete_action=None, header=None):
66 if not exists(dirname(dest)):
67 os.makedirs(dirname(dest))
75 cmd = [
'wget',
'-c', self.
url,
'-O', dest,
76 '--tries=20',
'--read-timeout=5']
78 cmd += [
'--header={}'.format(self.
header)]
79 return subprocess.call(cmd)
83 Does the actual download. 93 arg_hash = hash(self.
url + self.
dest)
96 if arg_hash
in _running_downloads:
97 _running_downloads.pop(arg_hash)
101 Move the .part file to the final destination and perform any 102 actions that should be performed at completion. 104 os.rename(tmp, self.
dest)
110 Cleanup after download attempt 113 os.remove(self.
dest +
'.part')
119 Abort download process 124 def download(url, dest, complete_action=None, header=None):
125 global _running_downloads
126 arg_hash = hash(url + dest)
127 if arg_hash
not in _running_downloads:
128 _running_downloads[arg_hash] =
Downloader(url, dest, complete_action,
130 return _running_downloads[arg_hash]
def __init__(self, url, dest, complete_action=None, header=None)
def download(url, dest, complete_action=None, header=None)
def perform_download(self, dest)
def _get_download_tmp(dest)