MusicBrainz

Instruction how to get information from MusicBrainz database.

Script above shows how to get basic information about entered artist. Script returns list of albums with release date, title, and url to front and back cover (if exits)

Tutorial for MusicBrain is here

First of all we need to check artist id

http://www.musicbrainz.org/ws/2/artist/?query=<artist>

Next we can get xml contains release-list using following link:

http://www.musicbrainz.org/ws/2/release?artist=<artistID>

According to the wiki.musicbrainz.org : cover arts are available via coverartarchive.org

http://coverartarchive.org

 Discussion

bll 2018-11-8: To get information on a known recording, I am using:

http://musicbrainz.org/ws/2/recording/2409871f-6cde-42a1-a017-722b86f87e68?inc=artists%20releases%20media%20artist-credits

Let's try to get some basic info:

package require http
package require tdom

proc ___returnArtistID {artist} {
        set r [::http::geturl http://www.musicbrainz.org/ws/2/artist/?query=$artist]
        set data [::http::data $r]
        ::http::cleanup $r
        set doc [dom parse $data]
        set root [$doc documentElement]
        set ns {xmlns http://musicbrainz.org/ns/mmd-2.0#}
        set nodesList [$root selectNodes -namespaces $ns //xmlns:artist-list//xmlns:artist]
        return [[lindex $nodesList 0] getAttribute id]
}

proc ___coverURL {type id} {
        return "http://coverartarchive.org/release/$id/$type"
}

proc ___returnReleases {artistID} {
        set r [::http::geturl http://www.musicbrainz.org/ws/2/release?artist=$artistID]
        set data [::http::data $r]
        ::http::cleanup $r
        set doc [dom parse $data]
        set root [$doc documentElement]
        set ns {xmlns http://musicbrainz.org/ns/mmd-2.0#}
        set nodesList [$root selectNodes -namespaces $ns //xmlns:release-list//xmlns:release]
        #id used in the second query
        foreach node $nodesList {
                set date ""
                set id [$node getAttribute id]
                lappend ids $id
                set titleNode [$node selectNodes -namespaces $ns //xmlns:release-list//xmlns:release\[@id='$id'\]//xmlns:title]
                set dateNodes [$node selectNodes -namespaces $ns //xmlns:release-list//xmlns:release\[@id='$id'\]//xmlns:date]
                set coverFrontNode [$node selectNodes -namespaces $ns //xmlns:release-list//xmlns:release\[@id='$id'\]//xmlns:cover-art-archive//xmlns:front]
                set coverBackNode [$node selectNodes -namespaces $ns //xmlns:release-list//xmlns:release\[@id='$id'\]//xmlns:cover-art-archive//xmlns:back]
                set frontC [expr {[$coverFrontNode text] eq "true" ? [___coverURL "front" $id] : "false" }]
                set backC [expr {[$coverFrontNode text] eq "true" ? [___coverURL "back" $id] : "false" }]
                puts "##### Title: [$titleNode text] #####"
                puts "date: [[lindex $dateNodes 0] text]"
                puts "cover front: $frontC"
                puts "cover back: $backC"
                puts "##### #####"
        }
        return $ids
}


set artistID [___returnArtistID "portishead"]
set releaseIDs [___returnReleases $artistID]

puts [___returnArtistID "portishead"]

DG: I'm trying the JSON interface and prefer it so far. The code is much easier to get inside the data, IMO. JSON Web Service

package require http
package require uri  ;# in tcllib
package require json

http::config -useragent {example wiki.tcl-lang.org}

proc fetchJSON {uri {recurse_limit 4}} {
   http::config -accept "application/json"

   set token [http::geturl $uri]
   upvar #0 $token state
   if {[http::status $token] ne "ok" || [http::ncode $token] != 200} {
       # was the error a redirect?  If so, do it..
       if {[http::ncode $token] == 302 && [incr recurse_limit -1] > 0} {
           array set meta $state(meta)
           set result [fetchJSON $meta(Location) $recurse_limit]
           http::cleanup $token
           return $result
       }
       set err [http::code $token]
       http::cleanup $token
       return -code error $err
   }
   set json [http::data $token]
   array set meta $state(meta)
   http::cleanup $token

   # Do we need to do encoding conversions or was it already done
   # in transit?

   if {[info exist meta(Content-Type)] && \
           [regexp -nocase {charset\s*=\s*(\S+)} $meta(Content-Type)]} {

       # Socket channel encodings already performed!  No Work to do
       # here.  See section 5.2.2 of the html spec.  Server set
       # encodings win.

   }

   # Sloppy server.  Send them a bug report.
   set json [encoding convertfrom utf-8 $json]

   return [json::json2dict $json]
}

proc mblookup {entity mbid {inc {}} {method json}} {
    array set uri [list scheme http host musicbrainz.org path ws/2]

    set uri(path) [file join $uri(path) $entity]
    set uri(path) [file join $uri(path) $mbid]
    if {$inc ne ""} {
        set uri(query) "inc=$inc"
    }

    switch $method {
        json {
            return [fetchJSON [uri::join {*}[array get uri]]]
        }
        xml {
            return [[fetchXML [uri::join {*}[array get uri]]] documentElement]
        }
    }
}

# Generate the live bootleg directory name from the
# (new WS/2) advanced relationship information
#
proc genBootlegDirName {mbid} {
    array set ri [list]

    set query [mblookup release $mbid {place-rels+release-groups} json]

    # first check that this is a live, bootleg, single show recording
    if {[dict get $query status] ne "Bootleg"} {
        return -code error "not a bootleg"
    }

    set secondaries [dict get $query release-group secondary-types]

    if {[lsearch $secondaries "Compilation"] != -1} {
        return -code error "compilations are not a single show"
    }
    if {[lsearch $secondaries "Live"] == -1} {
        return -code error "not a live show"
    }

    foreach relation [dict get $query relations] {
        switch -- [dict get $relation type-id] {
            "4dda6e40-14af-46bb-bb78-ea22f4a99dfa" {
                # event
                unset relation
                continue ;# not usable. 'held at' is not shared (at this time)
            }
            "3b1fae9f-5b22-42c5-a40c-d1e5c9b90251" {
                # place
                if {[dict get $relation type] ne "recorded at"} continue
                # use target-credit instead of name, if set
                set ri(place) [expr {
                        [dict get $relation target-credit] ne "" ?
                        [dict get $relation target-credit] :
                        [dict get $relation place name]}]
                set ri(area)  [dict get $relation place area name]
                break ;# done
            }
        }
    }

    if {![info exist relation] || [dict get $relation type] ne "recorded at"} {
        return -code error "recording location not specified"
    }

    set d          [dict get $query disambiguation]
    # replace all dash with endash
    set ri(date)  [regsub -all -- {-} [dict get $relation begin] "\u2013"]
    set ri(title) [dict get $query title]

    return "$ri(date): $ri(title)[expr {$d ne ""?" ($d)":""}]: $ri(place), $ri(area)"
}

% genBootlegDirName 277decca-ce36-48dd-9115-0d69a14cd955
1971–09–23: Ladies and Gentlemen… This Is Led Zeppelin: Nippon Budokan, Kitanomaru Kōen
% genBootlegDirName 0b80625d-87dd-4aa3-a270-3398545efe53
1973–05–26: A Memento of Salt Lake City (sb+audc mix): Salt Palace, Salt Lake City
% genBootlegDirName 4dc3c0af-4c56-4900-ba34-65d081ce274d
1973–01–22: Any Port in a Storm: University of Southampton Students' Union, Southampton