Skip to content

Instantly share code, notes, and snippets.

@mouuff
Created June 26, 2012 07:40
Show Gist options
  • Save mouuff/2994160 to your computer and use it in GitHub Desktop.
Save mouuff/2994160 to your computer and use it in GitHub Desktop.
a python web downloader with statut
from urllib import urlretrieve
from sys import stdout, argv
from time import time
from os import getcwd, path
import hashlib
class download:
def __init__(self, link, file_name='', checksum='md5'):
self.checksum = checksum.lower()
self.file_name = file_name
self.link = link
if (self.checksum not in ['md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512']):
self.checksum = 'md5'
if (self.file_name == ''):
if ("/" in self.link):
self.file_name = self.link.split("/")[len(self.link.split("/"))-1]
else:
self.file_name = link
@staticmethod
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))
stdout.flush()
def get(self):
'''Download a file from web with showing progression and hash'''
timer = time()
urlretrieve(self.link, 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.checksum, self.GetFileHash(self.file_name, self.checksum)]
def main():
try:
link = argv[1]
except IndexError:
print("You also can direct download with\npython %s http://link/" % argv[0])
link = raw_input("Link : ")
try:
print("Connecting to: %s" % link)
download(link).get()
except IOError:
print("\nError")
return 0
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment