From 7e0fd439dc95f8de41f02354afe4ec2b65f8b296 Mon Sep 17 00:00:00 2001 From: "Simon W. Jackson" Date: Mon, 12 May 2014 10:54:15 +0200 Subject: [PATCH] Separated download progress indicator from download method --- bandcamp-dl/__init__.py | 0 bandcamp-dl.py => bandcamp-dl/bandcamp-dl.py | 47 +++++++++++++------- jsobj.py => bandcamp-dl/jsobj.py | 0 requirements.txt | 4 ++ 4 files changed, 34 insertions(+), 17 deletions(-) create mode 100644 bandcamp-dl/__init__.py rename bandcamp-dl.py => bandcamp-dl/bandcamp-dl.py (81%) rename jsobj.py => bandcamp-dl/jsobj.py (100%) create mode 100644 requirements.txt diff --git a/bandcamp-dl/__init__.py b/bandcamp-dl/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/bandcamp-dl.py b/bandcamp-dl/bandcamp-dl.py similarity index 81% rename from bandcamp-dl.py rename to bandcamp-dl/bandcamp-dl.py index 6a0b8fe..ece3950 100644 --- a/bandcamp-dl.py +++ b/bandcamp-dl/bandcamp-dl.py @@ -50,34 +50,47 @@ def sanatize_text(text, space=False, slash=False, period=False): 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"}) - u = urllib2.urlopen(req) - f = open(album_path + '/' + track['title'] + '.mp3', 'wb') - meta = u.info() +def download(request, destination, show_progress=False): + remote_file = urllib2.urlopen(request) + meta = remote_file.info() file_size = int(meta.getheaders("Content-Length")[0]) file_size_dl = 0 block_sz = 8192 + local_file = open(destination, 'wb') + while True: - buffer = u.read(block_sz) + buffer = remote_file.read(block_sz) if not buffer: break file_size_dl += len(buffer) - f.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() + local_file.write(buffer) - f.close() - print "Done downloading " + title + if show_progress: + 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): @@ -144,7 +157,7 @@ def parse_file(url): url = track['file']['mp3-128'] 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: ") parse_file(url) diff --git a/jsobj.py b/bandcamp-dl/jsobj.py similarity index 100% rename from jsobj.py rename to bandcamp-dl/jsobj.py diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..c7e6b64 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +mutagen +BeautifulSoup4 +requests +slimit