Skip to content
Christian Woltering edited this page Sep 13, 2023 · 11 revisions

Setup an advanced search query to find a release using client.Releases.SearchAsync and get details including recordings using client.Releases.GetAsync:

using Hqub.MusicBrainz;
using Hqub.MusicBrainz.Entities;
using System;
using System.Linq;
using System.Threading.Tasks;

public class Example2
{
    public static async Task Run(MusicBrainzClient client)
    {
        await Search(client, "Massive Attack", "Mezzanine");
    }

    public static async Task Search(MusicBrainzClient client, string band, string album)
    {
        // Search for an artist by name.
        var artists = await client.Artists.SearchAsync(band.Quote());

        var artist = artists.Items.First();

        // Build an advanced query to search for the release.
        var query = new QueryParameters<Release>()
        {
            { "arid", artist.Id },
            { "release", album },
            { "type", "album" },
            { "status", "official" }
        };

        // Search for a release by title.
        var releases = await client.Releases.SearchAsync(query);

        Console.WriteLine("Total matches for '{0}': {1}", album, releases.Count);

        // Get the oldest release (remember to sort out items with no date set).
        var release = releases.Items.Where(r => !string.IsNullOrEmpty(r.Date) && IsCompactDisc(r)).OrderBy(r => r.Date).First();

        // Get detailed information of the release, including recordings.
        release = await client.Releases.GetAsync(release.Id, "recordings", "url-rels");

        Console.WriteLine();
        Console.WriteLine("Details for {0} - {1} ({2})", artist.Name, release.Title, release.Date);
        Console.WriteLine();

        // Get the medium associated with the release.
        var medium = release.Media.First();

        foreach (var track in medium.Tracks)
        {
            // The song length.
            var length = TimeSpan.FromMilliseconds((int)track.Length);

            Console.WriteLine("{0,3}  {1}  ({2:m\\:ss})", track.Number, track.Recording.Title, length);
        }
    }

    private static bool IsCompactDisc(Release r)
    {
        if (r.Media == null || r.Media.Count == 0)
        {
            return false;
        }

        return r.Media[0].Format == "CD";
    }
}

Clone this wiki locally