Artist search with libmusicbrainz5

I hope you have gone through the URLs posted in the previous post. Now lets move to the programming part. 😮
Getting data from libmusicbrainz5 is really simple.
Download code from the following URL to find Artist Westlife.
https://github.com/abhijangda/MB5_examples/blob/master/artist_search_in_libmusicbrainz5.c

This is the code for searching Artist Westlife. Its a simple program. Let us go in its details.
As said in earlier post, we have to make a query to the MusicBrainz server. Here query is created with the help of
Mb5Query     mb5_query_new (const char *UserAgent, const char *Server, int Port)
The arguments are:
UserAgent: The UserAgent to use. This UserAgent will be used in all the submissions.
Server: This is the Server Address, passing NULL it defaults to use default server, which is musicbrainz.org. Mostly you should pass NULL.
Port: The port to use to send/recieve data from Server. Passing 0 will use default port which is 80.
This function returns Mb5Query. Its good to use the same Mb5Query object for subsequent searches in your program.
After this if Query is created then, the object’s pointer will not be NULL. Moreover, Query is allocated on the Heap, hence you should free it using
mb5_query_delete(Mb5Query)
After this, we will query the server using the function
Mb5Metadata mb5_query_query (Mb5Query Query, const char *Entity,const char *ID,const char *Resource,int NumParams,char **ParamNames, char **ParamValues)
I will come to its arguments in a while. This function returns Mb5Metadata Object. This object contains all the searched data recieved from the MusicBrainz server. This metadata is in XML form. So, we need a parser to parse XML data and get the required information. For this so many functions are available in the libmusicbrainz5. We will now see how to get the artist information only.
As you can see in the code, I have declared ParamValues and ParamNames, these are arrays of strings. ParamNames contains the name of the Parameters to be passed in the query e.g. “query”, “limit”, “inc” etc. ParamValues has corresponding values for these ParamNames eg “artist:Westlife”, “5”, “artists” etc. If the name of parameter is “query”, then its corresponding value should be set in the following manner:
1. There should be colon ‘:’ in the value string.
2. The part of the string before colon contains one of the artist, release, release-group, recording i.e. 6 core parameters.
3. The part of the string after colon contains the value you want to search. For example, I have entered “Westlife” after “artist:” because I want to search for all the artist with Westlife.
NumParams: The length of these string arrays is passed to the above function.
Resource: is used only for Collection, for other uses it is passed as empty string “”.
Entity: is the entity you want to search for, so here it is passed as “artist”.
ID: is the MusicBrainz ID of the entity. We will discuss its use in later posts.
After calling this function, the program starts communicating with the MusicBrainz servers. When this function ends the process of communicating with the server ends and we get the Result and HTTPCode. Result indicates whether the query was Successful or not. If, not then it also tells the kind of error. We get result from the query using
tQueryResult mb5_query_get_lastresult(Mb5Query Query)
tQueryResult is an enum and it can have following values
eQuery_Success
eQuery_ConnectionError
eQuery_Timeout
eQuery_AuthenticationError
eQuery_FetchError
eQuery_RequestError
eQuery_ResourceNotFound
The name of the values itself displays what they want to depict.
You can get HTTPCode using
int mb5_query_get_lasthttpcode(Mb5Query Query)
Although I don’t know its use right know 😦
After the successful completion of the search query, lets see how to get the data for Artist.
Each and every search query contains list of the entity you requested. For example, here we requested Artist so we get a list of Artist. Hence, we will use corresponding entity’s list function to get the data.
To get the artist list from the Metadata, we use
Mb5ArtistList mb5_metadata_get_artistlist (Mb5Metadata Metadata)
This function extracts the artist list from metadata. This list is stored in Mb5ArtistList Object. Now you can use
int mb5_artist_list_size (Mb5ArtistList List)
to get the size of the Artist List object.
To retrieve each Mb5Artist Object, use
Mb5Artist mb5_artist_list_item (Mb5ArtistList List, int Item)
This function will return Mb5Artist Object at the index Item.
Now each Artist information can be returned by mb5_artist* functions. This is pretty straight forward. But let me explain the concept of MusicBrainz ID (MBID). Each entity is referenced by its MBID on the MusicBrainz server. This is unique. Here artist is having its own MBID. This MBID will be used in the later posts to retrieve Westlife’s Release-Groups.
Moreover, remember to delete what you created ;). Hence, Metadata which is allocated on the Heap should be removed.
This is a general procedure, similar procedure applies to search for other 5 core entities.
Thank You. Hope you understand this. 🙂 Good Luck

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