| | | 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 | | |
| | | 17 | | namespace MediaBrowser.Providers.Plugins.MusicBrainz; |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// MusicBrainz artist provider. |
| | | 21 | | /// </summary> |
| | | 22 | | public class MusicBrainzArtistProvider : IRemoteMetadataProvider<MusicArtist, ArtistInfo>, IHasOrder |
| | | 23 | | { |
| | | 24 | | /// <inheritdoc /> |
| | 0 | 25 | | public string Name => "MusicBrainz"; |
| | | 26 | | |
| | | 27 | | /// <inheritdoc /> |
| | | 28 | | /// Runs first to populate the MusicBrainz artist ID used by downstream providers. |
| | 0 | 29 | | public int Order => 0; |
| | | 30 | | |
| | | 31 | | /// <inheritdoc /> |
| | | 32 | | public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(ArtistInfo searchInfo, CancellationToken cancell |
| | | 33 | | { |
| | 0 | 34 | | var query = MusicBrainz.Plugin.Instance!.MusicBrainzQuery; |
| | 0 | 35 | | var artistId = searchInfo.GetMusicBrainzArtistId(); |
| | | 36 | | |
| | 0 | 37 | | if (!string.IsNullOrWhiteSpace(artistId)) |
| | | 38 | | { |
| | 0 | 39 | | var artistResult = await query.LookupArtistAsync(new Guid(artistId), Include.Aliases, null, null, cancellati |
| | 0 | 40 | | return GetResultFromResponse(artistResult).SingleItemAsEnumerable(); |
| | | 41 | | } |
| | | 42 | | |
| | 0 | 43 | | if (string.IsNullOrWhiteSpace(searchInfo.Name)) |
| | | 44 | | { |
| | 0 | 45 | | return []; |
| | | 46 | | } |
| | | 47 | | |
| | 0 | 48 | | var artistSearchResults = await query.FindArtistsAsync($"\"{searchInfo.Name}\"", null, null, false, cancellation |
| | 0 | 49 | | .ConfigureAwait(false); |
| | 0 | 50 | | if (artistSearchResults.Results.Count > 0) |
| | | 51 | | { |
| | 0 | 52 | | return GetResultsFromResponse(artistSearchResults.Results); |
| | | 53 | | } |
| | | 54 | | |
| | 0 | 55 | | if (searchInfo.Name.HasDiacritics()) |
| | | 56 | | { |
| | | 57 | | // Try again using the search with an accented characters query |
| | 0 | 58 | | var artistAccentsSearchResults = await query.FindArtistsAsync($"artistaccent:\"{searchInfo.Name}\"", null, n |
| | 0 | 59 | | .ConfigureAwait(false); |
| | 0 | 60 | | if (artistAccentsSearchResults.Results.Count > 0) |
| | | 61 | | { |
| | 0 | 62 | | return GetResultsFromResponse(artistAccentsSearchResults.Results); |
| | | 63 | | } |
| | | 64 | | } |
| | | 65 | | |
| | 0 | 66 | | return []; |
| | 0 | 67 | | } |
| | | 68 | | |
| | | 69 | | private IEnumerable<RemoteSearchResult> GetResultsFromResponse(IEnumerable<ISearchResult<IArtist>>? releaseSearchRes |
| | | 70 | | { |
| | 0 | 71 | | if (releaseSearchResults is null) |
| | | 72 | | { |
| | 0 | 73 | | yield break; |
| | | 74 | | } |
| | | 75 | | |
| | 0 | 76 | | foreach (var result in releaseSearchResults) |
| | | 77 | | { |
| | 0 | 78 | | yield return GetResultFromResponse(result.Item); |
| | | 79 | | } |
| | 0 | 80 | | } |
| | | 81 | | |
| | | 82 | | private RemoteSearchResult GetResultFromResponse(IArtist artist) |
| | | 83 | | { |
| | 0 | 84 | | var searchResult = new RemoteSearchResult |
| | 0 | 85 | | { |
| | 0 | 86 | | Name = artist.Name, |
| | 0 | 87 | | ProductionYear = artist.LifeSpan?.Begin?.Year, |
| | 0 | 88 | | PremiereDate = artist.LifeSpan?.Begin?.NearestDate, |
| | 0 | 89 | | SearchProviderName = Name, |
| | 0 | 90 | | }; |
| | | 91 | | |
| | 0 | 92 | | searchResult.SetProviderId(MetadataProvider.MusicBrainzArtist, artist.Id.ToString()); |
| | | 93 | | |
| | 0 | 94 | | return searchResult; |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | /// <inheritdoc /> |
| | | 98 | | public async Task<MetadataResult<MusicArtist>> GetMetadata(ArtistInfo info, CancellationToken cancellationToken) |
| | | 99 | | { |
| | 0 | 100 | | var result = new MetadataResult<MusicArtist> { Item = new MusicArtist() }; |
| | | 101 | | |
| | 0 | 102 | | var musicBrainzId = info.GetMusicBrainzArtistId(); |
| | | 103 | | |
| | | 104 | | // If we don't have an id yet, resolve one by name so we can look the artist up. |
| | 0 | 105 | | if (string.IsNullOrWhiteSpace(musicBrainzId)) |
| | | 106 | | { |
| | 0 | 107 | | var searchResults = await GetSearchResults(info, cancellationToken).ConfigureAwait(false); |
| | 0 | 108 | | musicBrainzId = searchResults.FirstOrDefault()?.GetProviderId(MetadataProvider.MusicBrainzArtist); |
| | | 109 | | } |
| | | 110 | | |
| | 0 | 111 | | if (string.IsNullOrWhiteSpace(musicBrainzId)) |
| | | 112 | | { |
| | 0 | 113 | | return result; |
| | | 114 | | } |
| | | 115 | | |
| | 0 | 116 | | var query = Plugin.Instance!.MusicBrainzQuery; |
| | 0 | 117 | | var artist = await query.LookupArtistAsync(new Guid(musicBrainzId), Include.Genres | Include.Tags, null, null, c |
| | | 118 | | |
| | 0 | 119 | | if (artist is null) |
| | | 120 | | { |
| | 0 | 121 | | return result; |
| | | 122 | | } |
| | | 123 | | |
| | 0 | 124 | | result.HasMetadata = true; |
| | 0 | 125 | | result.Item.SetProviderId(MetadataProvider.MusicBrainzArtist, artist.Id.ToString()); |
| | | 126 | | |
| | 0 | 127 | | if (Plugin.Instance!.Configuration.ReplaceArtistName && !string.IsNullOrWhiteSpace(artist.Name)) |
| | | 128 | | { |
| | 0 | 129 | | result.Item.Name = artist.Name; |
| | | 130 | | } |
| | | 131 | | |
| | 0 | 132 | | if (artist.LifeSpan?.Begin is not null) |
| | | 133 | | { |
| | 0 | 134 | | result.Item.PremiereDate = artist.LifeSpan.Begin.NearestDate; |
| | 0 | 135 | | result.Item.ProductionYear = artist.LifeSpan.Begin.Year; |
| | | 136 | | } |
| | | 137 | | |
| | 0 | 138 | | if (artist.LifeSpan?.End is not null) |
| | | 139 | | { |
| | 0 | 140 | | result.Item.EndDate = artist.LifeSpan.End.NearestDate; |
| | | 141 | | } |
| | | 142 | | |
| | 0 | 143 | | var location = string.IsNullOrWhiteSpace(artist.Area?.Name) ? artist.Country : artist.Area!.Name; |
| | 0 | 144 | | if (!string.IsNullOrWhiteSpace(location)) |
| | | 145 | | { |
| | 0 | 146 | | result.Item.ProductionLocations = [location]; |
| | | 147 | | } |
| | | 148 | | |
| | 0 | 149 | | if (artist.Genres is not null && artist.Genres.Count > 0) |
| | | 150 | | { |
| | 0 | 151 | | result.Item.Genres = artist.Genres |
| | 0 | 152 | | .OrderByDescending(genre => genre.VoteCount) |
| | 0 | 153 | | .Select(genre => genre.Name) |
| | 0 | 154 | | .Where(name => !string.IsNullOrWhiteSpace(name)) |
| | 0 | 155 | | .ToArray(); |
| | | 156 | | } |
| | | 157 | | |
| | 0 | 158 | | if (artist.Tags is not null && artist.Tags.Count > 0) |
| | | 159 | | { |
| | 0 | 160 | | result.Item.Tags = artist.Tags |
| | 0 | 161 | | .OrderByDescending(tag => tag.VoteCount) |
| | 0 | 162 | | .Select(tag => tag.Name) |
| | 0 | 163 | | .Where(name => !string.IsNullOrWhiteSpace(name)) |
| | 0 | 164 | | .ToArray(); |
| | | 165 | | } |
| | | 166 | | |
| | 0 | 167 | | return result; |
| | 0 | 168 | | } |
| | | 169 | | |
| | | 170 | | /// <inheritdoc /> |
| | | 171 | | public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken) |
| | | 172 | | { |
| | 0 | 173 | | throw new NotImplementedException(); |
| | | 174 | | } |
| | | 175 | | } |