One of my favorite radio stations of the Seattle area is 107.7 The End. It mostly plays Alternative Rock which is my go-to genre for general music listening. I’m also an avid Spotify Premium user and I create lots of playlists, one of which I populate entirely of songs played by 107.7 as listed on its TuneGenie page.

At first I would manually traverse this list of songs, find each track on Spotify, and add it to the playlist. This soon became very tedious because Spotify allows for adding the same song multiple times in the same playlist. For each track I had to first check (or remember) if I had previously added it to the playlist. So, I wrote a tool to do this all for me.

Spotify Metadata API

Spotify publishes a web API that allows for searching its database of artists, tracks, and albums. Example queries look like the following:

I wrote a .NET wrapper on this API called SpotifyWebSharp.

Extracting songs from a TuneGenie list

The 107.7 TuneGenie page is horrible. Slow, bloated, and no RSS feed to boot.

Let’s reformat this page to make it easier to pull the song information out of it. jQuery to the rescue!

Expand the page by clicking each Click to load songs from… links $(".hourlink").each(function() { $(this).click() }); Remove all the information except for the track artist and song $(".sl_item.sl_highlight.opened").removeAttr("style"); $(".ajaxdetails").remove(); $(".column.playedat").remove(); $(".column.pl_tiny.right").remove(); $(".tourdate").remove(); $(".boxad.adunit.column.last").remove(); Swap the artist and song name $(".song.column.tip").each(function(index) { $(this).before($(".artist.column")[index]) }); Add a pipe (|) character between the artist and song name. It will be used later to parse the track information. $(".artist.column").each(function() { $(this).append("<div>|</div>") }); Add a double-pipe (||) after each song name. It will be used to distinguish one song from another $(".song.column.tip").each(function() { $(this).append("<div>||</div>") }); Remove <script> tags that just get in the way $("script").remove(); Sure, right now it looks like crap, but now the data are in a format that can easily be stripped out. Grab the text of the parent #songlist <div>, remove the plethora of tab characters, and replace the double-pipe (||) with a newline character. Then replace the <body> with this string wrapped in a <pre> tag. $("body").html("<pre>" + $("#songlist").text().replace(/\s{2}/g, "").replace(/\|\|/g, "\r

") + "</pre>"); Voilà! I then copy this list into a text file that will then be read by a command-line tool that hooks into SpotifyWebSharp.

Find the songs missing from the playlist

Preparation

View your playlist in Spotify Ctrl-A to select all the tracks Drag/drop them to your favorite text editor. It should look something like this:

Optional, but I found it to work a little better: use the Spotify Lookup service to get the track IDs for each of these tracks. The same song can have multiple hrefs, but only one ID. Save a file that contains this list.

Pseudocode

foreach (artist-songname combination) { if (Spotify artist info is cached) { get artist info from cache } else { get artist info from Spotify search if (exact match not found) { prompt with suggestions or for manually entered name } cache artist info } if (Spotify track info is cached) { get track info from cache } else { get track info from Spotify search if (exact match for the tracknot found) { prompt with suggestions or for manually entered name } cache track info } if (track does not exist in playlist) { add track to list of new songs } }

Output

The program writes a text file that includes a list of all the track URLs. Drag/drop from your favorite text editor into Spotify and the tracks will be added to the playlist! The program also outputs a tracklist that piggy-backs off of billboard.com’s playlist viewer. An example. Here is some sample console output from a recent run:

