Apply review suggestions

This commit is contained in:
Shadowghost 2022-04-27 13:44:49 +02:00
parent 7ad0c9ba24
commit 385f1cc1b8
5 changed files with 37 additions and 40 deletions

View File

@ -23,11 +23,7 @@ public class PluginConfiguration : BasePluginConfiguration
{
get => _server;
set
{
_server = value.TrimEnd('/');
Query.DefaultServer = _server;
}
set => _server = value.TrimEnd('/');
}
/// <summary>
@ -46,8 +42,6 @@ public class PluginConfiguration : BasePluginConfiguration
{
_rateLimit = value;
}
Query.DelayBetweenRequests = _rateLimit;
}
}

View File

@ -28,6 +28,12 @@ public class MusicBrainzAlbumProvider : IRemoteMetadataProvider<MusicAlbum, Albu
/// </summary>
public MusicBrainzAlbumProvider()
{
MusicBrainz.Plugin.Instance!.ConfigurationChanged += (_, _) =>
{
Query.DefaultServer = MusicBrainz.Plugin.Instance.Configuration.Server;
Query.DelayBetweenRequests = MusicBrainz.Plugin.Instance.Configuration.RateLimit;
};
_musicBrainzQuery = new Query();
}
@ -45,14 +51,14 @@ public class MusicBrainzAlbumProvider : IRemoteMetadataProvider<MusicAlbum, Albu
if (!string.IsNullOrEmpty(releaseId))
{
var releaseResult = await _musicBrainzQuery.LookupReleaseAsync(new Guid(releaseId), Include.Releases, cancellationToken).ConfigureAwait(false);
return GetResultFromResponse(releaseResult).SingleItemAsEnumerable();
var releaseResult = await _musicBrainzQuery.LookupReleaseAsync(new Guid(releaseId), Include.ReleaseGroups, cancellationToken).ConfigureAwait(false);
return GetReleaseResult(releaseResult).SingleItemAsEnumerable();
}
if (!string.IsNullOrEmpty(releaseGroupId))
{
var releaseGroupResult = await _musicBrainzQuery.LookupReleaseGroupAsync(new Guid(releaseGroupId), Include.ReleaseGroups, null, cancellationToken).ConfigureAwait(false);
return GetResultsFromResponse(releaseGroupResult.Releases);
var releaseGroupResult = await _musicBrainzQuery.LookupReleaseGroupAsync(new Guid(releaseGroupId), Include.None, null, cancellationToken).ConfigureAwait(false);
return GetReleaseGroupResult(releaseGroupResult.Releases);
}
var artistMusicBrainzId = searchInfo.GetMusicBrainzArtistId();
@ -64,7 +70,7 @@ public class MusicBrainzAlbumProvider : IRemoteMetadataProvider<MusicAlbum, Albu
if (releaseSearchResults.Results.Count > 0)
{
return GetResultsFromResponse(releaseSearchResults.Results);
return GetReleaseSearchResult(releaseSearchResults.Results);
}
}
else
@ -77,14 +83,14 @@ public class MusicBrainzAlbumProvider : IRemoteMetadataProvider<MusicAlbum, Albu
if (releaseSearchResults.Results.Count > 0)
{
return GetResultsFromResponse(releaseSearchResults.Results);
return GetReleaseSearchResult(releaseSearchResults.Results);
}
}
return Enumerable.Empty<RemoteSearchResult>();
}
private IEnumerable<RemoteSearchResult> GetResultsFromResponse(IEnumerable<ISearchResult<IRelease>>? releaseSearchResults)
private IEnumerable<RemoteSearchResult> GetReleaseSearchResult(IEnumerable<ISearchResult<IRelease>>? releaseSearchResults)
{
if (releaseSearchResults is null)
{
@ -93,11 +99,11 @@ public class MusicBrainzAlbumProvider : IRemoteMetadataProvider<MusicAlbum, Albu
foreach (var result in releaseSearchResults)
{
yield return GetResultFromResponse(result.Item);
yield return GetReleaseResult(result.Item);
}
}
private IEnumerable<RemoteSearchResult> GetResultsFromResponse(IEnumerable<IRelease>? releaseSearchResults)
private IEnumerable<RemoteSearchResult> GetReleaseGroupResult(IEnumerable<IRelease>? releaseSearchResults)
{
if (releaseSearchResults is null)
{
@ -106,11 +112,11 @@ public class MusicBrainzAlbumProvider : IRemoteMetadataProvider<MusicAlbum, Albu
foreach (var result in releaseSearchResults)
{
yield return GetResultFromResponse(result);
yield return GetReleaseResult(result);
}
}
private RemoteSearchResult GetResultFromResponse(IRelease releaseSearchResult)
private RemoteSearchResult GetReleaseResult(IRelease releaseSearchResult)
{
var searchResult = new RemoteSearchResult
{
@ -159,11 +165,14 @@ public class MusicBrainzAlbumProvider : IRemoteMetadataProvider<MusicAlbum, Albu
if (string.IsNullOrWhiteSpace(releaseId) && !string.IsNullOrWhiteSpace(releaseGroupId))
{
// TODO: Actually try to match the release. Simply taking the first result is stupid.
var releaseGroup = await _musicBrainzQuery.LookupReleaseGroupAsync(new Guid(releaseGroupId), Include.ReleaseGroups, null, cancellationToken).ConfigureAwait(false);
var releaseGroup = await _musicBrainzQuery.LookupReleaseGroupAsync(new Guid(releaseGroupId), Include.None, null, cancellationToken).ConfigureAwait(false);
var release = releaseGroup.Releases?.Count > 0 ? releaseGroup.Releases[0] : null;
releaseId = release?.Id.ToString();
if (release != null)
{
releaseId = release.Id.ToString();
result.HasMetadata = true;
}
}
// If there is no release ID, lookup a release with the info we have
if (string.IsNullOrWhiteSpace(releaseId))

View File

@ -6,7 +6,7 @@ using MediaBrowser.Model.Providers;
namespace MediaBrowser.Providers.Plugins.MusicBrainz;
/// <summary>
/// MusicBrains Artist ExternalId.
/// MusicBrainz artist external id.
/// </summary>
public class MusicBrainzArtistExternalId : IExternalId
{

View File

@ -29,6 +29,12 @@ public class MusicBrainzArtistProvider : IRemoteMetadataProvider<MusicArtist, Ar
/// </summary>
public MusicBrainzArtistProvider()
{
MusicBrainz.Plugin.Instance!.ConfigurationChanged += (_, _) =>
{
Query.DefaultServer = MusicBrainz.Plugin.Instance.Configuration.Server;
Query.DelayBetweenRequests = MusicBrainz.Plugin.Instance.Configuration.RateLimit;
};
_musicBrainzQuery = new Query();
}
@ -42,7 +48,7 @@ public class MusicBrainzArtistProvider : IRemoteMetadataProvider<MusicArtist, Ar
if (!string.IsNullOrWhiteSpace(artistId))
{
var artistResult = await _musicBrainzQuery.LookupArtistAsync(new Guid(artistId), Include.Artists, null, null, cancellationToken).ConfigureAwait(false);
var artistResult = await _musicBrainzQuery.LookupArtistAsync(new Guid(artistId), Include.Aliases, null, null, cancellationToken).ConfigureAwait(false);
return GetResultFromResponse(artistResult).SingleItemAsEnumerable();
}
@ -80,19 +86,6 @@ public class MusicBrainzArtistProvider : IRemoteMetadataProvider<MusicArtist, Ar
}
}
private IEnumerable<RemoteSearchResult> GetResultsFromResponse(IEnumerable<IArtist>? releaseSearchResults)
{
if (releaseSearchResults is null)
{
yield break;
}
foreach (var result in releaseSearchResults)
{
yield return GetResultFromResponse(result);
}
}
private RemoteSearchResult GetResultFromResponse(IArtist artist)
{
var searchResult = new RemoteSearchResult

View File

@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using System.Net.Http.Headers;
using System.Reflection;
using MediaBrowser.Common;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Plugins;
using MediaBrowser.Model.Plugins;
@ -21,14 +21,15 @@ public class Plugin : BasePlugin<PluginConfiguration>, IHasWebPages
/// </summary>
/// <param name="applicationPaths">Instance of the <see cref="IApplicationPaths"/> interface.</param>
/// <param name="xmlSerializer">Instance of the <see cref="IXmlSerializer"/> interface.</param>
public Plugin(IApplicationPaths applicationPaths, IXmlSerializer xmlSerializer)
/// <param name="applicationHost">Instance of the <see cref="IApplicationHost"/> interface.</param>
public Plugin(IApplicationPaths applicationPaths, IXmlSerializer xmlSerializer, IApplicationHost applicationHost)
: base(applicationPaths, xmlSerializer)
{
Instance = this;
// TODO: Change this to "JellyfinMusicBrainzPlugin" once we take it out of the server repo.
Query.DefaultUserAgent.Add(new ProductInfoHeaderValue("Jellyfin", Assembly.GetExecutingAssembly().GetName().Version?.ToString(3)));
Query.DefaultUserAgent.Add(new ProductInfoHeaderValue("(apps@jellyfin.org)"));
Query.DefaultUserAgent.Add(new ProductInfoHeaderValue(applicationHost.Name.Replace(' ', '-'), applicationHost.ApplicationVersionString));
Query.DefaultUserAgent.Add(new ProductInfoHeaderValue($"({applicationHost.ApplicationUserAgentAddress})"));
Query.DelayBetweenRequests = Instance.Configuration.RateLimit;
Query.DefaultServer = Instance.Configuration.Server;
}