Release 1.0.0.4: album artists, credits, artist genres, maintenance task
This commit is contained in:
1 parent
84e3ea34c8
commit
0dbf427ce1
17 files changed
+332
-14
No files matched your search
@@ -0,0 +1,36 @@
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace Jellyfin.Plugin.YandexMusic.Providers;
|
||||
|
||||
/// <summary>
|
||||
/// Album artists resolved by the song provider are not merged into Audio items by Jellyfin,
|
||||
/// so they are remembered here for the custom provider which runs right after the merge.
|
||||
/// </summary>
|
||||
public static class AlbumArtistCache
|
||||
{
|
||||
private static readonly ConcurrentDictionary<string, string[]> Cache = new(StringComparer.Ordinal);
|
||||
|
||||
/// <summary>Stores album artists for an audio file path.</summary>
|
||||
public static void Set(string? path, string[] artists)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(path) || artists.Length == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Cache[path] = artists;
|
||||
}
|
||||
|
||||
/// <summary>Gets album artists for an audio file path.</summary>
|
||||
public static bool TryGet(string? path, out string[] artists)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(path) && Cache.TryGetValue(path, out var value))
|
||||
{
|
||||
artists = value;
|
||||
return true;
|
||||
}
|
||||
|
||||
artists = [];
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,62 @@ public static class Mapping
|
||||
|
||||
private static PluginConfiguration Config => Plugin.Instance?.Configuration ?? new PluginConfiguration();
|
||||
|
||||
/// <summary>Adds composers and lyricists from Yandex track credits.</summary>
|
||||
public static void AddCredits(
|
||||
MediaBrowser.Controller.Providers.MetadataResult<MediaBrowser.Controller.Entities.Audio.Audio> result,
|
||||
IEnumerable<CreditDto> credits)
|
||||
{
|
||||
foreach (var name in CreditValues(credits, IsComposerRole).SelectMany(SplitNames))
|
||||
{
|
||||
result.AddPerson(new MediaBrowser.Controller.Entities.PersonInfo
|
||||
{
|
||||
Name = name,
|
||||
Type = Jellyfin.Data.Enums.PersonKind.Composer
|
||||
});
|
||||
}
|
||||
|
||||
foreach (var name in CreditValues(credits, IsLyricistRole).SelectMany(SplitNames))
|
||||
{
|
||||
result.AddPerson(new MediaBrowser.Controller.Entities.PersonInfo
|
||||
{
|
||||
Name = name,
|
||||
Type = Jellyfin.Data.Enums.PersonKind.Lyricist
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Returns credit values whose role matches the predicate.</summary>
|
||||
public static IEnumerable<string> CreditValues(IEnumerable<CreditDto> credits, Func<string?, bool> role)
|
||||
=> credits
|
||||
.Where(c => role(c.Title) && !string.IsNullOrWhiteSpace(c.Value))
|
||||
.Select(c => c.Value!);
|
||||
|
||||
/// <summary>Splits a credit value into individual names.</summary>
|
||||
public static IEnumerable<string> SplitNames(string value)
|
||||
=> value
|
||||
.Split([',', ';'], StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
|
||||
.Where(v => v.Length > 0);
|
||||
|
||||
/// <summary>Gets a value indicating whether the credit role is a composer.</summary>
|
||||
public static bool IsComposerRole(string? title)
|
||||
=> title is not null
|
||||
&& (title.Contains("музык", StringComparison.OrdinalIgnoreCase)
|
||||
|| title.Contains("composer", StringComparison.OrdinalIgnoreCase)
|
||||
|| title.Equals("music", StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
/// <summary>Gets a value indicating whether the credit role is a lyricist.</summary>
|
||||
public static bool IsLyricistRole(string? title)
|
||||
=> title is not null
|
||||
&& (title.Contains("текст", StringComparison.OrdinalIgnoreCase)
|
||||
|| title.Contains("lyric", StringComparison.OrdinalIgnoreCase)
|
||||
|| title.Contains("writer", StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
/// <summary>Gets a value indicating whether the credit role is a label.</summary>
|
||||
public static bool IsLabelRole(string? title)
|
||||
=> title is not null
|
||||
&& (title.Contains("лейбл", StringComparison.OrdinalIgnoreCase)
|
||||
|| title.Contains("label", StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
/// <summary>Gets a provider id from lookup info.</summary>
|
||||
public static long? GetId(IReadOnlyDictionary<string, string>? ids, string key)
|
||||
{
|
||||
|
||||
@@ -71,7 +71,11 @@ public class YandexMusicAlbumProvider : IRemoteMetadataProvider<MusicAlbum, Albu
|
||||
item.AlbumArtists = artists;
|
||||
item.ProductionYear = Mapping.ValidYear(album.Year) ?? info.Year;
|
||||
item.PremiereDate = Mapping.ParseReleaseDate(album.ReleaseDate);
|
||||
item.Genres = await Mapping.GenresAsync(_client, album.Genre, null, cancellationToken)
|
||||
var primaryGenreArtist = album.Artists.FirstOrDefault(a => a.Id > 0);
|
||||
var albumArtistGenres = primaryGenreArtist is null
|
||||
? Array.Empty<string>()
|
||||
: await _client.GetArtistGenresAsync(primaryGenreArtist.Id, cancellationToken).ConfigureAwait(false);
|
||||
item.Genres = await Mapping.GenresAsync(_client, album.Genre, albumArtistGenres, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
var labels = Mapping.Labels(album);
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
using MediaBrowser.Controller.Entities.Audio;
|
||||
using MediaBrowser.Controller.Library;
|
||||
using MediaBrowser.Controller.Providers;
|
||||
using MediaBrowser.Model.Entities;
|
||||
|
||||
namespace Jellyfin.Plugin.YandexMusic.Providers;
|
||||
|
||||
/// <summary>
|
||||
/// Runs after the standard metadata merge and fixes fields that the Audio metadata service
|
||||
/// does not copy from a remote provider (most importantly AlbumArtists).
|
||||
/// </summary>
|
||||
public class YandexMusicAudioCustomProvider : ICustomMetadataProvider<Audio>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public string Name => Mapping.ProviderName;
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task<ItemUpdateType> FetchAsync(Audio item, MetadataRefreshOptions options, CancellationToken cancellationToken)
|
||||
{
|
||||
if (AlbumArtistCache.TryGet(item.Path, out var albumArtists) && albumArtists.Length > 0)
|
||||
{
|
||||
if (!item.AlbumArtists.SequenceEqual(albumArtists, StringComparer.OrdinalIgnoreCase))
|
||||
{
|
||||
item.AlbumArtists = albumArtists;
|
||||
return Task.FromResult(ItemUpdateType.MetadataEdit);
|
||||
}
|
||||
|
||||
return Task.FromResult(ItemUpdateType.None);
|
||||
}
|
||||
|
||||
if (item.AlbumArtists.Count == 0 && item.Artists.Count > 0)
|
||||
{
|
||||
item.AlbumArtists = [item.Artists[0]];
|
||||
return Task.FromResult(ItemUpdateType.MetadataEdit);
|
||||
}
|
||||
|
||||
return Task.FromResult(ItemUpdateType.None);
|
||||
}
|
||||
}
|
||||
@@ -91,6 +91,7 @@ public class YandexMusicSongProvider : IRemoteMetadataProvider<Audio, SongInfo>,
|
||||
|
||||
audio.Artists = artistNames;
|
||||
audio.AlbumArtists = albumArtistNames;
|
||||
AlbumArtistCache.Set(info.Path, albumArtistNames);
|
||||
audio.Album = config.UseYandexAlbumName
|
||||
? album?.Title ?? info.Album
|
||||
: string.IsNullOrWhiteSpace(info.Album) ? album?.Title : info.Album;
|
||||
@@ -101,15 +102,32 @@ public class YandexMusicSongProvider : IRemoteMetadataProvider<Audio, SongInfo>,
|
||||
}
|
||||
|
||||
audio.PremiereDate = Mapping.ParseReleaseDate(album?.ReleaseDate);
|
||||
audio.Genres = await Mapping.GenresAsync(_client, album?.Genre, null, cancellationToken)
|
||||
var primaryArtistForGenres = track.Artists.FirstOrDefault(a => a.Id > 0);
|
||||
var artistGenres = primaryArtistForGenres is null
|
||||
? Array.Empty<string>()
|
||||
: await _client.GetArtistGenresAsync(primaryArtistForGenres.Id, cancellationToken).ConfigureAwait(false);
|
||||
audio.Genres = await Mapping.GenresAsync(_client, album?.Genre, artistGenres, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
var labels = Mapping.Labels(album);
|
||||
if (labels.Length == 0 && !string.IsNullOrWhiteSpace(track.Major?.Name))
|
||||
if (labels.Length == 0 && !string.IsNullOrWhiteSpace(track.Major?.Name) && track.Major.Id > 0)
|
||||
{
|
||||
labels = [track.Major!.Name!];
|
||||
}
|
||||
|
||||
if (config.EnableCredits)
|
||||
{
|
||||
var credits = await _client.GetTrackCreditsAsync(track.Id, cancellationToken).ConfigureAwait(false);
|
||||
Mapping.AddCredits(result, credits);
|
||||
if (labels.Length == 0)
|
||||
{
|
||||
labels = Mapping.CreditValues(credits, Mapping.IsLabelRole)
|
||||
.SelectMany(Mapping.SplitNames)
|
||||
.Distinct(StringComparer.OrdinalIgnoreCase)
|
||||
.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
if (labels.Length > 0)
|
||||
{
|
||||
audio.Studios = labels;
|
||||
|
||||
Reference in new issue
Block a user