Bandcamp’s API, a Bit of Python, and New Design

Bandcamp has released an API through
the Bandcamp Tech blog. I have no idea when or if it will show up on the FAQ or main site. I requested an API key (took about a day?) and did a little bit of messing with it over the weekend.

Here’s a really simple Python script for extracting your album and track names, for those who are somehow even more inept at Python than I am:

#!/usr/local/bin/python
import httplib
import json

def attempt_print(s,e="Failed to print some Unicode."):
        try:
#               For generating XML entities:
#               print(s.encode('ascii', 'xmlcharrefreplace'))
                print(s)
        except UnicodeEncodeError:
                print(e)

disco_url = '/api/band/1/discography?key=DEVELOPER KEY GOES HERE&band_id=BAND ID GOES HERE'
album_url = '/api/album/1/info?key=DEVELOPER KEY GOES HERE&album_id=%s'
track_url = '/api/track/1/info?key=DEVELOPER KEY GOES HERE&track_id=%s'

h = httplib.HTTPConnection('api.bandcamp.com', 80)
h.request('GET',disco_url)
r = h.getresponse()
# A responsible programmer might check that r.status == 200 and handle problems.

d = r.read()

band_data = json.loads(d)

print("Band data: ")
discography = band_data[u'discography']

for t in discography:
        if(t.has_key(u'album_id')):
                attempt_print("Album: %s" % (t[u'title']))
                h.request('GET',album_url % (t[u'album_id']))
                r = h.getresponse()
                album_json= r.read()
                album = json.loads(album_json)
                attempt_print(" Title: %s" % (album[u'title']))
                for track in album[u'tracks']:
                        attempt_print("         Track: %s" % (track[u'title']))
        if(t.has_key(u'track_id')):
                attempt_print("Track: %s" % (t[u'title']))

For those who don’t care to look the API over themselves, it gives you MP3 URLs that you can embed and individual URLs for each item. I’m looking over WordPress Audio Player and I think I’ll probably use or modify that for playing music through the API.

Comments on my Python are welcome. I don’t normally use it, but I ended up at the local Python meetup and picked up a signed copy of The Quick Python Book by the author (Vernon L. Ceder) who was presenting on Python 3. I’ve since lost the book.

By the way, yes, our web design is very rough and we’re replacing it soon. The photos used up top (with some more to come as album titles and other graphics) are by Joseph Lamm. The design itself and all of the graphics were cooked up in Inkscape, which is quickly becoming one of my favorite open-source tools despite having a gratis copy of CS5 in my pocket. (Perhaps because it was gratis…) The font used is OptimusPrinceps, a really nice free serif font. No Bandcamp integration yet, but if and when we decide how to use it I might give some more information.

Leave a Reply