You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
415 lines
18 KiB
415 lines
18 KiB
6 years ago
|
using System;
|
||
11 years ago
|
using System.Collections.Generic;
|
||
10 years ago
|
using System.IO;
|
||
11 years ago
|
using System.Linq;
|
||
|
using NLog;
|
||
11 years ago
|
using NzbDrone.Common.Disk;
|
||
10 years ago
|
using NzbDrone.Common.Extensions;
|
||
11 years ago
|
using NzbDrone.Core.DecisionEngine;
|
||
7 years ago
|
using NzbDrone.Core.Download;
|
||
11 years ago
|
using NzbDrone.Core.Parser;
|
||
|
using NzbDrone.Core.Parser.Model;
|
||
11 years ago
|
using NzbDrone.Core.Qualities;
|
||
6 years ago
|
using NzbDrone.Core.Movies;
|
||
10 years ago
|
using NzbDrone.Core.MediaFiles.MediaInfo;
|
||
11 years ago
|
|
||
|
|
||
6 years ago
|
namespace NzbDrone.Core.MediaFiles.MovieImport
|
||
11 years ago
|
{
|
||
|
public interface IMakeImportDecision
|
||
|
{
|
||
8 years ago
|
List<ImportDecision> GetImportDecisions(List<string> videoFiles, Movie movie);
|
||
8 years ago
|
List<ImportDecision> GetImportDecisions(List<string> videoFiles, Movie movie, bool shouldCheckQuality);
|
||
7 years ago
|
List<ImportDecision> GetImportDecisions(List<string> videoFiles, Movie movie, DownloadClientItem downloadClientItem, ParsedMovieInfo folderInfo, bool sceneSource, bool shouldCheckQuality);
|
||
|
List<ImportDecision> GetImportDecisions(List<string> videoFiles, Movie movie, DownloadClientItem downloadClientItem, ParsedMovieInfo folderInfo, bool sceneSource);
|
||
11 years ago
|
}
|
||
|
|
||
|
public class ImportDecisionMaker : IMakeImportDecision
|
||
|
{
|
||
10 years ago
|
private readonly IEnumerable<IImportDecisionEngineSpecification> _specifications;
|
||
11 years ago
|
private readonly IParsingService _parsingService;
|
||
11 years ago
|
private readonly IMediaFileService _mediaFileService;
|
||
11 years ago
|
private readonly IDiskProvider _diskProvider;
|
||
10 years ago
|
private readonly IVideoFileInfoReader _videoFileInfoReader;
|
||
10 years ago
|
private readonly IDetectSample _detectSample;
|
||
8 years ago
|
private readonly IQualityDefinitionService _qualitiesService;
|
||
11 years ago
|
private readonly Logger _logger;
|
||
|
|
||
10 years ago
|
public ImportDecisionMaker(IEnumerable<IImportDecisionEngineSpecification> specifications,
|
||
11 years ago
|
IParsingService parsingService,
|
||
11 years ago
|
IMediaFileService mediaFileService,
|
||
11 years ago
|
IDiskProvider diskProvider,
|
||
10 years ago
|
IVideoFileInfoReader videoFileInfoReader,
|
||
10 years ago
|
IDetectSample detectSample,
|
||
8 years ago
|
IQualityDefinitionService qualitiesService,
|
||
11 years ago
|
Logger logger)
|
||
11 years ago
|
{
|
||
|
_specifications = specifications;
|
||
|
_parsingService = parsingService;
|
||
11 years ago
|
_mediaFileService = mediaFileService;
|
||
11 years ago
|
_diskProvider = diskProvider;
|
||
10 years ago
|
_videoFileInfoReader = videoFileInfoReader;
|
||
10 years ago
|
_detectSample = detectSample;
|
||
8 years ago
|
_qualitiesService = qualitiesService;
|
||
11 years ago
|
_logger = logger;
|
||
|
}
|
||
|
|
||
8 years ago
|
public List<ImportDecision> GetImportDecisions(List<string> videoFiles, Movie movie)
|
||
|
{
|
||
7 years ago
|
return GetImportDecisions(videoFiles, movie, null, null, true, false);
|
||
8 years ago
|
}
|
||
|
|
||
|
public List<ImportDecision> GetImportDecisions(List<string> videoFiles, Movie movie, bool shouldCheckQuality = false)
|
||
|
{
|
||
7 years ago
|
return GetImportDecisions(videoFiles, movie, null, null, true, shouldCheckQuality);
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
public List<ImportDecision> GetImportDecisions(List<string> videoFiles, Movie movie, DownloadClientItem downloadClientItem, ParsedMovieInfo folderInfo, bool sceneSource)
|
||
7 years ago
|
{
|
||
|
var newFiles = _mediaFileService.FilterExistingFiles(videoFiles.ToList(), movie);
|
||
|
|
||
|
_logger.Debug("Analyzing {0}/{1} files.", newFiles.Count, videoFiles.Count());
|
||
|
|
||
|
var shouldUseFolderName = ShouldUseFolderName(videoFiles, movie, folderInfo);
|
||
|
var decisions = new List<ImportDecision>();
|
||
|
|
||
|
foreach (var file in newFiles)
|
||
|
{
|
||
7 years ago
|
decisions.AddIfNotNull(GetDecision(file, movie, downloadClientItem, folderInfo, sceneSource, shouldUseFolderName));
|
||
7 years ago
|
}
|
||
|
|
||
|
return decisions;
|
||
|
}
|
||
|
|
||
7 years ago
|
public List<ImportDecision> GetImportDecisions(List<string> videoFiles, Movie movie, DownloadClientItem downloadClientItem, ParsedMovieInfo folderInfo, bool sceneSource, bool shouldCheckQuality)
|
||
8 years ago
|
{
|
||
|
var newFiles = _mediaFileService.FilterExistingFiles(videoFiles.ToList(), movie);
|
||
|
|
||
|
_logger.Debug("Analyzing {0}/{1} files.", newFiles.Count, videoFiles.Count());
|
||
|
|
||
|
var shouldUseFolderName = ShouldUseFolderName(videoFiles, movie, folderInfo);
|
||
|
var decisions = new List<ImportDecision>();
|
||
|
|
||
|
foreach (var file in newFiles)
|
||
|
{
|
||
7 years ago
|
decisions.AddIfNotNull(GetDecision(file, movie, downloadClientItem, folderInfo, sceneSource, shouldUseFolderName, shouldCheckQuality));
|
||
8 years ago
|
}
|
||
|
|
||
|
return decisions;
|
||
|
}
|
||
|
|
||
7 years ago
|
private ImportDecision GetDecision(string file, Movie movie, DownloadClientItem downloadClientItem, ParsedMovieInfo folderInfo, bool sceneSource, bool shouldUseFolderName, bool shouldCheckQuality = false)
|
||
8 years ago
|
{
|
||
|
ImportDecision decision = null;
|
||
|
|
||
8 years ago
|
try
|
||
8 years ago
|
{
|
||
8 years ago
|
var localMovie = _parsingService.GetLocalMovie(file, movie, shouldUseFolderName ? folderInfo : null, sceneSource);
|
||
8 years ago
|
|
||
8 years ago
|
if (localMovie != null)
|
||
8 years ago
|
{
|
||
8 years ago
|
localMovie.Quality = GetQuality(folderInfo, localMovie.Quality, movie);
|
||
|
localMovie.Size = _diskProvider.GetFileSize(file);
|
||
8 years ago
|
|
||
8 years ago
|
_logger.Debug("Size: {0}", localMovie.Size);
|
||
7 years ago
|
var current = localMovie.Quality;
|
||
7 years ago
|
localMovie.MediaInfo = _videoFileInfoReader.GetMediaInfo(file);
|
||
6 years ago
|
//TODO: make it so media info doesn't ruin the import process of a new movie
|
||
7 years ago
|
if (sceneSource && ShouldCheckQualityForParsedQuality(current.Quality))
|
||
8 years ago
|
{
|
||
7 years ago
|
|
||
8 years ago
|
if (shouldCheckQuality)
|
||
|
{
|
||
7 years ago
|
_logger.Debug("Checking quality for this video file to make sure nothing mismatched.");
|
||
8 years ago
|
var width = localMovie.MediaInfo.Width;
|
||
7 years ago
|
|
||
8 years ago
|
var qualityName = current.Quality.Name.ToLower();
|
||
|
QualityModel updated = null;
|
||
8 years ago
|
if (width > 2000)
|
||
|
{
|
||
|
if (qualityName.Contains("bluray"))
|
||
|
{
|
||
|
updated = new QualityModel(Quality.Bluray2160p);
|
||
|
}
|
||
|
|
||
|
else if (qualityName.Contains("webdl"))
|
||
|
{
|
||
|
updated = new QualityModel(Quality.WEBDL2160p);
|
||
|
}
|
||
|
|
||
|
else if (qualityName.Contains("hdtv"))
|
||
|
{
|
||
|
updated = new QualityModel(Quality.HDTV2160p);
|
||
|
}
|
||
|
|
||
|
else
|
||
|
{
|
||
|
var def = _qualitiesService.Get(Quality.HDTV2160p);
|
||
|
if (localMovie.Size > def.MinSize && def.MaxSize > localMovie.Size)
|
||
|
{
|
||
|
updated = new QualityModel(Quality.HDTV2160p);
|
||
|
}
|
||
|
def = _qualitiesService.Get(Quality.WEBDL2160p);
|
||
|
if (localMovie.Size > def.MinSize && def.MaxSize > localMovie.Size)
|
||
|
{
|
||
|
updated = new QualityModel(Quality.WEBDL2160p);
|
||
|
}
|
||
|
def = _qualitiesService.Get(Quality.Bluray2160p);
|
||
|
if (localMovie.Size > def.MinSize && def.MaxSize > localMovie.Size)
|
||
|
{
|
||
|
updated = new QualityModel(Quality.Bluray2160p);
|
||
|
}
|
||
|
if (updated == null)
|
||
|
{
|
||
|
updated = new QualityModel(Quality.Bluray2160p);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
else if (width > 1400)
|
||
8 years ago
|
{
|
||
|
if (qualityName.Contains("bluray"))
|
||
|
{
|
||
|
updated = new QualityModel(Quality.Bluray1080p);
|
||
|
}
|
||
|
|
||
|
else if (qualityName.Contains("webdl"))
|
||
|
{
|
||
|
updated = new QualityModel(Quality.WEBDL1080p);
|
||
|
}
|
||
|
|
||
|
else if (qualityName.Contains("hdtv"))
|
||
|
{
|
||
|
updated = new QualityModel(Quality.HDTV1080p);
|
||
|
}
|
||
|
|
||
|
else
|
||
|
{
|
||
|
var def = _qualitiesService.Get(Quality.HDTV1080p);
|
||
|
if (localMovie.Size > def.MinSize && def.MaxSize > localMovie.Size)
|
||
|
{
|
||
|
updated = new QualityModel(Quality.HDTV1080p);
|
||
|
}
|
||
|
def = _qualitiesService.Get(Quality.WEBDL1080p);
|
||
|
if (localMovie.Size > def.MinSize && def.MaxSize > localMovie.Size)
|
||
|
{
|
||
|
updated = new QualityModel(Quality.WEBDL1080p);
|
||
|
}
|
||
|
def = _qualitiesService.Get(Quality.Bluray1080p);
|
||
|
if (localMovie.Size > def.MinSize && def.MaxSize > localMovie.Size)
|
||
|
{
|
||
|
updated = new QualityModel(Quality.Bluray1080p);
|
||
|
}
|
||
|
if (updated == null)
|
||
|
{
|
||
|
updated = new QualityModel(Quality.Bluray1080p);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
else
|
||
|
if (width > 900)
|
||
|
{
|
||
|
if (qualityName.Contains("bluray"))
|
||
|
{
|
||
|
updated = new QualityModel(Quality.Bluray720p);
|
||
|
}
|
||
|
|
||
|
else if (qualityName.Contains("webdl"))
|
||
|
{
|
||
|
updated = new QualityModel(Quality.WEBDL720p);
|
||
|
}
|
||
|
|
||
|
else if (qualityName.Contains("hdtv"))
|
||
|
{
|
||
|
updated = new QualityModel(Quality.HDTV720p);
|
||
|
}
|
||
|
|
||
|
else
|
||
|
{
|
||
|
|
||
|
var def = _qualitiesService.Get(Quality.HDTV720p);
|
||
|
if (localMovie.Size > def.MinSize && def.MaxSize > localMovie.Size)
|
||
|
{
|
||
|
updated = new QualityModel(Quality.HDTV720p);
|
||
|
}
|
||
|
def = _qualitiesService.Get(Quality.WEBDL720p);
|
||
|
if (localMovie.Size > def.MinSize && def.MaxSize > localMovie.Size)
|
||
|
{
|
||
|
updated = new QualityModel(Quality.WEBDL720p);
|
||
|
}
|
||
|
def = _qualitiesService.Get(Quality.Bluray720p);
|
||
|
if (localMovie.Size > def.MinSize && def.MaxSize > localMovie.Size)
|
||
|
{
|
||
|
updated = new QualityModel(Quality.Bluray720p);
|
||
|
}
|
||
|
if (updated == null)
|
||
|
{
|
||
|
updated = new QualityModel(Quality.Bluray720p);
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|
||
|
if (updated != null && updated != current)
|
||
|
{
|
||
7 years ago
|
_logger.Debug("Quality ({0}) of the file is different than the one we have ({1})", updated, current);
|
||
8 years ago
|
updated.QualitySource = QualitySource.MediaInfo;
|
||
|
localMovie.Quality = updated;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
7 years ago
|
decision = GetDecision(localMovie, downloadClientItem);
|
||
8 years ago
|
}
|
||
|
else
|
||
|
{
|
||
7 years ago
|
decision = GetDecision(localMovie, downloadClientItem);
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
|
else
|
||
|
{
|
||
8 years ago
|
localMovie = new LocalMovie();
|
||
|
localMovie.Path = file;
|
||
8 years ago
|
|
||
8 years ago
|
decision = new ImportDecision(localMovie, new Rejection("Unable to parse file"));
|
||
8 years ago
|
}
|
||
|
}
|
||
|
catch (Exception e)
|
||
|
{
|
||
|
_logger.Error(e, "Couldn't import file. " + file);
|
||
|
|
||
8 years ago
|
var localMovie = new LocalMovie { Path = file };
|
||
|
decision = new ImportDecision(localMovie, new Rejection("Unexpected error processing file"));
|
||
8 years ago
|
}
|
||
|
|
||
|
//LocalMovie nullMovie = null;
|
||
8 years ago
|
|
||
8 years ago
|
//decision = new ImportDecision(nullMovie, new Rejection("IMPLEMENTATION MISSING!!!"));
|
||
8 years ago
|
|
||
|
return decision;
|
||
|
}
|
||
|
|
||
7 years ago
|
private ImportDecision GetDecision(LocalMovie localMovie, DownloadClientItem downloadClientItem)
|
||
8 years ago
|
{
|
||
7 years ago
|
var reasons = _specifications.Select(c => EvaluateSpec(c, localMovie, downloadClientItem))
|
||
8 years ago
|
.Where(c => c != null);
|
||
|
|
||
|
return new ImportDecision(localMovie, reasons.ToArray());
|
||
|
}
|
||
|
|
||
7 years ago
|
private Rejection EvaluateSpec(IImportDecisionEngineSpecification spec, LocalMovie localMovie, DownloadClientItem downloadClientItem)
|
||
8 years ago
|
{
|
||
|
try
|
||
|
{
|
||
7 years ago
|
var result = spec.IsSatisfiedBy(localMovie, downloadClientItem);
|
||
8 years ago
|
|
||
|
if (!result.Accepted)
|
||
|
{
|
||
|
return new Rejection(result.Reason);
|
||
|
}
|
||
|
}
|
||
|
catch (NotImplementedException e)
|
||
|
{
|
||
|
_logger.Warn(e, "Spec " + spec.ToString() + " currently does not implement evaluation for movies.");
|
||
|
return null;
|
||
|
}
|
||
|
catch (Exception e)
|
||
|
{
|
||
|
//e.Data.Add("report", remoteEpisode.Report.ToJson());
|
||
|
//e.Data.Add("parsed", remoteEpisode.ParsedEpisodeInfo.ToJson());
|
||
|
_logger.Error(e, "Couldn't evaluate decision on " + localMovie.Path);
|
||
|
return new Rejection(string.Format("{0}: {1}", spec.GetType().Name, e.Message));
|
||
|
}
|
||
|
|
||
|
return null;
|
||
|
}
|
||
|
|
||
8 years ago
|
private bool ShouldUseFolderName(List<string> videoFiles, Movie movie, ParsedMovieInfo folderInfo)
|
||
8 years ago
|
{
|
||
|
if (folderInfo == null)
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
|
||
8 years ago
|
//if (folderInfo.FullSeason)
|
||
|
//{
|
||
|
// return false;
|
||
|
//}
|
||
8 years ago
|
|
||
|
return videoFiles.Count(file =>
|
||
|
{
|
||
|
var size = _diskProvider.GetFileSize(file);
|
||
|
var fileQuality = QualityParser.ParseQuality(file);
|
||
|
//var sample = null;//_detectSample.IsSample(movie, GetQuality(folderInfo, fileQuality, movie), file, size, folderInfo.IsPossibleSpecialEpisode); //Todo to this
|
||
|
|
||
|
return true;
|
||
|
|
||
|
//if (sample)
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
if (SceneChecker.IsSceneTitle(Path.GetFileName(file)))
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
return true;
|
||
|
}) == 1;
|
||
|
}
|
||
|
|
||
8 years ago
|
private QualityModel GetQuality(ParsedMovieInfo folderInfo, QualityModel fileQuality, Movie movie)
|
||
8 years ago
|
{
|
||
|
if (UseFolderQuality(folderInfo, fileQuality, movie))
|
||
|
{
|
||
|
_logger.Debug("Using quality from folder: {0}", folderInfo.Quality);
|
||
|
return folderInfo.Quality;
|
||
|
}
|
||
|
|
||
|
return fileQuality;
|
||
|
}
|
||
|
|
||
8 years ago
|
private bool UseFolderQuality(ParsedMovieInfo folderInfo, QualityModel fileQuality, Movie movie)
|
||
8 years ago
|
{
|
||
|
if (folderInfo == null)
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
if (folderInfo.Quality.Quality == Quality.Unknown)
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
if (fileQuality.QualitySource == QualitySource.Extension)
|
||
|
{
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
if (new QualityModelComparer(movie.Profile).Compare(folderInfo.Quality, fileQuality) > 0)
|
||
|
{
|
||
|
return true;
|
||
|
}
|
||
|
|
||
9 years ago
|
return false;
|
||
|
}
|
||
7 years ago
|
|
||
|
private bool ShouldCheckQualityForParsedQuality(Quality quality)
|
||
|
{
|
||
|
List<Quality> shouldNotCheck = new List<Quality> { Quality.WORKPRINT, Quality.TELECINE, Quality.TELESYNC,
|
||
|
Quality.DVDSCR, Quality.DVD, Quality.CAM, Quality.DVDR, Quality.Remux1080p, Quality.Remux2160p, Quality.REGIONAL
|
||
|
};
|
||
|
|
||
|
if (shouldNotCheck.Contains(quality))
|
||
|
{
|
||
|
return false;
|
||
|
|
||
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|
||
11 years ago
|
}
|
||
|
}
|