Skip to content

Instantly share code, notes, and snippets.

@forestbelton
Created April 6, 2013 03:34
Show Gist options
  • Save forestbelton/5324641 to your computer and use it in GitHub Desktop.
Save forestbelton/5324641 to your computer and use it in GitHub Desktop.
Bandcamp album ripper
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