2011-05-20 03:47:07 +00:00
|
|
|
|
using System;
|
2011-05-24 04:12:54 +00:00
|
|
|
|
using System.Collections.Generic;
|
2011-06-22 01:12:20 +00:00
|
|
|
|
using System.Linq;
|
2011-06-14 01:23:04 +00:00
|
|
|
|
using Ninject;
|
2011-05-20 03:47:07 +00:00
|
|
|
|
using NLog;
|
|
|
|
|
using NzbDrone.Core.Model;
|
|
|
|
|
using NzbDrone.Core.Repository;
|
2011-05-28 19:23:35 +00:00
|
|
|
|
using NzbDrone.Core.Repository.Quality;
|
2011-05-20 03:47:07 +00:00
|
|
|
|
|
2011-05-26 04:25:59 +00:00
|
|
|
|
namespace NzbDrone.Core.Providers
|
2011-05-20 03:47:07 +00:00
|
|
|
|
{
|
2011-05-20 04:59:35 +00:00
|
|
|
|
public class InventoryProvider
|
2011-05-20 03:47:07 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly SeriesProvider _seriesProvider;
|
|
|
|
|
private readonly EpisodeProvider _episodeProvider;
|
|
|
|
|
private readonly HistoryProvider _historyProvider;
|
2011-09-14 04:37:22 +00:00
|
|
|
|
private readonly QualityTypeProvider _qualityTypeProvider;
|
2012-01-18 07:45:21 +00:00
|
|
|
|
private readonly QualityProvider _qualityProvider;
|
2011-05-20 03:47:07 +00:00
|
|
|
|
|
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
2011-06-14 01:23:04 +00:00
|
|
|
|
[Inject]
|
2011-09-14 04:37:22 +00:00
|
|
|
|
public InventoryProvider(SeriesProvider seriesProvider, EpisodeProvider episodeProvider,
|
2012-01-18 07:45:21 +00:00
|
|
|
|
HistoryProvider historyProvider, QualityTypeProvider qualityTypeProvider,
|
|
|
|
|
QualityProvider qualityProvider)
|
2011-05-20 03:47:07 +00:00
|
|
|
|
{
|
|
|
|
|
_seriesProvider = seriesProvider;
|
|
|
|
|
_episodeProvider = episodeProvider;
|
|
|
|
|
_historyProvider = historyProvider;
|
2011-09-14 04:37:22 +00:00
|
|
|
|
_qualityTypeProvider = qualityTypeProvider;
|
2012-01-18 07:45:21 +00:00
|
|
|
|
_qualityProvider = qualityProvider;
|
2011-05-20 03:47:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-05-27 03:07:32 +00:00
|
|
|
|
public InventoryProvider()
|
|
|
|
|
{
|
2011-05-28 19:23:35 +00:00
|
|
|
|
|
2011-05-27 03:07:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-05-28 19:23:35 +00:00
|
|
|
|
|
|
|
|
|
public virtual bool IsMonitored(EpisodeParseResult parseResult)
|
2011-05-20 03:47:07 +00:00
|
|
|
|
{
|
2011-05-20 04:59:35 +00:00
|
|
|
|
var series = _seriesProvider.FindSeries(parseResult.CleanTitle);
|
|
|
|
|
|
|
|
|
|
if (series == null)
|
|
|
|
|
{
|
|
|
|
|
Logger.Trace("{0} is not mapped to any series in DB. skipping", parseResult.CleanTitle);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-20 05:52:05 +00:00
|
|
|
|
parseResult.Series = series;
|
|
|
|
|
|
2011-05-24 04:12:54 +00:00
|
|
|
|
if (!series.Monitored)
|
2011-05-20 03:47:07 +00:00
|
|
|
|
{
|
2011-05-24 04:12:54 +00:00
|
|
|
|
Logger.Debug("{0} is present in the DB but not tracked. skipping.", parseResult.CleanTitle);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2011-05-20 03:47:07 +00:00
|
|
|
|
|
2011-06-22 01:12:20 +00:00
|
|
|
|
var episodes = _episodeProvider.GetEpisodesByParseResult(parseResult, true);
|
2011-05-20 03:47:07 +00:00
|
|
|
|
|
2011-06-22 01:12:20 +00:00
|
|
|
|
//return monitored if any of the episodes are monitored
|
|
|
|
|
if (episodes.Any(episode => !episode.Ignored))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
2011-05-24 04:12:54 +00:00
|
|
|
|
}
|
2011-05-20 03:47:07 +00:00
|
|
|
|
|
2011-06-22 01:12:20 +00:00
|
|
|
|
Logger.Debug("All episodes are ignored. skipping.");
|
|
|
|
|
return false;
|
2011-05-28 19:23:35 +00:00
|
|
|
|
}
|
2011-05-24 04:12:54 +00:00
|
|
|
|
|
2011-05-28 19:23:35 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Comprehensive check on whether or not this episode is needed.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name = "parsedReport">Episode that needs to be checked</param>
|
2012-01-18 07:46:17 +00:00
|
|
|
|
/// <param name="skipHistory">False unless called by a manual search job</param>
|
2011-05-28 19:23:35 +00:00
|
|
|
|
/// <returns>Whether or not the file quality meets the requirements </returns>
|
|
|
|
|
/// <remarks>for multi episode files, all episodes need to meet the requirement
|
|
|
|
|
/// before the report is downloaded</remarks>
|
2012-01-18 07:46:17 +00:00
|
|
|
|
public virtual bool IsQualityNeeded(EpisodeParseResult parsedReport, bool skipHistory = false)
|
2011-05-28 19:23:35 +00:00
|
|
|
|
{
|
|
|
|
|
Logger.Trace("Checking if report meets quality requirements. {0}", parsedReport.Quality);
|
|
|
|
|
if (!parsedReport.Series.QualityProfile.Allowed.Contains(parsedReport.Quality.QualityType))
|
|
|
|
|
{
|
|
|
|
|
Logger.Trace("Quality {0} rejected by Series' quality profile", parsedReport.Quality);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2011-05-24 04:12:54 +00:00
|
|
|
|
|
2011-05-28 19:23:35 +00:00
|
|
|
|
var cutoff = parsedReport.Series.QualityProfile.Cutoff;
|
2011-05-24 04:12:54 +00:00
|
|
|
|
|
2011-09-14 04:37:22 +00:00
|
|
|
|
if (!IsAcceptableSize(parsedReport))
|
2012-01-22 21:16:25 +00:00
|
|
|
|
{
|
2012-01-22 22:45:37 +00:00
|
|
|
|
Logger.Trace("Size: {0} is not acceptable for Quality: {1}", parsedReport.Size, parsedReport.Quality);
|
2011-09-14 04:37:22 +00:00
|
|
|
|
return false;
|
2012-01-22 21:16:25 +00:00
|
|
|
|
}
|
2011-09-14 04:37:22 +00:00
|
|
|
|
|
2011-06-22 01:12:20 +00:00
|
|
|
|
foreach (var episode in _episodeProvider.GetEpisodesByParseResult(parsedReport, true))
|
2011-05-28 19:23:35 +00:00
|
|
|
|
{
|
|
|
|
|
//Checking File
|
|
|
|
|
var file = episode.EpisodeFile;
|
|
|
|
|
if (file != null)
|
2011-05-20 03:47:07 +00:00
|
|
|
|
{
|
2011-05-28 19:23:35 +00:00
|
|
|
|
Logger.Trace("Comparing file quality with report. Existing file is {0} proper:{1}", file.Quality, file.Proper);
|
|
|
|
|
if (!IsUpgrade(new Quality { QualityType = file.Quality, Proper = file.Proper }, parsedReport.Quality, cutoff))
|
|
|
|
|
return false;
|
2011-05-20 03:47:07 +00:00
|
|
|
|
}
|
2011-05-20 04:59:35 +00:00
|
|
|
|
|
2012-01-18 05:06:37 +00:00
|
|
|
|
//Checking History (If not a manual search)
|
2012-01-18 07:46:17 +00:00
|
|
|
|
if (!skipHistory)
|
2011-05-20 03:47:07 +00:00
|
|
|
|
{
|
2012-01-18 05:06:37 +00:00
|
|
|
|
var bestQualityInHistory = _historyProvider.GetBestQualityInHistory(episode.EpisodeId);
|
|
|
|
|
if(bestQualityInHistory != null)
|
|
|
|
|
{
|
|
|
|
|
Logger.Trace("Comparing history quality with report. History is {0}", bestQualityInHistory);
|
|
|
|
|
if(!IsUpgrade(bestQualityInHistory, parsedReport.Quality, cutoff))
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2011-05-20 03:47:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-05-28 19:23:35 +00:00
|
|
|
|
}
|
2011-05-24 04:12:54 +00:00
|
|
|
|
|
2011-05-28 19:23:35 +00:00
|
|
|
|
Logger.Debug("Episode {0} is needed", parsedReport);
|
|
|
|
|
return true; //If we get to this point and the file has not yet been rejected then accept it
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool IsUpgrade(Quality currentQuality, Quality newQuality, QualityTypes cutOff)
|
|
|
|
|
{
|
|
|
|
|
if (currentQuality.QualityType >= cutOff)
|
|
|
|
|
{
|
2011-07-03 23:04:57 +00:00
|
|
|
|
Logger.Trace("Existing item meets cut-off. skipping.");
|
2011-05-28 19:23:35 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (newQuality > currentQuality)
|
2011-05-20 03:47:07 +00:00
|
|
|
|
return true;
|
2011-05-28 19:23:35 +00:00
|
|
|
|
|
|
|
|
|
if (currentQuality > newQuality)
|
|
|
|
|
{
|
|
|
|
|
Logger.Trace("existing item has better quality. skipping");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (currentQuality == newQuality && !newQuality.Proper)
|
|
|
|
|
{
|
|
|
|
|
Logger.Trace("same quality. not proper skipping");
|
|
|
|
|
return false;
|
2011-05-20 03:47:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-05-28 19:23:35 +00:00
|
|
|
|
Logger.Debug("New item has better quality than existing item");
|
|
|
|
|
return true;
|
2011-05-20 03:47:07 +00:00
|
|
|
|
}
|
2011-09-14 04:37:22 +00:00
|
|
|
|
|
|
|
|
|
public virtual bool IsAcceptableSize(EpisodeParseResult parseResult)
|
|
|
|
|
{
|
|
|
|
|
var qualityType = _qualityTypeProvider.Get((int) parseResult.Quality.QualityType);
|
|
|
|
|
|
|
|
|
|
//Need to determine if this is a 30 or 60 minute episode
|
|
|
|
|
//Is it a multi-episode release?
|
|
|
|
|
//Is it the first or last series of a season?
|
|
|
|
|
|
2011-09-16 04:42:30 +00:00
|
|
|
|
//0 will be treated as unlimited
|
|
|
|
|
if (qualityType.MaxSize == 0)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
var maxSize = qualityType.MaxSize.Megabytes();
|
2011-09-14 04:37:22 +00:00
|
|
|
|
var series = parseResult.Series;
|
|
|
|
|
|
2011-09-16 04:42:30 +00:00
|
|
|
|
//Multiply maxSize by Series.Runtime
|
|
|
|
|
maxSize = maxSize * series.Runtime;
|
2011-09-14 04:37:22 +00:00
|
|
|
|
|
2011-12-01 05:08:36 +00:00
|
|
|
|
//Multiply maxSize by the number of episodes parsed (if EpisodeNumbers is null it will be treated as a single episode)
|
|
|
|
|
if (parseResult.EpisodeNumbers != null)
|
|
|
|
|
maxSize = maxSize * parseResult.EpisodeNumbers.Count;
|
2011-09-14 04:37:22 +00:00
|
|
|
|
|
|
|
|
|
//Check if there was only one episode parsed
|
|
|
|
|
//and it is the first or last episode of the season
|
2011-12-01 05:08:36 +00:00
|
|
|
|
if (parseResult.EpisodeNumbers != null && parseResult.EpisodeNumbers.Count == 1 &&
|
2011-09-14 04:37:22 +00:00
|
|
|
|
_episodeProvider.IsFirstOrLastEpisodeOfSeason(series.SeriesId,
|
|
|
|
|
parseResult.SeasonNumber, parseResult.EpisodeNumbers[0]))
|
|
|
|
|
{
|
|
|
|
|
maxSize = maxSize * 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//If the parsed size is greater than maxSize we don't want it
|
|
|
|
|
if (parseResult.Size > maxSize)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2012-01-18 07:45:21 +00:00
|
|
|
|
|
|
|
|
|
public virtual bool IsUpgradePossible(Episode episode)
|
|
|
|
|
{
|
|
|
|
|
//Used to check if the existing episode can be upgraded by searching (Before we search)
|
|
|
|
|
|
|
|
|
|
if (episode.EpisodeFileId == 0)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
var profile = _qualityProvider.Get(episode.Series.QualityProfileId);
|
|
|
|
|
|
|
|
|
|
if (episode.EpisodeFile.Quality >= profile.Cutoff)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2011-05-20 03:47:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|