From cecf0a473c1791c4850272b6e4394235adb63196 Mon Sep 17 00:00:00 2001 From: LJQ Date: Fri, 25 Oct 2024 22:58:02 +0800 Subject: [PATCH] Add original countries as tags --- .../Configuration/PluginConfiguration.cs | 5 ++++ .../Configuration/config.html | 6 +++++ .../Providers/TvdbMovieProvider.cs | 12 ++++++++++ .../Providers/TvdbSeriesProvider.cs | 12 ++++++++++ Jellyfin.Plugin.Tvdb/TvdbClientManager.cs | 23 +++++++++++++++++++ 5 files changed, 58 insertions(+) diff --git a/Jellyfin.Plugin.Tvdb/Configuration/PluginConfiguration.cs b/Jellyfin.Plugin.Tvdb/Configuration/PluginConfiguration.cs index 00e57ae..3fdb272 100644 --- a/Jellyfin.Plugin.Tvdb/Configuration/PluginConfiguration.cs +++ b/Jellyfin.Plugin.Tvdb/Configuration/PluginConfiguration.cs @@ -61,6 +61,11 @@ public int CacheDurationInDays /// public bool FallbackToOriginalLanguage { get; set; } = false; + /// + /// Gets or sets a value indicating whether to include original country in tags. + /// + public bool IncludeOriginalCountryInTags { get; set; } = false; + /// /// Gets or sets the metadata update in hours for the Check for Metadata Updates Scheduled Task. /// diff --git a/Jellyfin.Plugin.Tvdb/Configuration/config.html b/Jellyfin.Plugin.Tvdb/Configuration/config.html index 309668f..f293506 100644 --- a/Jellyfin.Plugin.Tvdb/Configuration/config.html +++ b/Jellyfin.Plugin.Tvdb/Configuration/config.html @@ -54,6 +54,10 @@

TheTVDB Settings:

Fallback to Original Language (Last resort if other fallback languages fails) +

Check for Metadata Updates Scheduled Task Settings:

@@ -104,6 +108,7 @@

Check for Metadata Updates Scheduled Task Settings:

Check for Metadata Updates Scheduled Task Settings: public string Name => TvdbPlugin.ProviderName; + private static bool IncludeOriginalCountryInTags => TvdbPlugin.Instance?.Configuration.IncludeOriginalCountryInTags ?? false; + /// public async Task> GetSearchResults(MovieInfo searchInfo, CancellationToken cancellationToken) { @@ -374,6 +376,16 @@ await _tvdbClientManager { _logger.LogError("Failed to retrieve actors for movie {TvdbId}:{MovieName}", tvdbId, movieInfo.Name); } + + if (IncludeOriginalCountryInTags && !string.IsNullOrWhiteSpace(movieResult.OriginalCountry)) + { + var countries = await _tvdbClientManager.GetCountriesAsync(cancellationToken).ConfigureAwait(false); + var country = countries.FirstOrDefault(x => string.Equals(x.Id, movieResult.OriginalCountry, StringComparison.OrdinalIgnoreCase))?.Name; + if (!string.IsNullOrWhiteSpace(country)) + { + movieMetadata.AddTag(country); + } + } } catch (Exception e) { diff --git a/Jellyfin.Plugin.Tvdb/Providers/TvdbSeriesProvider.cs b/Jellyfin.Plugin.Tvdb/Providers/TvdbSeriesProvider.cs index 67e2f93..5ecbc07 100644 --- a/Jellyfin.Plugin.Tvdb/Providers/TvdbSeriesProvider.cs +++ b/Jellyfin.Plugin.Tvdb/Providers/TvdbSeriesProvider.cs @@ -49,6 +49,8 @@ public TvdbSeriesProvider(IHttpClientFactory httpClientFactory, ILogger public string Name => TvdbPlugin.ProviderName; + private static bool IncludeOriginalCountryInTags => TvdbPlugin.Instance?.Configuration.IncludeOriginalCountryInTags ?? false; + /// public async Task> GetSearchResults(SeriesInfo searchInfo, CancellationToken cancellationToken) { @@ -239,6 +241,16 @@ await _tvdbClientManager { _logger.LogError("Failed to retrieve actors for series {TvdbId}:{SeriesName}", tvdbId, seriesInfo.Name); } + + if (IncludeOriginalCountryInTags && !string.IsNullOrWhiteSpace(seriesResult.OriginalCountry)) + { + var countries = await _tvdbClientManager.GetCountriesAsync(cancellationToken).ConfigureAwait(false); + var country = countries.FirstOrDefault(x => string.Equals(x.Id, seriesResult.OriginalCountry, StringComparison.OrdinalIgnoreCase))?.Name; + if (!string.IsNullOrWhiteSpace(country)) + { + seriesMetadata.AddTag(country); + } + } } catch (Exception e) { diff --git a/Jellyfin.Plugin.Tvdb/TvdbClientManager.cs b/Jellyfin.Plugin.Tvdb/TvdbClientManager.cs index d2d3369..ba649ab 100644 --- a/Jellyfin.Plugin.Tvdb/TvdbClientManager.cs +++ b/Jellyfin.Plugin.Tvdb/TvdbClientManager.cs @@ -544,6 +544,28 @@ public async Task> GetArtworkTypeAsync(CancellationTo return artworkTypesResult.Data; } + /// + /// Gets all tvdb Countries. + /// + /// Cancellation Token. + /// All tvdb countries. + public async Task> GetCountriesAsync(CancellationToken cancellationToken) + { + var key = "TvdbCountries"; + if (_memoryCache.TryGetValue(key, out IReadOnlyList? countries) + && countries is not null) + { + return countries; + } + + var countriesClient = _serviceProvider.GetRequiredService(); + await LoginAsync().ConfigureAwait(false); + var countriesResult = await countriesClient.GetAllCountriesAsync(cancellationToken: cancellationToken) + .ConfigureAwait(false); + _memoryCache.Set(key, countriesResult.Data, TimeSpan.FromDays(CacheDurationInDays)); + return countriesResult.Data; + } + /// /// Get an episode's tvdb id. /// @@ -744,6 +766,7 @@ private ServiceProvider ConfigureService(IApplicationHost applicationHost) services.AddTransient(_ => new ArtworkClient(_sdkClientSettings, _httpClientFactory.CreateClient(TvdbHttpClient))); services.AddTransient(_ => new Artwork_TypesClient(_sdkClientSettings, _httpClientFactory.CreateClient(TvdbHttpClient))); services.AddTransient(_ => new LanguagesClient(_sdkClientSettings, _httpClientFactory.CreateClient(TvdbHttpClient))); + services.AddTransient(_ => new CountriesClient(_sdkClientSettings, _httpClientFactory.CreateClient(TvdbHttpClient))); services.AddTransient(_ => new UpdatesClient(_sdkClientSettings, _httpClientFactory.CreateClient(TvdbHttpClient))); services.AddTransient(_ => new MoviesClient(_sdkClientSettings, _httpClientFactory.CreateClient(TvdbHttpClient)));