Skip to content

Instantly share code, notes, and snippets.

@joshdk
Last active December 16, 2015 22:08
Show Gist options
  • Save joshdk/5504329 to your computer and use it in GitHub Desktop.
Save joshdk/5504329 to your computer and use it in GitHub Desktop.
import json
import urllib2
import sys
from threading import Thread
class BandCamp(object):
def __init__(self, url):
self.album_id = self.get_id(url)
self.album_data = self.get_json()
def get_id(self, url):
r = urllib2.urlopen('http://api.bandcamp.com/api/url/1/info?key=vatnajokull&url=' + url).read()
return json.loads(r)['album_id']
def get_json(self):
r = urllib2.urlopen("http://api.bandcamp.com/api/album/2/info?key=vatnajokull&album_id=" + str(self.album_id)).read()
return json.loads(r)
def download_track(self, track):
title = track['title']
f = urllib2.urlopen(track['streaming_url']['mp3-128'])
data = f.read()
with open(title +".mp3", "wb") as code:
code.write(data)
print "Done downloading: " + title + ".mp3"
def download_tracks(self):
threads = []
for track in self.album_data['tracks']:
title = track['title']
print "Started downloading: " + title + ".mp3"
thread = Thread(target=self.download_track, args=(track,))
thread.start()
threads.append(thread)
for thread in threads:
thread.join()
print "Done downloading album"
def main(argv=None):
if argv is None:
argv = sys.argv
argc = len(argv)
url = sys.argv[1]
bc = BandCamp(url)
bc.download_tracks()
if __name__ == '__main__':
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment