152 lines
4.7 KiB
C#
152 lines
4.7 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>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));
|
|
}
|
|
}
|
|
}
|