Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes some cache key clashes and other improvements. #123

Merged
merged 2 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Jellyfin.Plugin.Tvdb/Providers/TvdbSeriesProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ private async Task<IEnumerable<RemoteSearchResult>> 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)
{
Expand All @@ -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
};
Expand Down Expand Up @@ -317,7 +317,7 @@ private async Task<List<RemoteSearchResult>> FindSeriesInternal(string name, str
{
var tvdbTitles = new List<string>
{
seriesSearchResult.Name
seriesSearchResult.Translations.GetTranslatedNamedOrDefault(language) ?? seriesSearchResult.Name
};
if (seriesSearchResult.Aliases is not null)
{
Expand Down
4 changes: 2 additions & 2 deletions Jellyfin.Plugin.Tvdb/TvdbClientManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public async Task<SeriesExtendedRecord> 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;
Expand Down Expand Up @@ -186,7 +186,7 @@ public async Task<Data2> 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;
Expand Down
21 changes: 17 additions & 4 deletions Jellyfin.Plugin.Tvdb/TvdbSdkExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,23 @@ public static class TvdbSdkExtensions
{
return translations?
.NameTranslations?
.FirstOrDefault(translation => IsMatch(translation, language))?
.FirstOrDefault(translation => IsMatch(translation.Language, language))?
.Name;
}

/// <summary>
/// Get the translated Name, or <see langword="null"/>.
/// </summary>
/// <param name="translations">Available translations.</param>
/// <param name="language">Requested language.</param>
/// <returns>Translated Name, or <see langword="null"/>.</returns>
public static string? GetTranslatedNamedOrDefault(this TranslationSimple? translations, string? language)
{
return translations?
.FirstOrDefault(translation => IsMatch(translation.Key, language))
.Value;
}

/// <summary>
/// Get the translated Overview, or <see langword="null"/>.
/// </summary>
Expand All @@ -41,11 +54,11 @@ 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))
{
Expand All @@ -62,7 +75,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;
}

Expand Down