| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Net.Http; |
| | | 5 | | using System.Runtime.CompilerServices; |
| | | 6 | | using System.Threading; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using Jellyfin.Extensions; |
| | | 9 | | using MediaBrowser.Controller.Entities.Audio; |
| | | 10 | | using MediaBrowser.Controller.Providers; |
| | | 11 | | using MediaBrowser.Model.Entities; |
| | | 12 | | using MediaBrowser.Model.Providers; |
| | | 13 | | using MediaBrowser.Providers.Music; |
| | | 14 | | using MetaBrainz.MusicBrainz; |
| | | 15 | | using MetaBrainz.MusicBrainz.Interfaces.Entities; |
| | | 16 | | using MetaBrainz.MusicBrainz.Interfaces.Searches; |
| | | 17 | | |
| | | 18 | | namespace MediaBrowser.Providers.Plugins.MusicBrainz; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Music album metadata provider for MusicBrainz. |
| | | 22 | | /// </summary> |
| | | 23 | | public class MusicBrainzAlbumProvider : IRemoteMetadataProvider<MusicAlbum, AlbumInfo>, IHasOrder |
| | | 24 | | { |
| | | 25 | | /// <inheritdoc /> |
| | 0 | 26 | | public string Name => "MusicBrainz"; |
| | | 27 | | |
| | | 28 | | /// <inheritdoc /> |
| | 0 | 29 | | public int Order => 0; |
| | | 30 | | |
| | | 31 | | /// <inheritdoc /> |
| | | 32 | | public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(AlbumInfo searchInfo, CancellationToken cancella |
| | | 33 | | { |
| | 0 | 34 | | var query = MusicBrainz.Plugin.Instance!.MusicBrainzQuery; |
| | 0 | 35 | | var releaseId = searchInfo.GetReleaseId(); |
| | 0 | 36 | | var releaseGroupId = searchInfo.GetReleaseGroupId(); |
| | | 37 | | |
| | 0 | 38 | | if (!string.IsNullOrEmpty(releaseId)) |
| | | 39 | | { |
| | 0 | 40 | | var releaseResult = await query.LookupReleaseAsync(new Guid(releaseId), Include.Artists | Include.ReleaseGro |
| | 0 | 41 | | return GetReleaseResult(releaseResult).SingleItemAsEnumerable(); |
| | | 42 | | } |
| | | 43 | | |
| | 0 | 44 | | if (!string.IsNullOrEmpty(releaseGroupId)) |
| | | 45 | | { |
| | 0 | 46 | | var releaseGroupResult = await query.LookupReleaseGroupAsync(new Guid(releaseGroupId), Include.Releases, nul |
| | | 47 | | |
| | | 48 | | // No need to pass the cancellation token to GetReleaseGroupResultAsync as we're already passing it to ToBlo |
| | 0 | 49 | | return GetReleaseGroupResultAsync(releaseGroupResult.Releases, CancellationToken.None).ToBlockingEnumerable( |
| | | 50 | | } |
| | | 51 | | |
| | 0 | 52 | | var artistMusicBrainzId = searchInfo.GetMusicBrainzArtistId(); |
| | | 53 | | |
| | 0 | 54 | | if (!string.IsNullOrWhiteSpace(artistMusicBrainzId)) |
| | | 55 | | { |
| | 0 | 56 | | var releaseSearchResults = await query.FindReleasesAsync($"\"{searchInfo.Name}\" AND arid:{artistMusicBrainz |
| | 0 | 57 | | .ConfigureAwait(false); |
| | | 58 | | |
| | 0 | 59 | | if (releaseSearchResults.Results.Count > 0) |
| | | 60 | | { |
| | 0 | 61 | | return GetReleaseSearchResult(releaseSearchResults.Results); |
| | | 62 | | } |
| | | 63 | | } |
| | | 64 | | else |
| | | 65 | | { |
| | | 66 | | // I'm sure there is a better way but for now it resolves search for 12" Mixes |
| | 0 | 67 | | var queryName = searchInfo.Name.Replace("\"", string.Empty, StringComparison.Ordinal); |
| | | 68 | | |
| | 0 | 69 | | var releaseSearchResults = await query.FindReleasesAsync($"\"{queryName}\" AND artist:\"{searchInfo.GetAlbum |
| | 0 | 70 | | .ConfigureAwait(false); |
| | | 71 | | |
| | 0 | 72 | | if (releaseSearchResults.Results.Count > 0) |
| | | 73 | | { |
| | 0 | 74 | | return GetReleaseSearchResult(releaseSearchResults.Results); |
| | | 75 | | } |
| | | 76 | | } |
| | | 77 | | |
| | 0 | 78 | | return Enumerable.Empty<RemoteSearchResult>(); |
| | 0 | 79 | | } |
| | | 80 | | |
| | | 81 | | private IEnumerable<RemoteSearchResult> GetReleaseSearchResult(IEnumerable<ISearchResult<IRelease>>? releaseSearchRe |
| | | 82 | | { |
| | 0 | 83 | | if (releaseSearchResults is null) |
| | | 84 | | { |
| | 0 | 85 | | yield break; |
| | | 86 | | } |
| | | 87 | | |
| | 0 | 88 | | foreach (var result in releaseSearchResults) |
| | | 89 | | { |
| | 0 | 90 | | yield return GetReleaseResult(result.Item); |
| | | 91 | | } |
| | 0 | 92 | | } |
| | | 93 | | |
| | | 94 | | private async IAsyncEnumerable<RemoteSearchResult> GetReleaseGroupResultAsync(IEnumerable<IRelease>? releaseSearchRe |
| | | 95 | | { |
| | 0 | 96 | | if (releaseSearchResults is null) |
| | | 97 | | { |
| | 0 | 98 | | yield break; |
| | | 99 | | } |
| | | 100 | | |
| | 0 | 101 | | var query = MusicBrainz.Plugin.Instance!.MusicBrainzQuery; |
| | 0 | 102 | | foreach (var result in releaseSearchResults) |
| | | 103 | | { |
| | | 104 | | // Fetch full release info, otherwise artists are missing |
| | 0 | 105 | | var fullResult = await query.LookupReleaseAsync(result.Id, Include.Artists | Include.ReleaseGroups, cancella |
| | 0 | 106 | | yield return GetReleaseResult(fullResult); |
| | | 107 | | } |
| | 0 | 108 | | } |
| | | 109 | | |
| | | 110 | | private RemoteSearchResult GetReleaseResult(IRelease releaseSearchResult) |
| | | 111 | | { |
| | 0 | 112 | | var searchResult = new RemoteSearchResult |
| | 0 | 113 | | { |
| | 0 | 114 | | Name = releaseSearchResult.Title, |
| | 0 | 115 | | ProductionYear = releaseSearchResult.Date?.Year, |
| | 0 | 116 | | PremiereDate = releaseSearchResult.Date?.NearestDate, |
| | 0 | 117 | | SearchProviderName = Name |
| | 0 | 118 | | }; |
| | | 119 | | |
| | | 120 | | // Add artists and use first as album artist |
| | 0 | 121 | | var artists = releaseSearchResult.ArtistCredit; |
| | 0 | 122 | | if (artists is not null && artists.Count > 0) |
| | | 123 | | { |
| | 0 | 124 | | var artistResults = new RemoteSearchResult[artists.Count]; |
| | 0 | 125 | | for (int i = 0; i < artists.Count; i++) |
| | | 126 | | { |
| | 0 | 127 | | var artist = artists[i]; |
| | 0 | 128 | | var artistResult = new RemoteSearchResult |
| | 0 | 129 | | { |
| | 0 | 130 | | Name = artist.Name |
| | 0 | 131 | | }; |
| | | 132 | | |
| | 0 | 133 | | if (artist.Artist?.Id is not null) |
| | | 134 | | { |
| | 0 | 135 | | artistResult.SetProviderId(MetadataProvider.MusicBrainzArtist, artist.Artist!.Id.ToString()); |
| | | 136 | | } |
| | | 137 | | |
| | 0 | 138 | | artistResults[i] = artistResult; |
| | | 139 | | } |
| | | 140 | | |
| | 0 | 141 | | searchResult.AlbumArtist = artistResults[0]; |
| | 0 | 142 | | searchResult.Artists = artistResults; |
| | | 143 | | } |
| | | 144 | | |
| | 0 | 145 | | searchResult.SetProviderId(MetadataProvider.MusicBrainzAlbum, releaseSearchResult.Id.ToString()); |
| | | 146 | | |
| | 0 | 147 | | if (releaseSearchResult.ReleaseGroup?.Id is not null) |
| | | 148 | | { |
| | 0 | 149 | | searchResult.SetProviderId(MetadataProvider.MusicBrainzReleaseGroup, releaseSearchResult.ReleaseGroup.Id.ToS |
| | | 150 | | } |
| | | 151 | | |
| | 0 | 152 | | return searchResult; |
| | | 153 | | } |
| | | 154 | | |
| | | 155 | | /// <inheritdoc /> |
| | | 156 | | public async Task<MetadataResult<MusicAlbum>> GetMetadata(AlbumInfo info, CancellationToken cancellationToken) |
| | | 157 | | { |
| | | 158 | | // TODO: This sets essentially nothing. As-is, it's mostly useless. Make it actually pull metadata and use it. |
| | 0 | 159 | | var query = MusicBrainz.Plugin.Instance!.MusicBrainzQuery; |
| | 0 | 160 | | var releaseId = info.GetReleaseId(); |
| | 0 | 161 | | var releaseGroupId = info.GetReleaseGroupId(); |
| | | 162 | | |
| | 0 | 163 | | var result = new MetadataResult<MusicAlbum> |
| | 0 | 164 | | { |
| | 0 | 165 | | Item = new MusicAlbum() |
| | 0 | 166 | | }; |
| | | 167 | | |
| | | 168 | | // If there is a release group, but no release ID, try to match the release |
| | 0 | 169 | | if (string.IsNullOrWhiteSpace(releaseId) && !string.IsNullOrWhiteSpace(releaseGroupId)) |
| | | 170 | | { |
| | | 171 | | // TODO: Actually try to match the release. Simply taking the first result is stupid. |
| | 0 | 172 | | var releaseGroup = await query.LookupReleaseGroupAsync(new Guid(releaseGroupId), Include.None, null, cancell |
| | 0 | 173 | | var release = releaseGroup.Releases?.Count > 0 ? releaseGroup.Releases[0] : null; |
| | 0 | 174 | | if (release is not null) |
| | | 175 | | { |
| | 0 | 176 | | releaseId = release.Id.ToString(); |
| | 0 | 177 | | result.HasMetadata = true; |
| | | 178 | | } |
| | | 179 | | } |
| | | 180 | | |
| | | 181 | | // If there is no release ID, lookup a release with the info we have |
| | 0 | 182 | | if (string.IsNullOrWhiteSpace(releaseId)) |
| | | 183 | | { |
| | 0 | 184 | | var artistMusicBrainzId = info.GetMusicBrainzArtistId(); |
| | 0 | 185 | | IRelease? releaseResult = null; |
| | | 186 | | |
| | 0 | 187 | | if (!string.IsNullOrEmpty(artistMusicBrainzId)) |
| | | 188 | | { |
| | 0 | 189 | | var releaseSearchResults = await query.FindReleasesAsync($"\"{info.Name}\" AND arid:{artistMusicBrainzId |
| | 0 | 190 | | .ConfigureAwait(false); |
| | 0 | 191 | | releaseResult = releaseSearchResults.Results.Count > 0 ? releaseSearchResults.Results[0].Item : null; |
| | | 192 | | } |
| | 0 | 193 | | else if (!string.IsNullOrEmpty(info.GetAlbumArtist())) |
| | | 194 | | { |
| | 0 | 195 | | var releaseSearchResults = await query.FindReleasesAsync($"\"{info.Name}\" AND artist:{info.GetAlbumArti |
| | 0 | 196 | | .ConfigureAwait(false); |
| | 0 | 197 | | releaseResult = releaseSearchResults.Results.Count > 0 ? releaseSearchResults.Results[0].Item : null; |
| | | 198 | | } |
| | | 199 | | |
| | 0 | 200 | | if (releaseResult is not null) |
| | | 201 | | { |
| | 0 | 202 | | releaseId = releaseResult.Id.ToString(); |
| | | 203 | | |
| | 0 | 204 | | if (releaseResult.ReleaseGroup?.Id is not null) |
| | | 205 | | { |
| | 0 | 206 | | releaseGroupId = releaseResult.ReleaseGroup.Id.ToString(); |
| | | 207 | | } |
| | | 208 | | |
| | 0 | 209 | | result.HasMetadata = true; |
| | 0 | 210 | | result.Item.ProductionYear = releaseResult.Date?.Year; |
| | 0 | 211 | | result.Item.Overview = releaseResult.Annotation; |
| | | 212 | | } |
| | | 213 | | } |
| | | 214 | | |
| | | 215 | | // If we have a release ID but not a release group ID, lookup the release group |
| | 0 | 216 | | if (!string.IsNullOrWhiteSpace(releaseId) && string.IsNullOrWhiteSpace(releaseGroupId)) |
| | | 217 | | { |
| | 0 | 218 | | var release = await query.LookupReleaseAsync(new Guid(releaseId), Include.ReleaseGroups, cancellationToken). |
| | 0 | 219 | | releaseGroupId = release.ReleaseGroup?.Id.ToString(); |
| | 0 | 220 | | result.HasMetadata = true; |
| | | 221 | | } |
| | | 222 | | |
| | | 223 | | // If we have a release ID and a release group ID |
| | 0 | 224 | | if (!string.IsNullOrWhiteSpace(releaseId) || !string.IsNullOrWhiteSpace(releaseGroupId)) |
| | | 225 | | { |
| | 0 | 226 | | result.HasMetadata = true; |
| | | 227 | | } |
| | | 228 | | |
| | 0 | 229 | | if (result.HasMetadata) |
| | | 230 | | { |
| | 0 | 231 | | if (!string.IsNullOrEmpty(releaseId)) |
| | | 232 | | { |
| | 0 | 233 | | result.Item.SetProviderId(MetadataProvider.MusicBrainzAlbum, releaseId); |
| | | 234 | | } |
| | | 235 | | |
| | 0 | 236 | | if (!string.IsNullOrEmpty(releaseGroupId)) |
| | | 237 | | { |
| | 0 | 238 | | result.Item.SetProviderId(MetadataProvider.MusicBrainzReleaseGroup, releaseGroupId); |
| | | 239 | | } |
| | | 240 | | } |
| | | 241 | | |
| | 0 | 242 | | return result; |
| | 0 | 243 | | } |
| | | 244 | | |
| | | 245 | | /// <inheritdoc /> |
| | | 246 | | public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken) |
| | | 247 | | { |
| | 0 | 248 | | throw new NotImplementedException(); |
| | | 249 | | } |
| | | 250 | | } |