From f47458aaf9cc9af2ab12a1c8a57962a59550411e Mon Sep 17 00:00:00 2001 From: JQ <81431263+scampower3@users.noreply.github.com> Date: Mon, 15 Jul 2024 22:09:47 +0800 Subject: [PATCH] Add Tvdb Collection Ids (#162) --- .../ExternalId/TvdbCollectionsExternalId.cs | 30 +++++++++++++++++++ .../Providers/TvdbSeriesProvider.cs | 22 +++++++++++++- Jellyfin.Plugin.Tvdb/TvdbPlugin.cs | 9 ++++-- 3 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 Jellyfin.Plugin.Tvdb/Providers/ExternalId/TvdbCollectionsExternalId.cs diff --git a/Jellyfin.Plugin.Tvdb/Providers/ExternalId/TvdbCollectionsExternalId.cs b/Jellyfin.Plugin.Tvdb/Providers/ExternalId/TvdbCollectionsExternalId.cs new file mode 100644 index 0000000..702b834 --- /dev/null +++ b/Jellyfin.Plugin.Tvdb/Providers/ExternalId/TvdbCollectionsExternalId.cs @@ -0,0 +1,30 @@ +using MediaBrowser.Controller.Entities.Movies; +using MediaBrowser.Controller.Entities.TV; +using MediaBrowser.Controller.Providers; +using MediaBrowser.Model.Entities; +using MediaBrowser.Model.Providers; + +namespace Jellyfin.Plugin.Tvdb.Providers.ExternalId +{ + /// + public class TvdbCollectionsExternalId : IExternalId + { + /// + public string ProviderName => TvdbPlugin.ProviderName; + + /// + public string Key => TvdbPlugin.CollectionProviderId; + + /// + public ExternalIdMediaType? Type => ExternalIdMediaType.BoxSet; + + /// + public string? UrlFormatString => null; + + /// + public bool Supports(IHasProviderIds item) + { + return item is Movie || item is Series; + } + } +} diff --git a/Jellyfin.Plugin.Tvdb/Providers/TvdbSeriesProvider.cs b/Jellyfin.Plugin.Tvdb/Providers/TvdbSeriesProvider.cs index ccb5c9e..5bb3195 100644 --- a/Jellyfin.Plugin.Tvdb/Providers/TvdbSeriesProvider.cs +++ b/Jellyfin.Plugin.Tvdb/Providers/TvdbSeriesProvider.cs @@ -440,7 +440,7 @@ private async Task Identify(SeriesInfo info) } } - private static void MapSeriesToResult(MetadataResult result, SeriesExtendedRecord tvdbSeries, SeriesInfo info) + private void MapSeriesToResult(MetadataResult result, SeriesExtendedRecord tvdbSeries, SeriesInfo info) { Series series = result.Item; series.SetTvdbId(tvdbSeries.Id); @@ -455,6 +455,26 @@ private static void MapSeriesToResult(MetadataResult result, SeriesExten // series.CommunityRating = (float?)tvdbSeries.SiteRating; // Attempts to default to USA if not found series.OfficialRating = tvdbSeries.ContentRatings.FirstOrDefault(x => string.Equals(x.Country, TvdbCultureInfo.GetCountryInfo(info.MetadataCountryCode)?.ThreeLetterISORegionName, StringComparison.OrdinalIgnoreCase))?.Name ?? tvdbSeries.ContentRatings.FirstOrDefault(x => string.Equals(x.Country, "usa", StringComparison.OrdinalIgnoreCase))?.Name; + if (tvdbSeries.Lists is not null && tvdbSeries.Lists is JsonElement jsonElement) + { + var collections = jsonElement.Deserialize>(); + if (collections is not null) + { + try + { + var collectionIds = collections.OfType() + .Where(x => x.GetProperty("isOfficial").GetBoolean()) + .Select(x => x.GetProperty("id").GetInt32().ToString(CultureInfo.InvariantCulture)) + .Aggregate(new StringBuilder(), (sb, id) => sb.Append(id).Append(';')); + + series.SetProviderIdIfHasValue(TvdbPlugin.CollectionProviderId, collectionIds.ToString()); + } + catch (Exception e) + { + _logger.LogError(e, "Failed to retrieve collection for series {TvdbId}:{SeriesName}", tvdbSeries.Id, tvdbSeries.Name); + } + } + } var imdbId = tvdbSeries.RemoteIds?.FirstOrDefault(x => string.Equals(x.SourceName, "IMDB", StringComparison.OrdinalIgnoreCase))?.Id.ToString(); series.SetProviderIdIfHasValue(MetadataProvider.Imdb, imdbId); diff --git a/Jellyfin.Plugin.Tvdb/TvdbPlugin.cs b/Jellyfin.Plugin.Tvdb/TvdbPlugin.cs index eaf4771..064a0ed 100644 --- a/Jellyfin.Plugin.Tvdb/TvdbPlugin.cs +++ b/Jellyfin.Plugin.Tvdb/TvdbPlugin.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using Jellyfin.Plugin.Tvdb.Configuration; using MediaBrowser.Common.Configuration; @@ -23,6 +23,11 @@ public class TvdbPlugin : BasePlugin, IHasWebPages /// public const string ProviderId = "Tvdb"; + /// + /// Gets the collection provider id. + /// + public const string CollectionProviderId = "TvdbCollection"; + /// /// Initializes a new instance of the class. /// @@ -55,4 +60,4 @@ public IEnumerable GetPages() }; } } -} \ No newline at end of file +}