diff --git a/bandcamp-dl/Bandcamp.py b/bandcamp-dl/Bandcamp.py index 8bdc12a..7d93915 100644 --- a/bandcamp-dl/Bandcamp.py +++ b/bandcamp-dl/Bandcamp.py @@ -15,7 +15,7 @@ import jsobj class Bandcamp: - DOWNLOAD_DIR = expanduser("~/music") + DOWNLOAD_DIR = expanduser("~/Music") # def __init__(self, artist, album): # self.artist = artist @@ -83,7 +83,7 @@ class Bandcamp: print "Now Downloading: " + track['title'], track['file']['mp3-128'] req = urllib2.Request(url, headers={'User-Agent': "Magic Browser"}) - destination = album_path + '/' + self.sanatize_text(track['title']) + '.mp3' + destination = album_path + '/' + self.sanatize_text(track['title'], space=True) + '.mp3' self.download(req, destination, show_progress = True) @@ -147,6 +147,11 @@ class Bandcamp: return album + @staticmethod + def generate_album_url(artist, album): + return "http://{0}.bandcamp.com/album/{1}".format(artist, album) + + def parse_album_page(self, url): print "Starting the parsing for: " + url @@ -162,10 +167,3 @@ class Bandcamp: self.download_track(track, url, title, album['path'], album['artist'], album['title']) self.write_id3_tags(track, title, album['path'], album['artist'], album['title']) - - -if __name__ == '__main__': - url = 'http://wmdchiptune.bandcamp.com/album/sophrosyne' - - bc = Bandcamp() - bc.parse_album_page(url) diff --git a/bandcamp-dl/bandcamp-dl.py b/bandcamp-dl/bandcamp-dl.py index 87eb952..2af990f 100644 --- a/bandcamp-dl/bandcamp-dl.py +++ b/bandcamp-dl/bandcamp-dl.py @@ -1,165 +1,46 @@ -""" Coded by Iheanyi Ekechukwu +"""Naval Fate. +Usage: + bandcamp-dl.py download [ | --artist= --album=] + bandcamp-dl.py (-h | --help) + bandcamp-dl.py --version -http://www.twitter.com/kwuchu -http://www.github.com/iheanyi +Options: + -h --help Show this screen. + --version Show version. + --artist= Speed in knots + --album= Speed in knots -Feel free to use this in any way you wish. I made this just for fun. +""" -Shout out to darkf for writing a helper function for parsing the JavaScript! """ +from docopt import docopt +from Bandcamp import Bandcamp -import unicodedata -import os -import urllib2 +""" Coded by: -from mutagen.mp3 import MP3 -from mutagen.id3 import TIT2 -from mutagen.easyid3 import EasyID3 -from bs4 import BeautifulSoup -import requests -import sys -import Bandcamp +Iheanyi Ekechukwu + http://twitter.com/kwuchu + http://github.com/iheanyi -import jsobj +Simon W. Jackson + http://miniarray.com + http://twitter.com/miniarray + http://github.com/miniarray -##################################### - -DOWNLOAD_DIR = "/Users/simonwjackson/music" - -##################################### +Iheanyi: + Feel free to use this in any way you wish. I made this just for fun. + Shout out to darkf for writing a helper function for parsing the JavaScript! """ -def get_embed_string_block(request): - embedBlock = request.text.split("var EmbedData = ") +if __name__ == '__main__': + arguments = docopt(__doc__, version='bandcamp-dl 1.0') + bandcamp = Bandcamp() - embedStringBlock = embedBlock[1] - embedStringBlock = unicodedata.normalize('NFKD', embedStringBlock).encode('ascii', 'ignore') - embedStringBlock = embedStringBlock.split("};")[0] + "};" - embedStringBlock = jsobj.read_js_object("var EmbedData = %s" % str(embedStringBlock)) + print arguments - return embedStringBlock + if (arguments['download']): + if (arguments['--artist'] and arguments['--album']): + url = Bandcamp.generate_album_url(arguments['--artist'], arguments['--album']) + bandcamp.parse_album_page(url) - -def sanatize_text(text, space=False, slash=False, period=False): - result = text - - if not space: - result = result.replace(" ", "") - if not slash: - result = result.replace("/", "") - if not period: - result = result.replace(".", "") - - return result - - -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 = remote_file.read(block_sz) - if not buffer: - break - - file_size_dl += len(buffer) - local_file.write(buffer) - - if show_progress: - display_progress(file_size_dl, file_size) - - 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): - print "Encoding . . . " - - audio = MP3(album_path + '/' + track['title'] + '.mp3') - audio["TIT2"] = TIT2(encoding=3, text=["title"]) - audio.save() - - audio = EasyID3(album_path + '/' + track['title'] + '.mp3') - audio["title"] = track['title'] - audio["artist"] = artist - audio["album"] = album - audio.save() - - print "Done encoding . . . " - - -def create_directories(artist, album): - album_path = DOWNLOAD_DIR + "/" + artist + "/" + sanatize_text(album) - - if not os.path.exists(DOWNLOAD_DIR): - os.makedirs(DOWNLOAD_DIR) - - if not os.path.exists(DOWNLOAD_DIR + "/zips"): - os.makedirs(DOWNLOAD_DIR + "/zips") - - if not os.path.exists(album_path): - os.makedirs(album_path) - - return album_path - - -def extract_album_meta_data(request): - album = {} - - embedData = get_embed_string_block(request) - - block = request.text.split("var TralbumData = ") - - stringBlock = block[1] - stringBlock = unicodedata.normalize('NFKD', stringBlock).encode('ascii', 'ignore') - stringBlock = stringBlock.split("};")[0] + "};" - stringBlock = jsobj.read_js_object("var TralbumData = %s" % str(stringBlock)) - - album['title'] = embedData['EmbedData']['album_title'] - album['artist'] = stringBlock['TralbumData']['artist'] - album['tracks'] = stringBlock['TralbumData']['trackinfo'] - - return album - - -def parse_file(url): - print "Starting the parsing for: " + url - - r = requests.get(url) - soup = BeautifulSoup(r.text) - - album = extract_album_meta_data(r) - album['path'] = create_directories(album['artist'], album['title']) - - for track in album['tracks']: - title = sanatize_text(track['title'], space=True) - 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) + elif (arguments['']): + bandcamp.parse_album_page(arguments['']) diff --git a/requirements.txt b/requirements.txt index c7e6b64..eebdf16 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,4 @@ mutagen BeautifulSoup4 requests slimit +docopt