From d9b250d7539c23acd6dc63b002c9070ef567603c Mon Sep 17 00:00:00 2001 From: Hayden Stith Date: Mon, 19 Jun 2023 08:59:31 -0400 Subject: [PATCH] Combine Title and Overview of combined episodes. (#86) Create new functions to combine name and overview metadata for TV episodes. Co-authored-by: Hayden Stith --- .../Providers/TvdbEpisodeProvider.cs | 55 ++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/Jellyfin.Plugin.Tvdb/Providers/TvdbEpisodeProvider.cs b/Jellyfin.Plugin.Tvdb/Providers/TvdbEpisodeProvider.cs index f190eca..0e10ad6 100644 --- a/Jellyfin.Plugin.Tvdb/Providers/TvdbEpisodeProvider.cs +++ b/Jellyfin.Plugin.Tvdb/Providers/TvdbEpisodeProvider.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Globalization; using System.Net.Http; +using System.Text; using System.Threading; using System.Threading.Tasks; using MediaBrowser.Common.Net; @@ -88,7 +89,17 @@ public async Task> GetMetadata(EpisodeInfo info, Cancell if (TvdbSeriesProvider.IsValidSeries(info.SeriesProviderIds) && (info.IndexNumber.HasValue || info.PremiereDate.HasValue)) { - result = await GetEpisode(info, cancellationToken).ConfigureAwait(false); + // Check for multiple episodes per file, if not run one query. + if (info.IndexNumberEnd.HasValue) + { + _logger.LogDebug("Multiple episodes found in {Path}", info.Path); + + result = await GetCombinedEpisode(info, cancellationToken).ConfigureAwait(false); + } + else + { + result = await GetEpisode(info, cancellationToken).ConfigureAwait(false); + } } else { @@ -98,6 +109,48 @@ public async Task> GetMetadata(EpisodeInfo info, Cancell return result; } + private async Task> GetCombinedEpisode(EpisodeInfo info, CancellationToken cancellationToken) + { + var startIndex = info.IndexNumber; + var endIndex = info.IndexNumberEnd; + + List> results = new List>(); + + for (int? episode = startIndex; episode <= endIndex; episode++) + { + var tempEpisodeInfo = info; + info.IndexNumber = episode; + + results.Add(await GetEpisode(tempEpisodeInfo, cancellationToken).ConfigureAwait(false)); + } + + var result = CombineResults(info, results); + + return result; + } + + private MetadataResult CombineResults(EpisodeInfo id, List> results) + { + // Use first result as baseline + var result = results[0]; + + var name = new StringBuilder(result.Item.Name); + var overview = new StringBuilder(result.Item.Overview); + + for (int res = 1; res < results.Count; res++) + { + name.Append(" / "); + name.Append(results[res].Item.Name); + overview.Append(" / "); + overview.Append(results[res].Item.Overview); + } + + result.Item.Name = name.ToString(); + result.Item.Overview = overview.ToString(); + + return result; + } + private async Task> GetEpisode(EpisodeInfo searchInfo, CancellationToken cancellationToken) { var result = new MetadataResult