Skip to content

Commit

Permalink
Combine Title and Overview of combined episodes. (#86)
Browse files Browse the repository at this point in the history
Create new functions to combine name and overview metadata for TV episodes.

Co-authored-by: Hayden Stith <[email protected]>
  • Loading branch information
Hayden Stith and haydenstith authored Jun 19, 2023
1 parent 04818e7 commit d9b250d
Showing 1 changed file with 54 additions and 1 deletion.
55 changes: 54 additions & 1 deletion Jellyfin.Plugin.Tvdb/Providers/TvdbEpisodeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -88,7 +89,17 @@ public async Task<MetadataResult<Episode>> 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
{
Expand All @@ -98,6 +109,48 @@ public async Task<MetadataResult<Episode>> GetMetadata(EpisodeInfo info, Cancell
return result;
}

private async Task<MetadataResult<Episode>> GetCombinedEpisode(EpisodeInfo info, CancellationToken cancellationToken)
{
var startIndex = info.IndexNumber;
var endIndex = info.IndexNumberEnd;

List<MetadataResult<Episode>> results = new List<MetadataResult<Episode>>();

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<Episode> CombineResults(EpisodeInfo id, List<MetadataResult<Episode>> 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<MetadataResult<Episode>> GetEpisode(EpisodeInfo searchInfo, CancellationToken cancellationToken)
{
var result = new MetadataResult<Episode>
Expand Down

0 comments on commit d9b250d

Please sign in to comment.