2019-01-13 21:03:10 +01:00
|
|
|
using System;
|
2014-02-20 05:53:15 +01:00
|
|
|
using System.Collections.Generic;
|
2014-02-02 17:59:14 +01:00
|
|
|
using System.Linq;
|
2020-08-17 21:10:02 +02:00
|
|
|
using System.Net.Http;
|
2014-02-02 17:59:14 +01:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2022-05-05 19:59:17 +02:00
|
|
|
using Jellyfin.Extensions;
|
2019-01-13 20:26:31 +01:00
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
using MediaBrowser.Model.Entities;
|
2023-02-19 16:16:34 +01:00
|
|
|
using MediaBrowser.Model.Plugins;
|
2019-01-13 20:26:31 +01:00
|
|
|
using MediaBrowser.Model.Providers;
|
2022-04-07 12:15:25 +02:00
|
|
|
using MediaBrowser.Providers.Music;
|
2023-02-19 16:16:34 +01:00
|
|
|
using MediaBrowser.Providers.Plugins.MusicBrainz.Configuration;
|
2022-04-07 12:15:25 +02:00
|
|
|
using MetaBrainz.MusicBrainz;
|
|
|
|
using MetaBrainz.MusicBrainz.Interfaces.Entities;
|
|
|
|
using MetaBrainz.MusicBrainz.Interfaces.Searches;
|
2022-12-03 16:47:50 +01:00
|
|
|
using Microsoft.Extensions.Logging;
|
2014-02-02 17:59:14 +01:00
|
|
|
|
2022-04-07 12:15:25 +02:00
|
|
|
namespace MediaBrowser.Providers.Plugins.MusicBrainz;
|
2014-03-14 04:23:58 +01:00
|
|
|
|
2022-04-07 12:15:25 +02:00
|
|
|
/// <summary>
|
|
|
|
/// MusicBrainz artist provider.
|
|
|
|
/// </summary>
|
|
|
|
public class MusicBrainzArtistProvider : IRemoteMetadataProvider<MusicArtist, ArtistInfo>, IDisposable
|
|
|
|
{
|
2022-12-03 16:47:50 +01:00
|
|
|
private readonly ILogger<MusicBrainzArtistProvider> _logger;
|
2023-02-19 16:16:34 +01:00
|
|
|
private Query _musicBrainzQuery;
|
2017-10-20 18:16:56 +02:00
|
|
|
|
2022-04-07 12:15:25 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="MusicBrainzArtistProvider"/> class.
|
|
|
|
/// </summary>
|
2022-12-03 16:47:50 +01:00
|
|
|
/// <param name="logger">The logger.</param>
|
|
|
|
public MusicBrainzArtistProvider(ILogger<MusicBrainzArtistProvider> logger)
|
2022-04-07 12:15:25 +02:00
|
|
|
{
|
2022-12-03 16:47:50 +01:00
|
|
|
_logger = logger;
|
2022-04-07 12:15:25 +02:00
|
|
|
_musicBrainzQuery = new Query();
|
2023-02-19 16:16:34 +01:00
|
|
|
ReloadConfig(null, MusicBrainz.Plugin.Instance!.Configuration);
|
|
|
|
MusicBrainz.Plugin.Instance!.ConfigurationChanged += ReloadConfig;
|
2022-04-07 12:15:25 +02:00
|
|
|
}
|
2014-02-02 17:59:14 +01:00
|
|
|
|
2022-04-07 12:15:25 +02:00
|
|
|
/// <inheritdoc />
|
|
|
|
public string Name => "MusicBrainz";
|
2014-03-14 04:23:58 +01:00
|
|
|
|
2023-02-19 16:16:34 +01:00
|
|
|
private void ReloadConfig(object? sender, BasePluginConfiguration e)
|
|
|
|
{
|
|
|
|
var configuration = (PluginConfiguration)e;
|
|
|
|
if (Uri.TryCreate(configuration.Server, UriKind.Absolute, out var server))
|
|
|
|
{
|
|
|
|
Query.DefaultServer = server.DnsSafeHost;
|
|
|
|
Query.DefaultPort = server.Port;
|
|
|
|
Query.DefaultUrlScheme = server.Scheme;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Fallback to official server
|
|
|
|
_logger.LogWarning("Invalid MusicBrainz server specified, falling back to official server");
|
|
|
|
var defaultServer = new Uri(configuration.Server);
|
|
|
|
Query.DefaultServer = defaultServer.Host;
|
|
|
|
Query.DefaultPort = defaultServer.Port;
|
|
|
|
Query.DefaultUrlScheme = defaultServer.Scheme;
|
|
|
|
}
|
|
|
|
|
|
|
|
Query.DelayBetweenRequests = configuration.RateLimit;
|
|
|
|
_musicBrainzQuery = new Query();
|
|
|
|
}
|
|
|
|
|
2022-04-07 12:15:25 +02:00
|
|
|
/// <inheritdoc />
|
|
|
|
public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(ArtistInfo searchInfo, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
var artistId = searchInfo.GetMusicBrainzArtistId();
|
2014-02-02 17:59:14 +01:00
|
|
|
|
2022-04-07 12:15:25 +02:00
|
|
|
if (!string.IsNullOrWhiteSpace(artistId))
|
|
|
|
{
|
2022-04-27 13:44:49 +02:00
|
|
|
var artistResult = await _musicBrainzQuery.LookupArtistAsync(new Guid(artistId), Include.Aliases, null, null, cancellationToken).ConfigureAwait(false);
|
2022-04-07 12:15:25 +02:00
|
|
|
return GetResultFromResponse(artistResult).SingleItemAsEnumerable();
|
2014-03-14 04:23:58 +01:00
|
|
|
}
|
2014-02-02 17:59:14 +01:00
|
|
|
|
2022-04-07 12:15:25 +02:00
|
|
|
var artistSearchResults = await _musicBrainzQuery.FindArtistsAsync($"\"{searchInfo.Name}\"", null, null, false, cancellationToken)
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
if (artistSearchResults.Results.Count > 0)
|
2014-03-14 04:23:58 +01:00
|
|
|
{
|
2022-04-07 12:15:25 +02:00
|
|
|
return GetResultsFromResponse(artistSearchResults.Results);
|
2016-10-27 21:03:23 +02:00
|
|
|
}
|
|
|
|
|
2022-04-07 12:15:25 +02:00
|
|
|
if (searchInfo.Name.HasDiacritics())
|
2016-10-27 21:03:23 +02:00
|
|
|
{
|
2022-04-07 12:15:25 +02:00
|
|
|
// Try again using the search with an accented characters query
|
|
|
|
var artistAccentsSearchResults = await _musicBrainzQuery.FindArtistsAsync($"artistaccent:\"{searchInfo.Name}\"", null, null, false, cancellationToken)
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
if (artistAccentsSearchResults.Results.Count > 0)
|
2014-02-02 17:59:14 +01:00
|
|
|
{
|
2022-04-07 12:15:25 +02:00
|
|
|
return GetResultsFromResponse(artistAccentsSearchResults.Results);
|
2016-10-27 21:03:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-07 12:15:25 +02:00
|
|
|
return Enumerable.Empty<RemoteSearchResult>();
|
|
|
|
}
|
|
|
|
|
|
|
|
private IEnumerable<RemoteSearchResult> GetResultsFromResponse(IEnumerable<ISearchResult<IArtist>>? releaseSearchResults)
|
|
|
|
{
|
|
|
|
if (releaseSearchResults is null)
|
2016-10-27 21:03:23 +02:00
|
|
|
{
|
2022-04-07 12:15:25 +02:00
|
|
|
yield break;
|
|
|
|
}
|
2014-03-14 04:23:58 +01:00
|
|
|
|
2022-04-07 12:15:25 +02:00
|
|
|
foreach (var result in releaseSearchResults)
|
|
|
|
{
|
|
|
|
yield return GetResultFromResponse(result.Item);
|
|
|
|
}
|
|
|
|
}
|
2016-10-27 23:05:25 +02:00
|
|
|
|
2022-04-07 12:15:25 +02:00
|
|
|
private RemoteSearchResult GetResultFromResponse(IArtist artist)
|
|
|
|
{
|
|
|
|
var searchResult = new RemoteSearchResult
|
|
|
|
{
|
|
|
|
Name = artist.Name,
|
|
|
|
ProductionYear = artist.LifeSpan?.Begin?.Year,
|
2023-02-19 17:54:59 +01:00
|
|
|
PremiereDate = artist.LifeSpan?.Begin?.NearestDate,
|
|
|
|
SearchProviderName = Name,
|
2022-04-07 12:15:25 +02:00
|
|
|
};
|
2020-06-15 23:43:52 +02:00
|
|
|
|
2022-04-07 12:15:25 +02:00
|
|
|
searchResult.SetProviderId(MetadataProvider.MusicBrainzArtist, artist.Id.ToString());
|
2014-02-02 17:59:14 +01:00
|
|
|
|
2022-04-07 12:15:25 +02:00
|
|
|
return searchResult;
|
|
|
|
}
|
2016-10-27 21:03:23 +02:00
|
|
|
|
2022-04-07 12:15:25 +02:00
|
|
|
/// <inheritdoc />
|
|
|
|
public async Task<MetadataResult<MusicArtist>> GetMetadata(ArtistInfo info, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
var result = new MetadataResult<MusicArtist> { Item = new MusicArtist() };
|
2016-10-27 21:03:23 +02:00
|
|
|
|
2022-04-07 12:15:25 +02:00
|
|
|
var musicBrainzId = info.GetMusicBrainzArtistId();
|
2014-03-14 04:23:58 +01:00
|
|
|
|
2022-04-07 12:15:25 +02:00
|
|
|
if (string.IsNullOrWhiteSpace(musicBrainzId))
|
2014-03-14 04:23:58 +01:00
|
|
|
{
|
2022-04-07 12:15:25 +02:00
|
|
|
var searchResults = await GetSearchResults(info, cancellationToken).ConfigureAwait(false);
|
2014-02-02 17:59:14 +01:00
|
|
|
|
2022-04-07 12:15:25 +02:00
|
|
|
var singleResult = searchResults.FirstOrDefault();
|
2014-02-02 17:59:14 +01:00
|
|
|
|
2022-12-05 15:01:13 +01:00
|
|
|
if (singleResult is not null)
|
2014-03-14 04:23:58 +01:00
|
|
|
{
|
2022-04-07 12:15:25 +02:00
|
|
|
musicBrainzId = singleResult.GetProviderId(MetadataProvider.MusicBrainzArtist);
|
|
|
|
result.Item.Overview = singleResult.Overview;
|
2014-03-14 04:23:58 +01:00
|
|
|
|
2022-04-07 12:15:25 +02:00
|
|
|
if (Plugin.Instance!.Configuration.ReplaceArtistName)
|
2014-02-02 17:59:14 +01:00
|
|
|
{
|
2022-04-07 12:15:25 +02:00
|
|
|
result.Item.Name = singleResult.Name;
|
2014-02-02 17:59:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-07 12:15:25 +02:00
|
|
|
if (!string.IsNullOrWhiteSpace(musicBrainzId))
|
2014-02-02 17:59:14 +01:00
|
|
|
{
|
2022-04-07 12:15:25 +02:00
|
|
|
result.HasMetadata = true;
|
|
|
|
result.Item.SetProviderId(MetadataProvider.MusicBrainzArtist, musicBrainzId);
|
2014-02-02 17:59:14 +01:00
|
|
|
}
|
|
|
|
|
2022-04-07 12:15:25 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
Dispose(true);
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Dispose all resources.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="disposing">Whether to dispose.</param>
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
{
|
|
|
|
if (disposing)
|
2014-02-20 05:53:15 +01:00
|
|
|
{
|
2022-04-07 12:15:25 +02:00
|
|
|
_musicBrainzQuery.Dispose();
|
2014-02-20 05:53:15 +01:00
|
|
|
}
|
2014-02-02 17:59:14 +01:00
|
|
|
}
|
|
|
|
}
|