111 lines
3.7 KiB
C#
111 lines
3.7 KiB
C#
using Jellyfin.Data.Enums;
|
|
using Jellyfin.Plugin.YandexMusic.Providers;
|
|
using MediaBrowser.Controller.Entities;
|
|
using MediaBrowser.Controller.Entities.Audio;
|
|
using MediaBrowser.Controller.Library;
|
|
using MediaBrowser.Model.Tasks;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace Jellyfin.Plugin.YandexMusic;
|
|
|
|
/// <summary>
|
|
/// Fills the AlbumArtists field for existing audio items. Jellyfin's Audio metadata merge
|
|
/// ignores this field, which is why artist and album pages stay empty.
|
|
/// </summary>
|
|
public class AlbumArtistTask : IScheduledTask
|
|
{
|
|
private readonly ILibraryManager _libraryManager;
|
|
private readonly ILogger<AlbumArtistTask> _logger;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="AlbumArtistTask"/> class.
|
|
/// </summary>
|
|
public AlbumArtistTask(ILibraryManager libraryManager, ILogger<AlbumArtistTask> logger)
|
|
{
|
|
_libraryManager = libraryManager;
|
|
_logger = logger;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public string Name => "Yandex Music: создать исполнителей";
|
|
|
|
/// <inheritdoc />
|
|
public string Key => "YandexMusicAlbumArtists";
|
|
|
|
/// <inheritdoc />
|
|
public string Description => "Заполняет AlbumArtists и связи исполнителей, чтобы Jellyfin создал страницы исполнителей.";
|
|
|
|
/// <inheritdoc />
|
|
public string Category => "Yandex Music";
|
|
|
|
/// <inheritdoc />
|
|
public async Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
|
|
{
|
|
var items = _libraryManager.GetItemList(new InternalItemsQuery
|
|
{
|
|
Recursive = true,
|
|
IncludeItemTypes = [BaseItemKind.Audio],
|
|
IsVirtualItem = false
|
|
}).OfType<Audio>().ToList();
|
|
|
|
var updated = 0;
|
|
for (var i = 0; i < items.Count; i++)
|
|
{
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
var item = items[i];
|
|
|
|
string[] artists;
|
|
if (AlbumArtistCache.TryGet(item.Path, out var cached))
|
|
{
|
|
artists = cached;
|
|
}
|
|
else if (item.Artists.Count > 0)
|
|
{
|
|
artists = [item.Artists[0]];
|
|
}
|
|
else
|
|
{
|
|
artists = [];
|
|
}
|
|
|
|
if (artists.Length > 0 && !item.AlbumArtists.SequenceEqual(artists, StringComparer.OrdinalIgnoreCase))
|
|
{
|
|
item.AlbumArtists = artists;
|
|
await item.UpdateToRepositoryAsync(ItemUpdateType.MetadataEdit, cancellationToken).ConfigureAwait(false);
|
|
updated++;
|
|
}
|
|
|
|
var people = new List<MediaBrowser.Controller.Entities.PersonInfo>();
|
|
foreach (var name in item.AlbumArtists)
|
|
{
|
|
people.Add(new MediaBrowser.Controller.Entities.PersonInfo
|
|
{
|
|
Name = name,
|
|
Type = Jellyfin.Data.Enums.PersonKind.AlbumArtist
|
|
});
|
|
}
|
|
|
|
foreach (var name in item.Artists)
|
|
{
|
|
people.Add(new MediaBrowser.Controller.Entities.PersonInfo
|
|
{
|
|
Name = name,
|
|
Type = Jellyfin.Data.Enums.PersonKind.Artist
|
|
});
|
|
}
|
|
|
|
if (people.Count > 0)
|
|
{
|
|
await _libraryManager.UpdatePeopleAsync(item, people, cancellationToken).ConfigureAwait(false);
|
|
}
|
|
|
|
progress.Report(100.0 * (i + 1) / Math.Max(1, items.Count));
|
|
}
|
|
|
|
_logger.LogInformation("Yandex Music: updated album artists for {Updated} of {Total} tracks", updated, items.Count);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public IEnumerable<TaskTriggerInfo> GetDefaultTriggers() => [];
|
|
}
|