Sort Your Music

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.

The code for the app is at github if you are interested in seeing how it all works. And be sure to give it the app a try. It is time to get your music in the right order.

  1. #1 by Bob Michael on October 27, 2014 - 8:08 am

    What is valence?

  2. #2 by bobbym529 on October 27, 2014 - 8:10 am

    Never mind. I found it. :-)

  3. #3 by Ramyn King on October 27, 2014 - 10:14 am

    just started giving this a whirl. this is so powerful! thank you for this. the potential for useful spotify apps is massive and untapped. i’ve been waiting for something like this.

  4. #4 by ViewPacific on October 27, 2014 - 11:52 am

    Sounds like a great way to enhance listening pleasure. Sometimes I’d just like a more automated way to sort.
    I seem to be one of those who had obscure tastes so often get ready for the wait since I won’t benefit from the cache of pop stuff. I wonder if there’s any way to design support for such odd ones, who I suspect are more numerous than expected. Well, that is, if the 80/20 rule applies and 80% of the songs are favorites of 20% of the listeners.
    Any guess about that?

  5. #5 by LOois Jean-Louis D'Hondt on October 28, 2014 - 12:05 pm

    Hello Paul,

    First, let me tell you that I’m following your work for a time and I think you’re doing a truly amazing job !

    I’ve been waiting for similar sorting features for a while too (+1 Ramyn). That’s an impressive use of Spotify & echonest API. I wish this could part of Spotify core features or, at least, part of an app.

    Here’s my first feedback after some testing: sometimes the save feature does not reflect the sorting I get from ?

    It seems that my playlist also “lost” 100 songs after a save..? Fortunately for me, I had duplicated an existing playlist for test purpose after seeing the bug mentioned above.. :)

    Anyway, wonderful work and thanks for that app! If you need any more feedback, I’ll be glad to answer.

    Best regards


    • #6 by Celina Corridore on November 1, 2014 - 1:32 pm

      +1 Love the work, keep it coming. But also seeing fewer songs and a different order after a save. This is with a very large playlist (5000+ songs).

      Would love to see this evolve into something that lets you specify a combination of criteria for filtering prior to sorting (e.g., BPM 120-130, valence >50, sorted by danceablity).

%d bloggers like this: