How to sort results from the MusicBrainz search API

1.1k Views Asked by At

I'm trying to retrieve track information from the MusicBrainz API, using only the artist and title as search queries:

http://musicbrainz.org/ws/2/recording/?query=artist:Coldplay+AND+recording:The+Scientist

This will return every version of the song that the artist released, including compilations, 'best of' albums and live recordings. I want to find the 'original' version, which should be the first one that was released.

Without retrieving every single result (which may not be possible in a single request) the obvious way to do this would be to have the response sorted by ascending release date, so that the first result is always the one I'm after.

The API documentation doesn't seem to mention sorting results, so is this possible?

2

There are 2 best solutions below

0
On BEST ANSWER

I don't think sorting is possible.

It may not solve all problems either. Many recordings have a date as a year only. Thus, a recording with release date "1989" will be considered an earlier version than a recording with release date "1989-01-01", which in reality may or may not be the case.

Also, many releases have the same date, especially if the date is a year only, and some of these releases may be "original", while others are not. All recordings from these releases will have the same release date (e.g. "1989")

If accuracy is important, you may have to find the earliest official release which is not marked as live or compilation, and then choose a recording from this release.

0
On

It may not be the solution for you, but the following bash one-liner works for me to get the oldest year of an album:

curl -s 'http://musicbrainz.org/ws/2/release/?query=artist:%22The%20Beatles%22%20AND%20release:%22Abbey%20Road%22%20AND%20status:Official&fmt=json'  | jq -r '.releases[] | .date' | sort | head -n 1  | sed 's/-.*//'

It will query and get all date fields, sort them and get the lowest. Finally it removes possible date values after the year.

For your specific question it will not be enough. But maybe this answer helps some people.

You need to have jq installed for this to work.

I am sure there are prettier solutions. But this one is good enough for me.