A few weeks ago, the Spotify Web API team pushed out some updates to the API that allows developers to update the tracks in a listener’s playlist. With these changes a developer can add, replace, remove and rearrange tracks in a playlist on behalf of a listener. This week I wanted to try out these new API features so I built an app called Sort Your Music.
Sort Your Music lets you sort the tracks in any of your playlists based on a number of Echo Nest parameters. You can sort a playlist by BPM, Energy, Danceability, Loudness, Acousticness, Valence and more. Once you’ve sorted a playlist you can save it back to Spotify, letting you listen to it on any of your music devices. For example, here’s a copy of the Spotify Top 50 playlist, where the tracks have been sorted from highest energy to lowest.[spotify spotify:user:plamere:playlist:5feKJB4zyQaFT6aw4i80tZ]
Using Sort Your Music is quite simple, login with your Spotify credentials and give the app permission to modify your playlists. Then, select the playlist you want to work on and, after a few seconds (while all of the song data is retrieved from The Echo Nest), your playlist will appear in table form like so:
To sort the playlist, just click on the column headings for any of the parameters. When you are happy with the changes, just click save, and your playlist will be updated.
Under the Hood
This is a pretty straightforward app, but there were a few challenging bits. The primary challenge was dealing with the large number of calls to The Echo Nest. Each song in a playlist requires a call to The Echo Nest to fetch the song attributes, so even a modest playlist of 40 songs results in 40 Echo Nest calls. Multiply that by a few dozen active users and the app will be overwhelming my Echo Nest API rate limit. To avoid this, I created my own caching server that sits between the web app and The Echo Nest. It fields bulk requests from the web app (all the IDs at once), and retrieves the song data from The Echo Nest, eliminating any unneeded data, and passing it back to the web app. The big performance win comes from keeping a cache of the song info. After a bit of usage, most popular songs will be in the cache making most playlist song resolving quite snappy. Still, if you have a long and obscure playlist it may take 10 seconds to resolve.
Having a caching server gives me a few other benefits – I have a central point to handle rate limit throttling – if the app gets busy and we start hitting the rate limit, the server can do the throttling automatically, and I can take action. Another big advantage is that I don’t have to expose my Echo Nest API key to the world like I would need to do if I made Echo Nest calls directly from the web client.
My caching server has an info endpoint that returns some json data about the server status, including the average time to process each request to resolve a playlist. The current average resolve time is about 700ms – not too bad.
A Safari glitch – when I tested the finished app on Safari, I found that authentication didn’t work. This was quite puzzling, as it had worked for me before. I went back to some of my older Spotify apps that perform authentication and it turns out that they were no longer working as well. What changed? Well, I’m running the spiffy new Yosemite with an update to Safari. Digging deeper it turned out that the new Safari doesn’t like redirect URLs without a trailing slash. Once I added a trailing slash to the redirect URL all was well.