Add MusicBrainz server validation and fallback (#8833)

This commit is contained in:
Shadowghost 2022-12-03 16:47:50 +01:00 committed by GitHub
parent e2cea6121a
commit fe3e7979b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 5 deletions

View File

@ -13,6 +13,7 @@ using MediaBrowser.Providers.Music;
using MetaBrainz.MusicBrainz;
using MetaBrainz.MusicBrainz.Interfaces.Entities;
using MetaBrainz.MusicBrainz.Interfaces.Searches;
using Microsoft.Extensions.Logging;
namespace MediaBrowser.Providers.Plugins.MusicBrainz;
@ -21,16 +22,36 @@ namespace MediaBrowser.Providers.Plugins.MusicBrainz;
/// </summary>
public class MusicBrainzAlbumProvider : IRemoteMetadataProvider<MusicAlbum, AlbumInfo>, IHasOrder, IDisposable
{
private readonly ILogger<MusicBrainzAlbumProvider> _logger;
private readonly Query _musicBrainzQuery;
private readonly string _musicBrainzDefaultUri = "https://musicbrainz.org";
/// <summary>
/// Initializes a new instance of the <see cref="MusicBrainzAlbumProvider"/> class.
/// </summary>
public MusicBrainzAlbumProvider()
/// <param name="logger">The logger.</param>
public MusicBrainzAlbumProvider(ILogger<MusicBrainzAlbumProvider> logger)
{
_logger = logger;
MusicBrainz.Plugin.Instance!.ConfigurationChanged += (_, _) =>
{
Query.DefaultServer = MusicBrainz.Plugin.Instance.Configuration.Server;
if (Uri.TryCreate(MusicBrainz.Plugin.Instance.Configuration.Server, UriKind.Absolute, out var server))
{
Query.DefaultServer = server.Host;
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(_musicBrainzDefaultUri);
Query.DefaultServer = defaultServer.Host;
Query.DefaultPort = defaultServer.Port;
Query.DefaultUrlScheme = defaultServer.Scheme;
}
Query.DelayBetweenRequests = MusicBrainz.Plugin.Instance.Configuration.RateLimit;
};

View File

@ -4,7 +4,6 @@ using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using Jellyfin.Extensions;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Providers;
@ -14,6 +13,7 @@ using MediaBrowser.Providers.Music;
using MetaBrainz.MusicBrainz;
using MetaBrainz.MusicBrainz.Interfaces.Entities;
using MetaBrainz.MusicBrainz.Interfaces.Searches;
using Microsoft.Extensions.Logging;
namespace MediaBrowser.Providers.Plugins.MusicBrainz;
@ -22,16 +22,36 @@ namespace MediaBrowser.Providers.Plugins.MusicBrainz;
/// </summary>
public class MusicBrainzArtistProvider : IRemoteMetadataProvider<MusicArtist, ArtistInfo>, IDisposable
{
private readonly ILogger<MusicBrainzArtistProvider> _logger;
private readonly Query _musicBrainzQuery;
private readonly string _musicBrainzDefaultUri = "https://musicbrainz.org";
/// <summary>
/// Initializes a new instance of the <see cref="MusicBrainzArtistProvider"/> class.
/// </summary>
public MusicBrainzArtistProvider()
/// <param name="logger">The logger.</param>
public MusicBrainzArtistProvider(ILogger<MusicBrainzArtistProvider> logger)
{
_logger = logger;
MusicBrainz.Plugin.Instance!.ConfigurationChanged += (_, _) =>
{
Query.DefaultServer = MusicBrainz.Plugin.Instance.Configuration.Server;
if (Uri.TryCreate(MusicBrainz.Plugin.Instance.Configuration.Server, UriKind.Absolute, out var server))
{
Query.DefaultServer = server.Host;
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(_musicBrainzDefaultUri);
Query.DefaultServer = defaultServer.Host;
Query.DefaultPort = defaultServer.Port;
Query.DefaultUrlScheme = defaultServer.Scheme;
}
Query.DelayBetweenRequests = MusicBrainz.Plugin.Instance.Configuration.RateLimit;
};