Find file
Fetching contributors…
Cannot retrieve contributors at this time
46 lines (38 sloc) 1.68 KB
import hashlib
from sys import stdout
from time import time
from urllib import urlretrieve
from os import getcwd, path
class download:
def __init__(self, link, file_name='', checksum='md5'):
'''Download a file, you must catch errors yourself'''
'''Just a class to download files using urllib'''
self.checksum = checksum.lower()
self.file_name = file_name = link
if (self.checksum not in ['md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512']):
self.checksum = 'md5'
if (self.file_name == ''):
if ("/" in
self.file_name ="/")[len("/"))-1]
self.file_name = link
def GetFileHash(file_name, encryption):
'''Returns the hash of one file
this is the long fonction , you can also simply do urllib.urlretrieve(link, name, hook)'''
return getattr(hashlib, encryption)(open(file_name).read(path.getsize(file_name))).hexdigest()
def hook(self, *data):
self.file_size = int(data[2]/1000)
total_packets = data[2]/data[1]
downloaded_packets = data[0]
stdout.write("\rDownload size\t= %i ko, packet: %i/%i" % (self.file_size, downloaded_packets, total_packets+1))
def get(self):
'''Download a file from web with showing progression and hash'''
timer = time()
urlretrieve(, self.file_name, self.hook)
print("\nFile name\t= %s\nETA\t\t= %i second(s)\n%s checksum\t= %s\nFile size\t= %i kb\nSaved in: %s" %
(self.file_name, int(time()-timer), self.checksum, self.GetFileHash(self.file_name, self.checksum), self.file_size, getcwd()))
return [self.file_name, int(timer-time()), self.file_size,self.checksum, self.GetFileHash(self.file_name, self.checksum)]