Visualising my Music Library

When you have a day off of work, the sun is shining, and everybody is on the beach, the ultimate thing to do is… visualising your music library. I wanted to have a two dimensional schema of artists, where similar artists are drawn  near each other, and unsimilar artists are drawn further apart. I made a small groovy script that does this in three steps:

  1. It reads the artists from my amarok database. Amarok is my favourite software for playing music.
  2. It calls a last.fm service to get to know which artists are similar
  3. It creates a (conceptual) graph of the artists in my database. Each artist is represented as a node. Similar artists are linked by an edge. The graph is written to a dot-file, which can easily be visualised by graphviz.
Part of music collection visualisation

Part of music collection visualisation

All of this happens in only 53 lines of Groovy script. Let’s look at some parts of it.

1. Reading the database

First put the jdbc driver jar into your path. Then, connect to the database using a groovy.sql.Sql object and query it. My script converts the returned rows into a set of Strings [“Venetian Snares”, “Max Richter”, “Murcof”, …].

def getArtists() {
    def sql = Sql.newInstance("jdbc:mysql://localhost:3306/amarok", "amarok", 
        "PASSWORDHERE", "com.mysql.jdbc.Driver")
    sql.rows("select name from artist").name as HashSet
}

2. Doing a web service call

Last.fm offers a RESTful webservice, that returns a list of artists similar to another. You can simply obtain it’s results by sending a get request to the
proper URL. Groovy simply let’s you type “http://url”.toUrl().text to do that!

def getSimilar(artist) {
    def url = "http://ws.audioscrobbler.com/2.0/?method=artist.getsimilar&
        artist=${removeSpaces(artist)}&api_key=b25b959554ed76058ac220b7b2e0a026"
    def text = url.toURL().text
    def slurper = new XmlSlurper().parseText(text)
    slurper.similarartists.artist.collect{ it.name.toString() }
}

The text is returned as xml. Reading Xml is very easy if you use Groovy’s XmlSlurper. It is what happens in the last two lines. The names of all
similar artists are returned as a list of Strings [“Swod”, “Rachel’s”, …]

3. Visualising

We now obtained the information to draw the graph. We have a list of artists in the music collection, and for each artist a list of similar artists. By doing
some processing, we can convert this into a dot-file, that represents a nice graph. The code for doing this is trivial. By opening the dot file in graphviz
we get a nice graph.

And all this didn’t take me too long. So don’t worry. I still spent most of the day in the park on my day off.

See the full script here .

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: