From 326ed322780e0db6621ff89b0891f63399bbcb78 Mon Sep 17 00:00:00 2001 From: LJQ Date: Tue, 12 Mar 2024 09:53:15 +0800 Subject: [PATCH 1/2] -Fixes some cache key clashes - Trys to return preferred metadata language when using identify --- .../Providers/TvdbSeriesProvider.cs | 12 +++++------ Jellyfin.Plugin.Tvdb/TvdbClientManager.cs | 4 ++-- Jellyfin.Plugin.Tvdb/TvdbSdkExtensions.cs | 20 ++++++++++++++++++- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/Jellyfin.Plugin.Tvdb/Providers/TvdbSeriesProvider.cs b/Jellyfin.Plugin.Tvdb/Providers/TvdbSeriesProvider.cs index 821f837..6ff44fc 100644 --- a/Jellyfin.Plugin.Tvdb/Providers/TvdbSeriesProvider.cs +++ b/Jellyfin.Plugin.Tvdb/Providers/TvdbSeriesProvider.cs @@ -135,9 +135,9 @@ private async Task> FetchSeriesSearchResult(Seri { var seriesResult = await _tvdbClientManager - .GetSeriesExtendedByIdAsync(tvdbId.Value, seriesInfo.MetadataLanguage, cancellationToken, small: true) + .GetSeriesExtendedByIdAsync(tvdbId.Value, seriesInfo.MetadataLanguage, cancellationToken, meta: Meta4.Translations, small: true) .ConfigureAwait(false); - return new[] { MapSeriesToRemoteSearchResult(seriesResult) }; + return new[] { MapSeriesToRemoteSearchResult(seriesResult, seriesInfo.MetadataLanguage) }; } catch (Exception e) { @@ -146,12 +146,12 @@ await _tvdbClientManager } } - private RemoteSearchResult MapSeriesToRemoteSearchResult(SeriesExtendedRecord series) + private RemoteSearchResult MapSeriesToRemoteSearchResult(SeriesExtendedRecord series, string language) { var remoteResult = new RemoteSearchResult { - Name = series.Name, - Overview = series.Overview?.Trim() ?? string.Empty, + Name = series.Translations.GetTranslatedNamedOrDefault(language) ?? series.Name, + Overview = series.Translations.GetTranslatedOverviewOrDefault(language)?.Trim() ?? series.Overview?.Trim(), SearchProviderName = Name, ImageUrl = series.Image }; @@ -317,7 +317,7 @@ private async Task> FindSeriesInternal(string name, str { var tvdbTitles = new List { - seriesSearchResult.Name + seriesSearchResult.Translations.GetTranslatedNamedOrDefault(language) ?? seriesSearchResult.Name }; if (seriesSearchResult.Aliases is not null) { diff --git a/Jellyfin.Plugin.Tvdb/TvdbClientManager.cs b/Jellyfin.Plugin.Tvdb/TvdbClientManager.cs index dd49e23..f8ccbfb 100644 --- a/Jellyfin.Plugin.Tvdb/TvdbClientManager.cs +++ b/Jellyfin.Plugin.Tvdb/TvdbClientManager.cs @@ -158,7 +158,7 @@ public async Task GetSeriesExtendedByIdAsync( Meta4? meta = null, bool? small = null) { - var key = $"TvdbSeriesExtended_{tvdbId.ToString(CultureInfo.InvariantCulture)}"; + var key = $"TvdbSeriesExtended_{tvdbId.ToString(CultureInfo.InvariantCulture)}_{meta}_{small}"; if (_memoryCache.TryGetValue(key, out SeriesExtendedRecord series)) { return series; @@ -186,7 +186,7 @@ public async Task GetSeriesEpisodesAsync( string seasonType, CancellationToken cancellationToken) { - var key = $"TvdbSeriesEpisodes_{tvdbId.ToString(CultureInfo.InvariantCulture)}"; + var key = $"TvdbSeriesEpisodes_{tvdbId.ToString(CultureInfo.InvariantCulture)}_{seasonType}"; if (_memoryCache.TryGetValue(key, out Data2 series)) { return series; diff --git a/Jellyfin.Plugin.Tvdb/TvdbSdkExtensions.cs b/Jellyfin.Plugin.Tvdb/TvdbSdkExtensions.cs index 5d32e00..3dcaed8 100644 --- a/Jellyfin.Plugin.Tvdb/TvdbSdkExtensions.cs +++ b/Jellyfin.Plugin.Tvdb/TvdbSdkExtensions.cs @@ -31,6 +31,19 @@ public static class TvdbSdkExtensions .Name; } + /// + /// Get the translated Name, or . + /// + /// Available translations. + /// Requested language. + /// Translated Name, or . + public static string? GetTranslatedNamedOrDefault(this TranslationSimple? translations, string? language) + { + return translations? + .FirstOrDefault(translation => IsMatch(translation.Key, language)) + .Value; + } + /// /// Get the translated Overview, or . /// @@ -52,6 +65,11 @@ private static bool IsMatch(this Translation translation, string? language) return false; } + return IsMatch(translation.Language, language); + } + + private static bool IsMatch(this string translation, string? language) + { language = language?.ToLowerInvariant() switch { "zh-tw" => "zh", // Unique case for zh-TW @@ -62,7 +80,7 @@ private static bool IsMatch(this Translation translation, string? language) // try to find a match (ISO 639-2) return TvdbCultureInfo.GetCultureInfo(language!)? .ThreeLetterISOLanguageNames? - .Contains(translation.Language, StringComparer.OrdinalIgnoreCase) + .Contains(translation, StringComparer.OrdinalIgnoreCase) ?? false; } From 45eea79bc2dfdeaaa69e681a384f94cda8185dbf Mon Sep 17 00:00:00 2001 From: LJQ Date: Tue, 12 Mar 2024 10:02:33 +0800 Subject: [PATCH 2/2] Reduce code duplication --- Jellyfin.Plugin.Tvdb/TvdbSdkExtensions.cs | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/Jellyfin.Plugin.Tvdb/TvdbSdkExtensions.cs b/Jellyfin.Plugin.Tvdb/TvdbSdkExtensions.cs index 3dcaed8..e6e2a09 100644 --- a/Jellyfin.Plugin.Tvdb/TvdbSdkExtensions.cs +++ b/Jellyfin.Plugin.Tvdb/TvdbSdkExtensions.cs @@ -27,7 +27,7 @@ public static class TvdbSdkExtensions { return translations? .NameTranslations? - .FirstOrDefault(translation => IsMatch(translation, language))? + .FirstOrDefault(translation => IsMatch(translation.Language, language))? .Name; } @@ -54,22 +54,17 @@ public static class TvdbSdkExtensions { return translations? .OverviewTranslations? - .FirstOrDefault(translation => IsMatch(translation, language))? + .FirstOrDefault(translation => IsMatch(translation.Language, language))? .Overview; } - private static bool IsMatch(this Translation translation, string? language) + private static bool IsMatch(this string translation, string? language) { if (string.IsNullOrWhiteSpace(language)) { return false; } - return IsMatch(translation.Language, language); - } - - private static bool IsMatch(this string translation, string? language) - { language = language?.ToLowerInvariant() switch { "zh-tw" => "zh", // Unique case for zh-TW