Sonarr/src/NzbDrone.Core/Tv/EpisodeCutoffService.cs

55 lines
1.9 KiB
C#
Raw Normal View History

2022-08-14 19:07:26 +00:00
using System.Collections.Generic;
2014-02-23 03:19:05 +00:00
using System.Linq;
using NzbDrone.Common.Extensions;
2014-02-23 03:19:05 +00:00
using NzbDrone.Core.Datastore;
2021-08-03 04:43:28 +00:00
using NzbDrone.Core.Profiles.Qualities;
using NzbDrone.Core.Qualities;
2014-02-23 03:19:05 +00:00
namespace NzbDrone.Core.Tv
{
public interface IEpisodeCutoffService
{
PagingSpec<Episode> EpisodesWhereCutoffUnmet(PagingSpec<Episode> pagingSpec);
}
public class EpisodeCutoffService : IEpisodeCutoffService
{
private readonly IEpisodeRepository _episodeRepository;
private readonly IQualityProfileService _qualityProfileService;
2014-02-23 03:19:05 +00:00
2024-01-08 15:57:01 +00:00
public EpisodeCutoffService(IEpisodeRepository episodeRepository, IQualityProfileService qualityProfileService)
2014-02-23 03:19:05 +00:00
{
_episodeRepository = episodeRepository;
_qualityProfileService = qualityProfileService;
2014-02-23 03:19:05 +00:00
}
public PagingSpec<Episode> EpisodesWhereCutoffUnmet(PagingSpec<Episode> pagingSpec)
{
var qualitiesBelowCutoff = new List<QualitiesBelowCutoff>();
var profiles = _qualityProfileService.All();
2021-08-03 04:43:28 +00:00
2022-11-07 00:44:13 +00:00
// Get all items less than the cutoff
foreach (var profile in profiles)
2014-02-23 03:19:05 +00:00
{
var cutoff = profile.UpgradeAllowed ? profile.Cutoff : profile.FirststAllowedQuality().Id;
var cutoffIndex = profile.GetIndex(cutoff);
2017-06-18 17:02:20 +00:00
var belowCutoff = profile.Items.Take(cutoffIndex.Index).ToList();
2014-02-23 03:19:05 +00:00
if (belowCutoff.Any())
{
2017-06-18 17:02:20 +00:00
qualitiesBelowCutoff.Add(new QualitiesBelowCutoff(profile.Id, belowCutoff.SelectMany(i => i.GetQualities().Select(q => q.Id))));
2014-02-23 03:19:05 +00:00
}
}
if (qualitiesBelowCutoff.Empty())
{
pagingSpec.Records = new List<Episode>();
return pagingSpec;
}
2024-01-08 15:57:01 +00:00
return _episodeRepository.EpisodesWhereCutoffUnmet(pagingSpec, qualitiesBelowCutoff, false);
2014-02-23 03:19:05 +00:00
}
}
}