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

64 lines
2.5 KiB
C#
Raw Normal View History

2014-02-23 03:19:05 +00:00
using System.Collections.Generic;
using System.Linq;
using NLog;
using NzbDrone.Core.Datastore;
using NzbDrone.Core.Qualities;
2015-07-12 16:44:33 +00:00
using NzbDrone.Core.Languages;
using NzbDrone.Core.Profiles.Qualities;
using NzbDrone.Core.Profiles.Languages;
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;
2015-07-12 16:44:33 +00:00
private readonly ILanguageProfileService _languageProfileService;
2014-02-23 03:19:05 +00:00
public EpisodeCutoffService(IEpisodeRepository episodeRepository, IQualityProfileService qualityProfileService, ILanguageProfileService languageProfileService, Logger logger)
2014-02-23 03:19:05 +00:00
{
_episodeRepository = episodeRepository;
_qualityProfileService = qualityProfileService;
2015-07-12 16:44:33 +00:00
_languageProfileService = languageProfileService;
2014-02-23 03:19:05 +00:00
}
public PagingSpec<Episode> EpisodesWhereCutoffUnmet(PagingSpec<Episode> pagingSpec)
{
var qualitiesBelowCutoff = new List<QualitiesBelowCutoff>();
2015-07-12 16:44:33 +00:00
var languagesBelowCutoff = new List<LanguagesBelowCutoff>();
var profiles = _qualityProfileService.All();
2015-07-12 16:44:33 +00:00
var languageProfiles = _languageProfileService.All();
2014-02-23 03:19:05 +00:00
//Get all items less than the cutoff
foreach (var profile in profiles)
2014-02-23 03:19:05 +00:00
{
2017-06-18 17:02:20 +00:00
var cutoffIndex = profile.GetIndex(profile.Cutoff);
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
}
}
2015-07-12 16:44:33 +00:00
foreach (var profile in languageProfiles)
{
var languageCutoffIndex = profile.Languages.FindIndex(v => v.Language == profile.Cutoff);
var belowLanguageCutoff = profile.Languages.Take(languageCutoffIndex).ToList();
if (belowLanguageCutoff.Any())
{
languagesBelowCutoff.Add(new LanguagesBelowCutoff(profile.Id, belowLanguageCutoff.Select(l => l.Language.Id)));
}
}
return _episodeRepository.EpisodesWhereCutoffUnmet(pagingSpec, qualitiesBelowCutoff, languagesBelowCutoff, false);
2014-02-23 03:19:05 +00:00
}
}
}