Created
April 6, 2013 03:34
-
-
Save forestbelton/5324641 to your computer and use it in GitHub Desktop.
Bandcamp album ripper
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
from ID3 import * | |
import os, re, string, urllib2 | |
embed_data = re.compile(r'var EmbedData = {.*?album_title : "([^"]+?)".*?artist : "([^"]+?)".*?};', re.DOTALL) | |
track_data = re.compile(r'trackinfo : \[([^\]]+?)\]', re.DOTALL) | |
def grab(url): | |
f = urllib2.urlopen(url) | |
data = f.read() | |
match = embed_data.findall(data) | |
album, artist = match[0] | |
album = string.replace(album, r'\/', '-') | |
print 'Album: {0}'.format(album) | |
print 'Artist: {0}'.format(artist) | |
match = track_data.findall(data) | |
obj = '[' + match[0] + ']' | |
obj = string.replace(obj, 'false', 'False') | |
obj = string.replace(obj, 'true', 'True') | |
obj = string.replace(obj, 'null', 'None') | |
obj = string.replace(obj, r'\/', r'/') | |
obj = eval(obj) | |
try: | |
os.makedirs('{0}/{1}'.format(artist, album)) | |
except OSError: | |
pass | |
trackno = 1 | |
for track in obj: | |
filename = '{0:02} - {1}.mp3'.format(trackno, track['title']) | |
print 'Downloading {0}...'.format(filename) | |
link = urllib2.urlopen(track['file']) | |
song = open('{0}/{1}/{2}'.format(artist, album, filename), 'wb') | |
song.write(link.read()) | |
song.close() | |
file = ID3('{0}/{1}/{2}'.format(artist, album, filename)) | |
file['TITLE'] = track['title'] | |
file['ARTIST'] = artist | |
file['ALBUM'] = album | |
file['TRACKNUMBER'] = str(trackno) | |
del file | |
trackno += 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment