| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Net.Http; |
| | | 5 | | using System.Threading; |
| | | 6 | | using System.Threading.Tasks; |
| | | 7 | | using Jellyfin.Extensions; |
| | | 8 | | using MediaBrowser.Controller.Entities.Audio; |
| | | 9 | | using MediaBrowser.Controller.Providers; |
| | | 10 | | using MediaBrowser.Model.Entities; |
| | | 11 | | using MediaBrowser.Model.Providers; |
| | | 12 | | using MediaBrowser.Providers.Music; |
| | | 13 | | using MetaBrainz.MusicBrainz; |
| | | 14 | | using MetaBrainz.MusicBrainz.Interfaces.Entities; |
| | | 15 | | using MetaBrainz.MusicBrainz.Interfaces.Searches; |
| | | 16 | | using Microsoft.Extensions.Logging; |
| | | 17 | | |
| | | 18 | | namespace MediaBrowser.Providers.Plugins.MusicBrainz; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// MusicBrainz artist provider. |
| | | 22 | | /// </summary> |
| | | 23 | | public class MusicBrainzArtistProvider : IRemoteMetadataProvider<MusicArtist, ArtistInfo>, IHasOrder |
| | | 24 | | { |
| | | 25 | | private readonly ILogger<MusicBrainzArtistProvider> _logger; |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Initializes a new instance of the <see cref="MusicBrainzArtistProvider"/> class. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <param name="logger">The logger.</param> |
| | | 31 | | public MusicBrainzArtistProvider(ILogger<MusicBrainzArtistProvider> logger) |
| | | 32 | | { |
| | 27 | 33 | | _logger = logger; |
| | 27 | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <inheritdoc /> |
| | 0 | 37 | | public string Name => "MusicBrainz"; |
| | | 38 | | |
| | | 39 | | /// <inheritdoc /> |
| | | 40 | | /// Runs first to populate the MusicBrainz artist ID used by downstream providers. |
| | 0 | 41 | | public int Order => 0; |
| | | 42 | | |
| | | 43 | | /// <inheritdoc /> |
| | | 44 | | public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(ArtistInfo searchInfo, CancellationToken cancell |
| | | 45 | | { |
| | 0 | 46 | | var query = MusicBrainz.Plugin.Instance!.MusicBrainzQuery; |
| | 0 | 47 | | var artistId = MusicBrainzQueryExtensions.ParseMusicBrainzId(searchInfo.GetMusicBrainzArtistId(), "artist", _log |
| | | 48 | | |
| | 0 | 49 | | if (artistId is not null) |
| | | 50 | | { |
| | 0 | 51 | | var artistResult = await query.LookupArtistOrNullAsync(artistId.Value, Include.Aliases, _logger, cancellatio |
| | 0 | 52 | | if (artistResult is not null) |
| | | 53 | | { |
| | 0 | 54 | | return GetResultFromResponse(artistResult).SingleItemAsEnumerable(); |
| | | 55 | | } |
| | | 56 | | } |
| | | 57 | | |
| | 0 | 58 | | if (string.IsNullOrWhiteSpace(searchInfo.Name)) |
| | | 59 | | { |
| | 0 | 60 | | return []; |
| | | 61 | | } |
| | | 62 | | |
| | 0 | 63 | | var artistSearchResults = await query.FindArtistsWithRetryAsync($"\"{searchInfo.Name}\"", _logger, cancellationT |
| | 0 | 64 | | .ConfigureAwait(false); |
| | 0 | 65 | | if (artistSearchResults.Results.Count > 0) |
| | | 66 | | { |
| | 0 | 67 | | return GetResultsFromResponse(artistSearchResults.Results); |
| | | 68 | | } |
| | | 69 | | |
| | 0 | 70 | | if (searchInfo.Name.HasDiacritics()) |
| | | 71 | | { |
| | | 72 | | // Try again using the search with an accented characters query |
| | 0 | 73 | | var artistAccentsSearchResults = await query.FindArtistsWithRetryAsync($"artistaccent:\"{searchInfo.Name}\"" |
| | 0 | 74 | | .ConfigureAwait(false); |
| | 0 | 75 | | if (artistAccentsSearchResults.Results.Count > 0) |
| | | 76 | | { |
| | 0 | 77 | | return GetResultsFromResponse(artistAccentsSearchResults.Results); |
| | | 78 | | } |
| | | 79 | | } |
| | | 80 | | |
| | 0 | 81 | | return []; |
| | 0 | 82 | | } |
| | | 83 | | |
| | | 84 | | private IEnumerable<RemoteSearchResult> GetResultsFromResponse(IEnumerable<ISearchResult<IArtist>>? releaseSearchRes |
| | | 85 | | { |
| | 0 | 86 | | if (releaseSearchResults is null) |
| | | 87 | | { |
| | 0 | 88 | | yield break; |
| | | 89 | | } |
| | | 90 | | |
| | 0 | 91 | | foreach (var result in releaseSearchResults) |
| | | 92 | | { |
| | 0 | 93 | | yield return GetResultFromResponse(result.Item); |
| | | 94 | | } |
| | 0 | 95 | | } |
| | | 96 | | |
| | | 97 | | private RemoteSearchResult GetResultFromResponse(IArtist artist) |
| | | 98 | | { |
| | 0 | 99 | | var searchResult = new RemoteSearchResult |
| | 0 | 100 | | { |
| | 0 | 101 | | Name = artist.Name, |
| | 0 | 102 | | ProductionYear = artist.LifeSpan?.Begin?.Year, |
| | 0 | 103 | | PremiereDate = artist.LifeSpan?.Begin?.NearestDate, |
| | 0 | 104 | | SearchProviderName = Name, |
| | 0 | 105 | | }; |
| | | 106 | | |
| | 0 | 107 | | searchResult.SetProviderId(MetadataProvider.MusicBrainzArtist, artist.Id.ToString()); |
| | | 108 | | |
| | 0 | 109 | | return searchResult; |
| | | 110 | | } |
| | | 111 | | |
| | | 112 | | /// <inheritdoc /> |
| | | 113 | | public async Task<MetadataResult<MusicArtist>> GetMetadata(ArtistInfo info, CancellationToken cancellationToken) |
| | | 114 | | { |
| | 0 | 115 | | var result = new MetadataResult<MusicArtist> { Item = new MusicArtist() }; |
| | | 116 | | |
| | 0 | 117 | | var musicBrainzId = MusicBrainzQueryExtensions.ParseMusicBrainzId(info.GetMusicBrainzArtistId(), "artist", _logg |
| | | 118 | | |
| | | 119 | | // If we don't have an id yet, resolve one by name so we can look the artist up. |
| | 0 | 120 | | if (musicBrainzId is null) |
| | | 121 | | { |
| | 0 | 122 | | var searchResults = await GetSearchResults(info, cancellationToken).ConfigureAwait(false); |
| | 0 | 123 | | musicBrainzId = MusicBrainzQueryExtensions.ParseMusicBrainzId(searchResults.FirstOrDefault()?.GetProviderId( |
| | | 124 | | } |
| | | 125 | | |
| | 0 | 126 | | if (musicBrainzId is null) |
| | | 127 | | { |
| | 0 | 128 | | return result; |
| | | 129 | | } |
| | | 130 | | |
| | 0 | 131 | | var query = Plugin.Instance!.MusicBrainzQuery; |
| | 0 | 132 | | var artist = await query.LookupArtistOrNullAsync(musicBrainzId.Value, Include.Genres | Include.Tags, _logger, ca |
| | | 133 | | |
| | 0 | 134 | | if (artist is null) |
| | | 135 | | { |
| | 0 | 136 | | return result; |
| | | 137 | | } |
| | | 138 | | |
| | 0 | 139 | | result.HasMetadata = true; |
| | 0 | 140 | | result.Item.SetProviderId(MetadataProvider.MusicBrainzArtist, artist.Id.ToString()); |
| | | 141 | | |
| | 0 | 142 | | if (Plugin.Instance!.Configuration.ReplaceArtistName && !string.IsNullOrWhiteSpace(artist.Name)) |
| | | 143 | | { |
| | 0 | 144 | | result.Item.Name = artist.Name; |
| | | 145 | | } |
| | | 146 | | |
| | 0 | 147 | | if (artist.LifeSpan?.Begin is not null) |
| | | 148 | | { |
| | 0 | 149 | | result.Item.PremiereDate = artist.LifeSpan.Begin.NearestDate; |
| | 0 | 150 | | result.Item.ProductionYear = artist.LifeSpan.Begin.Year; |
| | | 151 | | } |
| | | 152 | | |
| | 0 | 153 | | if (artist.LifeSpan?.End is not null) |
| | | 154 | | { |
| | 0 | 155 | | result.Item.EndDate = artist.LifeSpan.End.NearestDate; |
| | | 156 | | } |
| | | 157 | | |
| | 0 | 158 | | var location = string.IsNullOrWhiteSpace(artist.Area?.Name) ? artist.Country : artist.Area!.Name; |
| | 0 | 159 | | if (!string.IsNullOrWhiteSpace(location)) |
| | | 160 | | { |
| | 0 | 161 | | result.Item.ProductionLocations = [location]; |
| | | 162 | | } |
| | | 163 | | |
| | 0 | 164 | | if (artist.Genres is not null && artist.Genres.Count > 0) |
| | | 165 | | { |
| | 0 | 166 | | result.Item.Genres = artist.Genres |
| | 0 | 167 | | .OrderByDescending(genre => genre.VoteCount) |
| | 0 | 168 | | .Select(genre => genre.Name) |
| | 0 | 169 | | .Where(name => !string.IsNullOrWhiteSpace(name)) |
| | 0 | 170 | | .ToArray(); |
| | | 171 | | } |
| | | 172 | | |
| | 0 | 173 | | if (artist.Tags is not null && artist.Tags.Count > 0) |
| | | 174 | | { |
| | 0 | 175 | | result.Item.Tags = artist.Tags |
| | 0 | 176 | | .OrderByDescending(tag => tag.VoteCount) |
| | 0 | 177 | | .Select(tag => tag.Name) |
| | 0 | 178 | | .Where(name => !string.IsNullOrWhiteSpace(name)) |
| | 0 | 179 | | .ToArray(); |
| | | 180 | | } |
| | | 181 | | |
| | 0 | 182 | | return result; |
| | 0 | 183 | | } |
| | | 184 | | |
| | | 185 | | /// <inheritdoc /> |
| | | 186 | | public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken) |
| | | 187 | | { |
| | 0 | 188 | | throw new NotImplementedException(); |
| | | 189 | | } |
| | | 190 | | } |