Separated download progress indicator from download method
parent
df590ef1fb
commit
7e0fd439dc
|
@ -50,34 +50,47 @@ def sanatize_text(text, space=False, slash=False, period=False):
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def download_track(track, url, title, album_path, artist, album):
|
|
||||||
print "Now Downloading: " + track['title'], track['file']['mp3-128']
|
|
||||||
|
|
||||||
req = urllib2.Request(url, headers={'User-Agent': "Magic Browser"})
|
def download(request, destination, show_progress=False):
|
||||||
u = urllib2.urlopen(req)
|
remote_file = urllib2.urlopen(request)
|
||||||
f = open(album_path + '/' + track['title'] + '.mp3', 'wb')
|
meta = remote_file.info()
|
||||||
meta = u.info()
|
|
||||||
file_size = int(meta.getheaders("Content-Length")[0])
|
file_size = int(meta.getheaders("Content-Length")[0])
|
||||||
file_size_dl = 0
|
file_size_dl = 0
|
||||||
block_sz = 8192
|
block_sz = 8192
|
||||||
|
|
||||||
|
local_file = open(destination, 'wb')
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
buffer = u.read(block_sz)
|
buffer = remote_file.read(block_sz)
|
||||||
if not buffer:
|
if not buffer:
|
||||||
break
|
break
|
||||||
|
|
||||||
file_size_dl += len(buffer)
|
file_size_dl += len(buffer)
|
||||||
f.write(buffer)
|
local_file.write(buffer)
|
||||||
p = float(file_size_dl) / file_size
|
|
||||||
status = r"[{1:.2%}]".format(file_size_dl, p)
|
|
||||||
status = status + chr(8) * (len(status) + 1)
|
|
||||||
sys.stdout.write("Download progress: %s%% \r" % (status))
|
|
||||||
sys.stdout.flush()
|
|
||||||
|
|
||||||
f.close()
|
if show_progress:
|
||||||
print "Done downloading " + title
|
display_progress(file_size_dl, file_size)
|
||||||
|
|
||||||
write_id3_tags(track, title, album_path, artist, album)
|
local_file.close()
|
||||||
|
print "Done downloading: " + destination
|
||||||
|
|
||||||
|
|
||||||
|
def display_progress(current, total):
|
||||||
|
factor = float(current) / total
|
||||||
|
percent = factor * 100
|
||||||
|
status = str( int(round(percent)) )
|
||||||
|
|
||||||
|
sys.stdout.write("Download progress: %s%% \r" % (status))
|
||||||
|
sys.stdout.flush()
|
||||||
|
|
||||||
|
|
||||||
|
def download_track(track, url, title, album_path, artist, album):
|
||||||
|
print "Now Downloading: " + track['title'], track['file']['mp3-128']
|
||||||
|
|
||||||
|
req = urllib2.Request(url, headers={'User-Agent': "Magic Browser"})
|
||||||
|
destination = album_path + '/' + track['title'] + '.mp3'
|
||||||
|
|
||||||
|
download(req, destination, show_progress = True)
|
||||||
|
|
||||||
|
|
||||||
def write_id3_tags(track, title, album_path, artist, album):
|
def write_id3_tags(track, title, album_path, artist, album):
|
||||||
|
@ -144,7 +157,7 @@ def parse_file(url):
|
||||||
url = track['file']['mp3-128']
|
url = track['file']['mp3-128']
|
||||||
|
|
||||||
download_track(track, url, title, album['path'], album['artist'], album['title'])
|
download_track(track, url, title, album['path'], album['artist'], album['title'])
|
||||||
|
write_id3_tags(track, title, album['path'], album['artist'], album['title'])
|
||||||
|
|
||||||
url = raw_input("Please enter the url of the album or song you wish to download: ")
|
url = raw_input("Please enter the url of the album or song you wish to download: ")
|
||||||
parse_file(url)
|
parse_file(url)
|
|
@ -0,0 +1,4 @@
|
||||||
|
mutagen
|
||||||
|
BeautifulSoup4
|
||||||
|
requests
|
||||||
|
slimit
|
Loading…
Reference in New Issue