| | | 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 | | using Microsoft.Extensions.Logging; |
| | | 18 | | |
| | | 19 | | namespace MediaBrowser.Providers.Plugins.MusicBrainz; |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Music album metadata provider for MusicBrainz. |
| | | 23 | | /// </summary> |
| | | 24 | | public class MusicBrainzAlbumProvider : IRemoteMetadataProvider<MusicAlbum, AlbumInfo>, IHasOrder |
| | | 25 | | { |
| | | 26 | | private readonly ILogger<MusicBrainzAlbumProvider> _logger; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Initializes a new instance of the <see cref="MusicBrainzAlbumProvider"/> class. |
| | | 30 | | /// </summary> |
| | | 31 | | /// <param name="logger">The logger.</param> |
| | | 32 | | public MusicBrainzAlbumProvider(ILogger<MusicBrainzAlbumProvider> logger) |
| | | 33 | | { |
| | 27 | 34 | | _logger = logger; |
| | 27 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <inheritdoc /> |
| | 0 | 38 | | public string Name => "MusicBrainz"; |
| | | 39 | | |
| | | 40 | | /// <inheritdoc /> |
| | 0 | 41 | | public int Order => 0; |
| | | 42 | | |
| | | 43 | | /// <inheritdoc /> |
| | | 44 | | public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(AlbumInfo searchInfo, CancellationToken cancella |
| | | 45 | | { |
| | 0 | 46 | | var query = MusicBrainz.Plugin.Instance!.MusicBrainzQuery; |
| | 0 | 47 | | var releaseId = MusicBrainzQueryExtensions.ParseMusicBrainzId(searchInfo.GetReleaseId(), "release", _logger); |
| | 0 | 48 | | var releaseGroupId = MusicBrainzQueryExtensions.ParseMusicBrainzId(searchInfo.GetReleaseGroupId(), "release grou |
| | | 49 | | |
| | 0 | 50 | | if (releaseId is not null) |
| | | 51 | | { |
| | 0 | 52 | | var releaseResult = await query.LookupReleaseOrNullAsync(releaseId.Value, Include.Artists | Include.ReleaseG |
| | 0 | 53 | | if (releaseResult is not null) |
| | | 54 | | { |
| | 0 | 55 | | return GetReleaseResult(releaseResult).SingleItemAsEnumerable(); |
| | | 56 | | } |
| | | 57 | | } |
| | | 58 | | |
| | 0 | 59 | | if (releaseGroupId is not null) |
| | | 60 | | { |
| | 0 | 61 | | var releaseGroupResult = await query.LookupReleaseGroupOrNullAsync(releaseGroupId.Value, Include.Releases, _ |
| | 0 | 62 | | if (releaseGroupResult is not null) |
| | | 63 | | { |
| | | 64 | | // No need to pass the cancellation token to GetReleaseGroupResultAsync as we're already passing it to T |
| | 0 | 65 | | return GetReleaseGroupResultAsync(releaseGroupResult.Releases, CancellationToken.None).ToBlockingEnumera |
| | | 66 | | } |
| | | 67 | | } |
| | | 68 | | |
| | 0 | 69 | | var artistMusicBrainzId = searchInfo.GetMusicBrainzArtistId(); |
| | | 70 | | |
| | 0 | 71 | | if (!string.IsNullOrWhiteSpace(artistMusicBrainzId)) |
| | | 72 | | { |
| | 0 | 73 | | var releaseSearchResults = await query.FindReleasesWithRetryAsync($"\"{searchInfo.Name}\" AND arid:{artistMu |
| | 0 | 74 | | .ConfigureAwait(false); |
| | | 75 | | |
| | 0 | 76 | | if (releaseSearchResults.Results.Count > 0) |
| | | 77 | | { |
| | 0 | 78 | | return GetReleaseSearchResult(releaseSearchResults.Results); |
| | | 79 | | } |
| | | 80 | | } |
| | | 81 | | else |
| | | 82 | | { |
| | | 83 | | // I'm sure there is a better way but for now it resolves search for 12" Mixes |
| | 0 | 84 | | var queryName = searchInfo.Name.Replace("\"", string.Empty, StringComparison.Ordinal); |
| | | 85 | | |
| | 0 | 86 | | var releaseSearchResults = await query.FindReleasesWithRetryAsync($"\"{queryName}\" AND artist:\"{searchInfo |
| | 0 | 87 | | .ConfigureAwait(false); |
| | | 88 | | |
| | 0 | 89 | | if (releaseSearchResults.Results.Count > 0) |
| | | 90 | | { |
| | 0 | 91 | | return GetReleaseSearchResult(releaseSearchResults.Results); |
| | | 92 | | } |
| | | 93 | | } |
| | | 94 | | |
| | 0 | 95 | | return Enumerable.Empty<RemoteSearchResult>(); |
| | 0 | 96 | | } |
| | | 97 | | |
| | | 98 | | private IEnumerable<RemoteSearchResult> GetReleaseSearchResult(IEnumerable<ISearchResult<IRelease>>? releaseSearchRe |
| | | 99 | | { |
| | 0 | 100 | | if (releaseSearchResults is null) |
| | | 101 | | { |
| | 0 | 102 | | yield break; |
| | | 103 | | } |
| | | 104 | | |
| | 0 | 105 | | foreach (var result in releaseSearchResults) |
| | | 106 | | { |
| | 0 | 107 | | yield return GetReleaseResult(result.Item); |
| | | 108 | | } |
| | 0 | 109 | | } |
| | | 110 | | |
| | | 111 | | private async IAsyncEnumerable<RemoteSearchResult> GetReleaseGroupResultAsync(IEnumerable<IRelease>? releaseSearchRe |
| | | 112 | | { |
| | 0 | 113 | | if (releaseSearchResults is null) |
| | | 114 | | { |
| | 0 | 115 | | yield break; |
| | | 116 | | } |
| | | 117 | | |
| | 0 | 118 | | var query = MusicBrainz.Plugin.Instance!.MusicBrainzQuery; |
| | 0 | 119 | | foreach (var result in releaseSearchResults) |
| | | 120 | | { |
| | | 121 | | // Fetch full release info, otherwise artists are missing |
| | 0 | 122 | | var fullResult = await query.LookupReleaseOrNullAsync(result.Id, Include.Artists | Include.ReleaseGroups, _l |
| | 0 | 123 | | if (fullResult is not null) |
| | | 124 | | { |
| | 0 | 125 | | yield return GetReleaseResult(fullResult); |
| | | 126 | | } |
| | | 127 | | } |
| | 0 | 128 | | } |
| | | 129 | | |
| | | 130 | | private RemoteSearchResult GetReleaseResult(IRelease releaseSearchResult) |
| | | 131 | | { |
| | 0 | 132 | | var searchResult = new RemoteSearchResult |
| | 0 | 133 | | { |
| | 0 | 134 | | Name = releaseSearchResult.Title, |
| | 0 | 135 | | ProductionYear = releaseSearchResult.Date?.Year, |
| | 0 | 136 | | PremiereDate = releaseSearchResult.Date?.NearestDate, |
| | 0 | 137 | | SearchProviderName = Name |
| | 0 | 138 | | }; |
| | | 139 | | |
| | | 140 | | // Add artists and use first as album artist |
| | 0 | 141 | | var artists = releaseSearchResult.ArtistCredit; |
| | 0 | 142 | | if (artists is not null && artists.Count > 0) |
| | | 143 | | { |
| | 0 | 144 | | var artistResults = new RemoteSearchResult[artists.Count]; |
| | 0 | 145 | | for (int i = 0; i < artists.Count; i++) |
| | | 146 | | { |
| | 0 | 147 | | var artist = artists[i]; |
| | 0 | 148 | | var artistResult = new RemoteSearchResult |
| | 0 | 149 | | { |
| | 0 | 150 | | Name = artist.Name |
| | 0 | 151 | | }; |
| | | 152 | | |
| | 0 | 153 | | if (artist.Artist?.Id is not null) |
| | | 154 | | { |
| | 0 | 155 | | artistResult.SetProviderId(MetadataProvider.MusicBrainzArtist, artist.Artist!.Id.ToString()); |
| | | 156 | | } |
| | | 157 | | |
| | 0 | 158 | | artistResults[i] = artistResult; |
| | | 159 | | } |
| | | 160 | | |
| | 0 | 161 | | searchResult.AlbumArtist = artistResults[0]; |
| | 0 | 162 | | searchResult.Artists = artistResults; |
| | | 163 | | } |
| | | 164 | | |
| | 0 | 165 | | searchResult.SetProviderId(MetadataProvider.MusicBrainzAlbum, releaseSearchResult.Id.ToString()); |
| | | 166 | | |
| | 0 | 167 | | if (releaseSearchResult.ReleaseGroup?.Id is not null) |
| | | 168 | | { |
| | 0 | 169 | | searchResult.SetProviderId(MetadataProvider.MusicBrainzReleaseGroup, releaseSearchResult.ReleaseGroup.Id.ToS |
| | | 170 | | } |
| | | 171 | | |
| | 0 | 172 | | return searchResult; |
| | | 173 | | } |
| | | 174 | | |
| | | 175 | | /// <inheritdoc /> |
| | | 176 | | public async Task<MetadataResult<MusicAlbum>> GetMetadata(AlbumInfo info, CancellationToken cancellationToken) |
| | | 177 | | { |
| | 0 | 178 | | var query = MusicBrainz.Plugin.Instance!.MusicBrainzQuery; |
| | 0 | 179 | | var releaseId = MusicBrainzQueryExtensions.ParseMusicBrainzId(info.GetReleaseId(), "release", _logger); |
| | 0 | 180 | | var releaseGroupId = MusicBrainzQueryExtensions.ParseMusicBrainzId(info.GetReleaseGroupId(), "release group", _l |
| | | 181 | | |
| | 0 | 182 | | var result = new MetadataResult<MusicAlbum> |
| | 0 | 183 | | { |
| | 0 | 184 | | Item = new MusicAlbum() |
| | 0 | 185 | | }; |
| | | 186 | | |
| | | 187 | | // If there is a release group, but no release ID, try to match the release |
| | 0 | 188 | | if (releaseId is null && releaseGroupId is not null) |
| | | 189 | | { |
| | | 190 | | // TODO: Actually try to match the release. Simply taking the first result is stupid. |
| | 0 | 191 | | var releaseGroupLookup = await query.LookupReleaseGroupOrNullAsync(releaseGroupId.Value, Include.None, _logg |
| | 0 | 192 | | releaseId = releaseGroupLookup?.Releases?.Count > 0 ? releaseGroupLookup.Releases[0].Id : null; |
| | | 193 | | } |
| | | 194 | | |
| | | 195 | | // If there is no release ID, lookup a release with the info we have |
| | 0 | 196 | | if (releaseId is null) |
| | | 197 | | { |
| | 0 | 198 | | var artistMusicBrainzId = info.GetMusicBrainzArtistId(); |
| | 0 | 199 | | IRelease? releaseResult = null; |
| | | 200 | | |
| | 0 | 201 | | if (!string.IsNullOrEmpty(artistMusicBrainzId)) |
| | | 202 | | { |
| | 0 | 203 | | var releaseSearchResults = await query.FindReleasesWithRetryAsync($"\"{info.Name}\" AND arid:{artistMusi |
| | 0 | 204 | | .ConfigureAwait(false); |
| | 0 | 205 | | releaseResult = releaseSearchResults.Results.Count > 0 ? releaseSearchResults.Results[0].Item : null; |
| | | 206 | | } |
| | 0 | 207 | | else if (!string.IsNullOrEmpty(info.GetAlbumArtist())) |
| | | 208 | | { |
| | 0 | 209 | | var releaseSearchResults = await query.FindReleasesWithRetryAsync($"\"{info.Name}\" AND artist:{info.Get |
| | 0 | 210 | | .ConfigureAwait(false); |
| | 0 | 211 | | releaseResult = releaseSearchResults.Results.Count > 0 ? releaseSearchResults.Results[0].Item : null; |
| | | 212 | | } |
| | | 213 | | |
| | 0 | 214 | | if (releaseResult is not null) |
| | | 215 | | { |
| | 0 | 216 | | releaseId = releaseResult.Id; |
| | | 217 | | |
| | 0 | 218 | | if (releaseResult.ReleaseGroup?.Id is not null) |
| | | 219 | | { |
| | 0 | 220 | | releaseGroupId = releaseResult.ReleaseGroup.Id; |
| | | 221 | | } |
| | | 222 | | } |
| | | 223 | | } |
| | | 224 | | |
| | 0 | 225 | | if (releaseId is null && releaseGroupId is null) |
| | | 226 | | { |
| | 0 | 227 | | return result; |
| | | 228 | | } |
| | | 229 | | |
| | | 230 | | // Fetch the full release (and its release group) so we can populate everything MusicBrainz returns. |
| | 0 | 231 | | IRelease? release = null; |
| | 0 | 232 | | if (releaseId is not null) |
| | | 233 | | { |
| | 0 | 234 | | release = await query.LookupReleaseOrNullAsync( |
| | 0 | 235 | | releaseId.Value, |
| | 0 | 236 | | Include.Artists | Include.ReleaseGroups | Include.Labels | Include.Genres | Include.Tags, |
| | 0 | 237 | | _logger, |
| | 0 | 238 | | cancellationToken).ConfigureAwait(false); |
| | | 239 | | |
| | 0 | 240 | | if (releaseGroupId is null && release?.ReleaseGroup?.Id is not null) |
| | | 241 | | { |
| | 0 | 242 | | releaseGroupId = release.ReleaseGroup.Id; |
| | | 243 | | } |
| | | 244 | | } |
| | | 245 | | |
| | 0 | 246 | | IReleaseGroup? releaseGroup = null; |
| | 0 | 247 | | if (releaseGroupId is not null) |
| | | 248 | | { |
| | 0 | 249 | | releaseGroup = await query.LookupReleaseGroupOrNullAsync( |
| | 0 | 250 | | releaseGroupId.Value, |
| | 0 | 251 | | Include.Artists | Include.Genres | Include.Tags, |
| | 0 | 252 | | _logger, |
| | 0 | 253 | | cancellationToken).ConfigureAwait(false); |
| | | 254 | | } |
| | | 255 | | |
| | 0 | 256 | | if (release is null && releaseGroup is null) |
| | | 257 | | { |
| | 0 | 258 | | return result; |
| | | 259 | | } |
| | | 260 | | |
| | 0 | 261 | | result.HasMetadata = true; |
| | | 262 | | |
| | 0 | 263 | | if (releaseId is not null) |
| | | 264 | | { |
| | 0 | 265 | | result.Item.SetProviderId(MetadataProvider.MusicBrainzAlbum, releaseId.Value.ToString()); |
| | | 266 | | } |
| | | 267 | | |
| | 0 | 268 | | if (releaseGroupId is not null) |
| | | 269 | | { |
| | 0 | 270 | | result.Item.SetProviderId(MetadataProvider.MusicBrainzReleaseGroup, releaseGroupId.Value.ToString()); |
| | | 271 | | } |
| | | 272 | | |
| | 0 | 273 | | Populate(result.Item, release, releaseGroup); |
| | | 274 | | |
| | 0 | 275 | | return result; |
| | 0 | 276 | | } |
| | | 277 | | |
| | | 278 | | private static void Populate(MusicAlbum item, IRelease? release, IReleaseGroup? releaseGroup) |
| | | 279 | | { |
| | | 280 | | // Prefer the release group (album-level) data, falling back to the specific release. |
| | | 281 | | // The release group's first release date is the original album date. |
| | 0 | 282 | | var date = releaseGroup?.FirstReleaseDate ?? release?.Date; |
| | 0 | 283 | | if (date is not null) |
| | | 284 | | { |
| | 0 | 285 | | item.PremiereDate = date.NearestDate; |
| | 0 | 286 | | item.ProductionYear = date.Year; |
| | | 287 | | } |
| | | 288 | | |
| | 0 | 289 | | var artistCredit = release?.ArtistCredit ?? releaseGroup?.ArtistCredit; |
| | 0 | 290 | | if (artistCredit is not null && artistCredit.Count > 0) |
| | | 291 | | { |
| | 0 | 292 | | item.AlbumArtists = artistCredit |
| | 0 | 293 | | .Select(credit => credit.Artist?.Name ?? credit.Name) |
| | 0 | 294 | | .Where(name => !string.IsNullOrWhiteSpace(name)) |
| | 0 | 295 | | .ToArray(); |
| | | 296 | | } |
| | | 297 | | |
| | 0 | 298 | | var genres = releaseGroup?.Genres ?? release?.Genres; |
| | 0 | 299 | | if (genres is not null && genres.Count > 0) |
| | | 300 | | { |
| | 0 | 301 | | item.Genres = genres |
| | 0 | 302 | | .OrderByDescending(genre => genre.VoteCount) |
| | 0 | 303 | | .Select(genre => genre.Name) |
| | 0 | 304 | | .Where(name => !string.IsNullOrWhiteSpace(name)) |
| | 0 | 305 | | .ToArray(); |
| | | 306 | | } |
| | | 307 | | |
| | 0 | 308 | | var tags = releaseGroup?.Tags ?? release?.Tags; |
| | 0 | 309 | | if (tags is not null && tags.Count > 0) |
| | | 310 | | { |
| | 0 | 311 | | item.Tags = tags |
| | 0 | 312 | | .OrderByDescending(tag => tag.VoteCount) |
| | 0 | 313 | | .Select(tag => tag.Name) |
| | 0 | 314 | | .Where(name => !string.IsNullOrWhiteSpace(name)) |
| | 0 | 315 | | .ToArray(); |
| | | 316 | | } |
| | | 317 | | |
| | 0 | 318 | | if (release?.LabelInfo is not null && release.LabelInfo.Count > 0) |
| | | 319 | | { |
| | 0 | 320 | | item.Studios = release.LabelInfo |
| | 0 | 321 | | .Where(labelInfo => !string.IsNullOrWhiteSpace(labelInfo.Label?.Name)) |
| | 0 | 322 | | .Select(labelInfo => labelInfo.Label!.Name!) |
| | 0 | 323 | | .Distinct(StringComparer.OrdinalIgnoreCase) |
| | 0 | 324 | | .ToArray(); |
| | | 325 | | } |
| | 0 | 326 | | } |
| | | 327 | | |
| | | 328 | | /// <inheritdoc /> |
| | | 329 | | public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken) |
| | | 330 | | { |
| | 0 | 331 | | throw new NotImplementedException(); |
| | | 332 | | } |
| | | 333 | | } |