Saturday, November 24, 2012

Backing up my Spotify Playlists

I've been using Spotify for a number as my exclusive source of music, during which time i've built up some rather large playlists. If Spotify were to loose these lists it would take me years to recall and collate all of the tracks again, especially as many are 'Spotify Discoveries', for which I can't remember the artist seconds after adding, let alone years later. This makes me slightly paranoid.

Strangely, there doesn't appear to be a way to save a playlist tracklisting to a file (unless i'm missing something). You can copy the playlist into a text file, but this gives you Spotify URLs rather than track names. You can also choose to copy all tracks as Spotify URIs, but these are just internal Spotify Track IDs.

Spotify have a web API that allows you to query the spotify database, returning results as XML. This does allow you to query individual tracks, using the aforementioned URIs.

Therefore, with a bit of automation it's possible to take the list of URIs exported from Spotify and to query the Web API for each individual track, writing the call results to a CSV file..

I'm a Java programmer by trade, and I know that there are far more elegant ways of dealing with web APIs than the Java SDK plus a bunch of third party libraries. I could of used Groovy, but that felt like cheating, so I went with PhantomJS, a 'Headless Webkit' with a Javascript API.

The result is a script that given a named file containing a list of spotify URIs (one on each line), would query Spotify for the track name and artist, writing the result to a CSV file. All of this in less than 50 lines of code. Nice!
Obviously this is all a bit hacky and there's no error handling, but it's successfully saved my 445 track playlist outside of Spotify's Infrastructure. And I get to use recursion, which is always fun!

No comments:

Post a Comment