208 lines
7.3 KiB
C#
208 lines
7.3 KiB
C#
using System.Globalization;
|
|
using Jellyfin.Plugin.YandexMusic.Configuration;
|
|
using Jellyfin.Plugin.YandexMusic.Yandex;
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
namespace Jellyfin.Plugin.YandexMusic.Providers;
|
|
|
|
/// <summary>Provider id keys used by this plugin.</summary>
|
|
public static class YandexProviderIds
|
|
{
|
|
/// <summary>Track id.</summary>
|
|
public const string Track = "YandexMusicTrack";
|
|
|
|
/// <summary>Album id.</summary>
|
|
public const string Album = "YandexMusicAlbum";
|
|
|
|
/// <summary>Artist id.</summary>
|
|
public const string Artist = "YandexMusicArtist";
|
|
}
|
|
|
|
/// <summary>Shared mapping helpers.</summary>
|
|
public static class Mapping
|
|
{
|
|
/// <summary>Provider display name.</summary>
|
|
public const string ProviderName = "Yandex Music";
|
|
|
|
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)
|
|
{
|
|
if (ids is null || !ids.TryGetValue(key, out var value) || string.IsNullOrWhiteSpace(value))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return long.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var id) ? id : null;
|
|
}
|
|
|
|
/// <summary>Gets a provider id from an item.</summary>
|
|
public static long? GetId(MediaBrowser.Controller.Entities.BaseItem item, string key)
|
|
{
|
|
var value = item.GetProviderId(key);
|
|
return long.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var id) ? id : null;
|
|
}
|
|
|
|
/// <summary>Parses a Yandex release date.</summary>
|
|
public static DateTime? ParseReleaseDate(string? releaseDate)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(releaseDate))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
if (DateTime.TryParse(releaseDate, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out var parsed))
|
|
{
|
|
return parsed.Year is >= 1900 and <= 2100 ? parsed : null;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/// <summary>Validates a release year.</summary>
|
|
public static int? ValidYear(int? year)
|
|
=> year is >= 1900 and <= 2100 ? year : null;
|
|
|
|
/// <summary>Maps a Yandex album genre to a Jellyfin genre name.</summary>
|
|
public static async Task<string[]> GenresAsync(
|
|
YandexMusicClient client,
|
|
string? albumGenre,
|
|
IEnumerable<string>? artistGenres,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
if (!Config.EnableGenres)
|
|
{
|
|
return [];
|
|
}
|
|
|
|
var result = new List<string>();
|
|
var name = await client.GetGenreNameAsync(albumGenre, cancellationToken).ConfigureAwait(false);
|
|
if (!string.IsNullOrWhiteSpace(name))
|
|
{
|
|
result.Add(name);
|
|
}
|
|
|
|
foreach (var genre in (artistGenres ?? []).Take(4))
|
|
{
|
|
var artistGenre = await client.GetGenreNameAsync(genre, cancellationToken).ConfigureAwait(false);
|
|
if (!string.IsNullOrWhiteSpace(artistGenre)
|
|
&& !result.Contains(artistGenre, StringComparer.OrdinalIgnoreCase))
|
|
{
|
|
result.Add(artistGenre);
|
|
}
|
|
}
|
|
|
|
return result.ToArray();
|
|
}
|
|
|
|
/// <summary>Gets label names.</summary>
|
|
public static string[] Labels(AlbumDto? album)
|
|
{
|
|
if (album is null || !Config.EnableLabels)
|
|
{
|
|
return [];
|
|
}
|
|
|
|
return album.Labels
|
|
.Select(l => l.Name)
|
|
.Where(n => !string.IsNullOrWhiteSpace(n))
|
|
.Select(n => n!)
|
|
.Distinct(StringComparer.OrdinalIgnoreCase)
|
|
.ToArray();
|
|
}
|
|
|
|
/// <summary>Adds a primary remote image when allowed.</summary>
|
|
public static void AddPrimaryImage<T>(
|
|
MediaBrowser.Controller.Providers.MetadataResult<T> result,
|
|
string? coverUri,
|
|
int size = 1000)
|
|
{
|
|
if (!Config.EnableImages)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var url = YandexMusicClient.BuildCoverUrl(coverUri, size);
|
|
if (!string.IsNullOrWhiteSpace(url))
|
|
{
|
|
result.RemoteImages.Add((url, MediaBrowser.Model.Entities.ImageType.Primary));
|
|
}
|
|
}
|
|
|
|
/// <summary>Adds a remote image of a given type.</summary>
|
|
public static void AddImage<T>(
|
|
MediaBrowser.Controller.Providers.MetadataResult<T> result,
|
|
string? coverUri,
|
|
MediaBrowser.Model.Entities.ImageType type,
|
|
int size = 1000)
|
|
{
|
|
if (!Config.EnableImages)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var url = YandexMusicClient.BuildCoverUrl(coverUri, size);
|
|
if (!string.IsNullOrWhiteSpace(url))
|
|
{
|
|
result.RemoteImages.Add((url, type));
|
|
}
|
|
}
|
|
}
|