| | | 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 | | var artistSearchResults = await query.FindArtistsAsync($"\"{searchInfo.Name}\"", null, null, false, cancellation |
| | 0 | 44 | | .ConfigureAwait(false); |
| | 0 | 45 | | if (artistSearchResults.Results.Count > 0) |
| | | 46 | | { |
| | 0 | 47 | | return GetResultsFromResponse(artistSearchResults.Results); |
| | | 48 | | } |
| | | 49 | | |
| | 0 | 50 | | if (searchInfo.Name.HasDiacritics()) |
| | | 51 | | { |
| | | 52 | | // Try again using the search with an accented characters query |
| | 0 | 53 | | var artistAccentsSearchResults = await query.FindArtistsAsync($"artistaccent:\"{searchInfo.Name}\"", null, n |
| | 0 | 54 | | .ConfigureAwait(false); |
| | 0 | 55 | | if (artistAccentsSearchResults.Results.Count > 0) |
| | | 56 | | { |
| | 0 | 57 | | return GetResultsFromResponse(artistAccentsSearchResults.Results); |
| | | 58 | | } |
| | | 59 | | } |
| | | 60 | | |
| | 0 | 61 | | return Enumerable.Empty<RemoteSearchResult>(); |
| | 0 | 62 | | } |
| | | 63 | | |
| | | 64 | | private IEnumerable<RemoteSearchResult> GetResultsFromResponse(IEnumerable<ISearchResult<IArtist>>? releaseSearchRes |
| | | 65 | | { |
| | 0 | 66 | | if (releaseSearchResults is null) |
| | | 67 | | { |
| | 0 | 68 | | yield break; |
| | | 69 | | } |
| | | 70 | | |
| | 0 | 71 | | foreach (var result in releaseSearchResults) |
| | | 72 | | { |
| | 0 | 73 | | yield return GetResultFromResponse(result.Item); |
| | | 74 | | } |
| | 0 | 75 | | } |
| | | 76 | | |
| | | 77 | | private RemoteSearchResult GetResultFromResponse(IArtist artist) |
| | | 78 | | { |
| | 0 | 79 | | var searchResult = new RemoteSearchResult |
| | 0 | 80 | | { |
| | 0 | 81 | | Name = artist.Name, |
| | 0 | 82 | | ProductionYear = artist.LifeSpan?.Begin?.Year, |
| | 0 | 83 | | PremiereDate = artist.LifeSpan?.Begin?.NearestDate, |
| | 0 | 84 | | SearchProviderName = Name, |
| | 0 | 85 | | }; |
| | | 86 | | |
| | 0 | 87 | | searchResult.SetProviderId(MetadataProvider.MusicBrainzArtist, artist.Id.ToString()); |
| | | 88 | | |
| | 0 | 89 | | return searchResult; |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | /// <inheritdoc /> |
| | | 93 | | public async Task<MetadataResult<MusicArtist>> GetMetadata(ArtistInfo info, CancellationToken cancellationToken) |
| | | 94 | | { |
| | 0 | 95 | | var result = new MetadataResult<MusicArtist> { Item = new MusicArtist() }; |
| | | 96 | | |
| | 0 | 97 | | var musicBrainzId = info.GetMusicBrainzArtistId(); |
| | | 98 | | |
| | 0 | 99 | | if (string.IsNullOrWhiteSpace(musicBrainzId)) |
| | | 100 | | { |
| | 0 | 101 | | var searchResults = await GetSearchResults(info, cancellationToken).ConfigureAwait(false); |
| | | 102 | | |
| | 0 | 103 | | var singleResult = searchResults.FirstOrDefault(); |
| | | 104 | | |
| | 0 | 105 | | if (singleResult is not null) |
| | | 106 | | { |
| | 0 | 107 | | musicBrainzId = singleResult.GetProviderId(MetadataProvider.MusicBrainzArtist); |
| | 0 | 108 | | result.Item.Overview = singleResult.Overview; |
| | | 109 | | |
| | 0 | 110 | | if (Plugin.Instance!.Configuration.ReplaceArtistName) |
| | | 111 | | { |
| | 0 | 112 | | result.Item.Name = singleResult.Name; |
| | | 113 | | } |
| | | 114 | | } |
| | | 115 | | } |
| | | 116 | | |
| | 0 | 117 | | if (!string.IsNullOrWhiteSpace(musicBrainzId)) |
| | | 118 | | { |
| | 0 | 119 | | result.HasMetadata = true; |
| | 0 | 120 | | result.Item.SetProviderId(MetadataProvider.MusicBrainzArtist, musicBrainzId); |
| | | 121 | | } |
| | | 122 | | |
| | 0 | 123 | | return result; |
| | 0 | 124 | | } |
| | | 125 | | |
| | | 126 | | /// <inheritdoc /> |
| | | 127 | | public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken) |
| | | 128 | | { |
| | 0 | 129 | | throw new NotImplementedException(); |
| | | 130 | | } |
| | | 131 | | } |