Created
November 30, 2016 04:40
-
-
Save amitbhoraniya/0eb13d393a51b847f2d548a0f681773a to your computer and use it in GitHub Desktop.
Python Download
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys, os, tempfile, logging,shutil | |
import urllib.request as urllib2 | |
import urllib.parse as urlparse | |
import threading | |
import time | |
maxThreads = 64 | |
block_sz = 16 * 1024 | |
class FilePartThread(threading.Thread): | |
def __init__(self,threadID,url,fileName,startIndex,endIndex): | |
threading.Thread.__init__(self) | |
self.threadID = threadID | |
self.url = url | |
self.fileName = fileName | |
self.startIndex = startIndex | |
self.endIndex = endIndex | |
def run(self): | |
print ("Starting " + self.fileName + '[ {0} to {1}]'.format(self.startIndex,self.endIndex)) | |
self.download_part() | |
print ("Exiting " + self.fileName) | |
def download_part(self): | |
#u1 = urllib2.urlopen(url) | |
#meta = u1.info() | |
#meta_func = meta.getheaders if hasattr(meta, 'getheaders') else meta.get_all | |
#meta_length = meta_func("Content-Length") | |
#print(dict(meta)) | |
#print(meta_length) | |
req = urllib2.Request(self.url) | |
req.headers['Range'] = 'bytes=%s-%s' % (self.startIndex,self.endIndex) | |
u = urllib2.urlopen(req) | |
meta = u.info() | |
meta_func = meta.getheaders if hasattr(meta, 'getheaders') else meta.get_all | |
meta_length = meta_func("Content-Length") | |
file_size = None | |
if meta_length: | |
file_size = int(meta_length[0]) | |
print("Downloading {0}: {1} Bytes: {2}".format(self.fileName,self.url, file_size)) | |
file_size_dl = 0 | |
with open(self.fileName, 'wb') as f: | |
shutil.copyfileobj(u,f,block_sz) | |
# buffer = u.read(block_sz) | |
# if not buffer: | |
# break | |
# file_size_dl += len(buffer) | |
# f.write(buffer) | |
# status = "{0:16}".format(file_size_dl) | |
# if file_size: | |
# status += " [{0:6.2f}%] {1}".format(file_size_dl * 100 / file_size,self.fileName) | |
# status += chr(13) | |
# print(status, end="") | |
def download_file(url, desc=None): | |
u = urllib2.urlopen(url) | |
meta = u.info() | |
scheme, netloc, path, query, fragment = urlparse.urlsplit(u.geturl()) | |
filename = os.path.basename(path) | |
filename = urllib2.unquote(filename) | |
if not filename: | |
filename = 'downloaded.file' | |
if desc: | |
filename = os.path.join(desc, filename) | |
meta_func = meta.getheaders if hasattr(meta, 'getheaders') else meta.get_all | |
meta_length = meta_func("Content-Length") | |
print(meta_length[0]) | |
threads = [] | |
index = 0 | |
i = 0 | |
while i < (maxThreads-1): | |
startIndex = index | |
index += int(int(int(meta_length[0])/maxThreads)/block_sz)*block_sz | |
endIndex = index | |
thread = FilePartThread(i,url,filename+str(i),startIndex,endIndex) | |
threads.append(thread) | |
index +=1 | |
i +=1 | |
thread = FilePartThread(1,url,filename+str(i),index,int(meta_length[0])-1) | |
threads.append(thread) | |
for t in threads: | |
t.start() | |
for t in threads: | |
t.join() | |
destination = open(filename, 'wb') | |
i = 0 | |
while i < (maxThreads): | |
shutil.copyfileobj(open(filename+str(i), 'rb'), destination) | |
os.remove(filename+str(i)) | |
i +=1 | |
destination.close() | |
filename = download_file('http://cdn.frkmusic.info/dl/dz/zip') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment