#region ========================================================================= USING ===================================================================================== using Nemera.Contracts.DTO.MediaLibrary.AudioLibrary.MusicLibrary; using Nemera.Domain.SharedKernel.Common.Enums.MediaLibrary; using Nemera.Plugins.Contracts.Core.Metadata; using Nemera.Plugins.MusicBrainz.Common.Models.Contracts.Responses; using Nemera.Plugins.MusicBrainz.Common.Models.DTO.Settings; using Nemera.Plugins.MusicBrainz.Core.Api; using Nemera.Plugins.MusicBrainz.Core.Mapping; using Nemera.Plugins.MusicBrainz.Core.Settings; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; #endregion namespace Nemera.Plugins.MusicBrainz.Core; /// /// Provides music artist metadata from MusicBrainz by resolving lookups into artist metadata DTOs. /// internal sealed class MusicBrainzArtistMetadataProvider : IMetadataProvider { private readonly MusicBrainzHttpClient _musicBrainzHttpClient; private readonly MusicBrainzSettingsProvider _settingsProvider; /// /// Gets the display name of the metadata provider. /// public string Name => "MusicBrainz"; /// /// Gets the media library types this metadata provider supports. /// public IReadOnlyList SupportedLibraryTypes => [LibraryType.Music]; /// /// Gets a value indicating whether this metadata provider requires access to the web to retrieve metadata. /// public bool RequiresWebAccess => true; /// /// Initializes a new instance of the class. /// /// The HTTP client used to call the MusicBrainz web service. /// The provider of the settings that configure the MusicBrainz web service requests. public MusicBrainzArtistMetadataProvider(MusicBrainzHttpClient musicBrainzHttpClient, MusicBrainzSettingsProvider settingsProvider) { _musicBrainzHttpClient = musicBrainzHttpClient; _settingsProvider = settingsProvider; } /// /// Searches for the metadata of the artist described by . /// /// The lookup describing the artist to search for. /// Cancellation token that can be used to stop the execution. /// The collection of artist metadata candidates found. public async Task> GetSearchResultsAsync(ArtistMetadataLookupDto lookup, CancellationToken cancellationToken) { if (string.IsNullOrWhiteSpace(lookup.Name)) return []; MusicBrainzSettingsDto settings = await _settingsProvider.GetAsync(cancellationToken).ConfigureAwait(false); IReadOnlyList artists = await _musicBrainzHttpClient .SearchArtistsAsync(lookup.Name.Trim(), settings.SearchResultLimit, cancellationToken).ConfigureAwait(false); List candidates = []; foreach (MusicBrainzArtistResponse artist in artists) if (!string.IsNullOrWhiteSpace(artist.Name)) candidates.Add(MusicBrainzMapper.MapArtist(artist)); return candidates; } /// /// Gets the metadata of the artist described by . /// /// The lookup describing the artist to get the metadata for. /// Cancellation token that can be used to stop the execution. /// The metadata of the artist, or when no artist was found. public async Task GetMetadataAsync(ArtistMetadataLookupDto lookup, CancellationToken cancellationToken) { // A MusicBrainz identifier is the most precise lookup, so it is used first, if present. if (lookup.MusicBrainzArtistId is not null) { MusicBrainzArtistResponse? artist = await _musicBrainzHttpClient.GetArtistAsync(lookup.MusicBrainzArtistId.Value, cancellationToken).ConfigureAwait(false); return artist is null ? null : MusicBrainzMapper.MapArtist(artist); } if (string.IsNullOrWhiteSpace(lookup.Name)) return null; // Without an identifier, the artist is searched by name, and the first match is resolved in full. IReadOnlyList artists = await _musicBrainzHttpClient .SearchArtistsAsync(lookup.Name.Trim(), 1, cancellationToken).ConfigureAwait(false); MusicBrainzArtistResponse? searchResult = artists.Count > 0 ? artists[0] : null; if (searchResult?.Id is null) return null; MusicBrainzArtistResponse? detailedArtist = System.Guid.TryParse(searchResult.Id, out System.Guid artistId) ? await _musicBrainzHttpClient.GetArtistAsync(artistId, cancellationToken).ConfigureAwait(false) : null; return detailedArtist is null ? MusicBrainzMapper.MapArtist(searchResult) : MusicBrainzMapper.MapArtist(detailedArtist); } }