A Groovy Script for Visualising my Music Collection

import groovy.sql.Sql

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
}

def correct(artist) {
    artist.replace(' ', '_')
}

def getSimilar(artist) {
    def url = "http://ws.audioscrobbler.com/2.0/?method=artist.getsimilar&
artist=${correct(artist)}&api_key=b25b959554ed76058ac220b7b2e0a026"
    def text = ""
    try {
         text = url.toURL().text
    }
    catch (IOException) {
        // artist not found, skip
        return
    }
    def slurper = new XmlSlurper().parseText(text)
    slurper.similarartists.artist.collect{ it.name.toString() }
}

def createNodes(matrix, all, artist, similar) {
    matrix[artist] = [] as HashSet
    similar.each {
        if (artist != it &&
        all.contains(it) &&
        !matrix[it]?.contains(artist)) {
            matrix[artist] += it
        }
    }
}

def writeGraph(matrix) {
    println "graph {"
    matrix.each { artist ->
        matrix[artist.key].each { similar ->
            println ("    \"${artist.key}\" -- \"$similar\"")
        }
    }
    println "}"
}

matrix = [:]
def all = getArtists()
all.each {
    createNodes(matrix, all, it, getSimilar(it))
}
writeGraph(matrix)
Advertisements

One Response to “A Groovy Script for Visualising my Music Collection”

  1. Visualising my Music Library « Groovy Guts Says:

    […] See the full script here . […]

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: