using System; using System.Collections.Generic; using System.Globalization; namespace Jellyfin.Plugin.RecentlyAddedGuard.Core; /// /// Builds the alias keys that identify a logical medium independent of its file path. /// /// /// Keys are compared case-insensitively by the ledger, so provider ids are only trimmed. /// public static class MediaKeys { /// /// Builds the keys of a movie from its provider ids. /// /// The TMDb id, if known. /// The IMDb id, if known. /// The TVDb id, if known. /// The keys; empty if the movie has no provider id yet. public static IReadOnlyList ForMovie(string? tmdbId, string? imdbId, string? tvdbId) { var keys = new List(3); AddIfPresent(keys, "movie:tmdb:", tmdbId, string.Empty); AddIfPresent(keys, "movie:imdb:", imdbId, string.Empty); AddIfPresent(keys, "movie:tvdb:", tvdbId, string.Empty); return keys; } /// /// Builds the episode part of an episode key, e.g. s1e5 or s1e5-6 for a double episode. /// /// The season number (ParentIndexNumber). /// The episode number (IndexNumber). /// The last episode number of a multi-episode file (IndexNumberEnd). /// The episode part, or null if season or episode number is missing. public static string? EpisodePart(int? seasonNumber, int? episodeNumber, int? episodeNumberEnd) { if (seasonNumber is not { } season || episodeNumber is not { } episode) { return null; } return episodeNumberEnd is { } end && end > episode ? string.Create(CultureInfo.InvariantCulture, $"s{season}e{episode}-{end}") : string.Create(CultureInfo.InvariantCulture, $"s{season}e{episode}"); } /// /// Builds the keys of an episode. /// /// The episode part from . /// The series' TVDb id, if known. /// The series' TMDb id, if known. /// The series' IMDb id, if known. /// The Jellyfin id of the series; if unknown. /// The keys; contains the series-guid key whenever the series id is known. public static IReadOnlyList ForEpisode( string episodePart, string? seriesTvdbId, string? seriesTmdbId, string? seriesImdbId, Guid seriesId) { ArgumentException.ThrowIfNullOrWhiteSpace(episodePart); var suffix = ":" + episodePart; var keys = new List(4); AddIfPresent(keys, "ep:tvdb:", seriesTvdbId, suffix); AddIfPresent(keys, "ep:tmdb:", seriesTmdbId, suffix); AddIfPresent(keys, "ep:imdb:", seriesImdbId, suffix); if (seriesId != Guid.Empty) { keys.Add("ep:series-guid:" + seriesId.ToString("N", CultureInfo.InvariantCulture) + suffix); } return keys; } private static void AddIfPresent(List keys, string prefix, string? id, string suffix) { if (!string.IsNullOrWhiteSpace(id)) { keys.Add(prefix + id.Trim() + suffix); } } }