Skip to content

Commit

Permalink
Filter by runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
PCJones committed Nov 11, 2024
1 parent d30817a commit 6b3b64d
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions MediathekArr/Services/MediathekSearchService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,11 @@ public async Task<string> FetchSearchResultsFromApiById(TvdbData tvdbData, strin
}

var initialResults = responseObject.Result.Results;
var resultsByAiredDate = FilterByAiredDate(initialResults, episode.Aired!.Value).Where(item => !ShouldSkipItem(item)).ToList();
var resultsByTitleDate = FilterByTitleDate(initialResults, episode.Aired.Value).Where(item => !ShouldSkipItem(item)).ToList();
var resultsByDescriptionDate = FilterByDescriptionDate(initialResults, episode.Aired.Value).Where(item => !ShouldSkipItem(item)).ToList();
var resultsByEpisodeTitleMatch = FilterByEpisodeTitleMatch(initialResults, episode.Name).Where(item => !ShouldSkipItem(item)).ToList();
var resultsFilteredByRuntime = FilterByRuntime(initialResults, episode.Runtime);
var resultsByAiredDate = FilterByAiredDate(resultsFilteredByRuntime, episode.Aired!.Value).Where(item => !ShouldSkipItem(item)).ToList();
var resultsByTitleDate = FilterByTitleDate(resultsFilteredByRuntime, episode.Aired.Value).Where(item => !ShouldSkipItem(item)).ToList();
var resultsByDescriptionDate = FilterByDescriptionDate(resultsFilteredByRuntime, episode.Aired.Value).Where(item => !ShouldSkipItem(item)).ToList();
var resultsByEpisodeTitleMatch = FilterByEpisodeTitleMatch(resultsFilteredByRuntime, episode.Name).Where(item => !ShouldSkipItem(item)).ToList();
List<ApiResultItem> resultsBySeasonEpisodeMatch = [];
// if more than 3 results we assume episode title match wasn't correct
if (resultsByEpisodeTitleMatch.Count > 3)
Expand All @@ -113,7 +114,7 @@ public async Task<string> FetchSearchResultsFromApiById(TvdbData tvdbData, strin
{
// Only trust Mediathek season/episode if no other match:
resultsBySeasonEpisodeMatch =
FilterBySeasonEpisodeMatch(initialResults, episode.SeasonNumber.ToString(), episode.EpisodeNumber.ToString())
FilterBySeasonEpisodeMatch(resultsFilteredByRuntime, episode.SeasonNumber.ToString(), episode.EpisodeNumber.ToString())
.Where(item => !ShouldSkipItem(item)).ToList(); ;
}

Expand All @@ -135,6 +136,19 @@ public async Task<string> FetchSearchResultsFromApiById(TvdbData tvdbData, strin
}


private static List<ApiResultItem> FilterByRuntime(List<ApiResultItem> results, int? runtime)
{
if (runtime is null || runtime is 0)
{
return results;
}
var minRuntime = Math.Max(5, (int)(runtime * 0.65)) * 60;
var maxRuntime = (int)(runtime * 1.35) * 60;
return results.Where(item =>
item.Duration >= minRuntime && item.Duration <= maxRuntime)
.ToList();
}

private static List<ApiResultItem> FilterByAiredDate(List<ApiResultItem> results, DateTime airedDate)
{
return results.Where(item =>
Expand Down

0 comments on commit 6b3b64d

Please sign in to comment.