Posted on December 18th, 2008 by Neil Crosby. Filed under Uncategorized.
Gosh, it’s thursday already? Last Sunday I attended last.fm‘s first open Hack Day near Brick Lane in London with the intention of building a system to allow creation of Genius Playlists using last.fm’s data.
For those of you who aren’t sure, a Genius Playlist is a playlist of music that all “works together” that’s created from one seed track of the user’s choice. Essentially, it’s a shortcut to creating playlists of stuff that suits how you’re feeling at that moment, rather than you having to go to the trouble of creating the playlist yourself.
Whilst I was initially sceptical of how well this would work (I’ve worked in this problem space before, so I know it isn’t awesomely easy), since iTunes gained this feature in v8 I’ve become more and more reliant on it. My reliance has been so great that I’ve expected the feature in all the other music based services I use. For example, when I started using Spotify, one of the first things I did without thinking was to start playing a track that I wanted to listen to and then look for the “genius” button to see what else Spotify would try and play for me. It upset me that this feature wasn’t there. Sure, Spotify and last.fm allow you to play radio stations based on a certain genre, artist or tag, but that misses a piece of perceived control that starting with a chosen song implies.
So, for last.fm’s hack day I decided to hack together a genius playlist creator. After all, last.fm’s API supplies the track.getSimilar method, which pretty much gives all the data you need for a genius playlist.
There were a few hurdles to get through though. First up was playlist creation and playback itself. It turns out that in last.fm-world you can only create actual playlists if you’re a paid subscriber to the service. That’s not a dealbreaker for me, so I paid my £1.50 for a month’s subscription to see how it went. Unfortunately, at that point I realised that whilst I could now create playlists, I could only play them back if they had at least 45 songs in them by at least 15 different artists. Oh, and I could only play them back in shuffle mode. On the website. Not in the desktop app or on the iPhone. Somewhat sub-optimal.
So, I went back to the drawing board. If I couldn’t do playlists how I wanted in last.fm, how about if I found the data I wanted in last.fm and then used Spotify to play it? Unfortunately, Spotify doesn’t yet have an API, so this also proved impossible.
Scratch Plan B then. Thankfully, I did have a Plan 9 available to me – last.fm’s ability to autostart tracks on the website by adding
?autostart to the end of any song page URL. Using this ability, I reckoned, I would be able to create a page containing a list of songs that could then be played in a browser window by loading up the individual song pages in a hidden iframe. Simplicity itsef, and as if by magic it actually worked!
There was, of course, more magic that went into creating Last Genius. Not all songs on the site are available to stream, and of those that are not all of them are full length tracks. Because I was playing the songs in iframes I had no way of knowing when a song had actually ended. I had to make sure that you didn’t end up hearing the same song twice in a playlist. All these things and more were hacked around during the hack day, and in the end I had something that actually worked.
The end result was a bookmarklet that you can stick into your browser toolbar and click to create a genius playlist any time you want. Just make sure you’re on a Last.fm song page and a playlist will be created for you. If you’re not on a song page, lets just say that the system will try and find you some music that it reckons you’ll like.
Last Genius bookmarklet – drag onto your toolbar.
All I ask is that you remember that this is a hack. It’s only tested in Firefox 3 on the Mac, because during a hack day you don’t have time to make everything work everywhere (it’ll probably work elsewhere, I just haven’t tested yet). Because Last Genius has to make quite a few calls back to the mothership to set itself up, it might also take a little while to start up. Just be patient. For what it’s worth, I have plans to improve this over Christmas.
Because this is a toy I want for myself I plan to spend a bit more time making it nicer. With that in mind, if you have any ideas for things that will make it better, please let me know. Likewise, the source code is up on GitHub, so please go fork it and make improvements. Anything awesome I’ll very happily merge back into the trunk.
If you enjoyed this post, subscribe to The Code Train and read more when I write more.