2013-07-06 21:47:49 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2013-07-12 08:15:23 +00:00
|
|
|
|
using System.IO;
|
2013-07-06 21:47:49 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using NLog;
|
2013-07-15 23:53:06 +00:00
|
|
|
|
using NzbDrone.Common;
|
2013-07-06 21:47:49 +00:00
|
|
|
|
using NzbDrone.Core.DecisionEngine;
|
|
|
|
|
using NzbDrone.Core.Parser;
|
|
|
|
|
using NzbDrone.Core.Parser.Model;
|
|
|
|
|
using NzbDrone.Core.Tv;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.MediaFiles.EpisodeImport
|
|
|
|
|
{
|
|
|
|
|
public interface IMakeImportDecision
|
|
|
|
|
{
|
|
|
|
|
List<ImportDecision> GetImportDecisions(IEnumerable<String> videoFiles, Series series);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ImportDecisionMaker : IMakeImportDecision
|
|
|
|
|
{
|
|
|
|
|
private readonly IEnumerable<IRejectWithReason> _specifications;
|
|
|
|
|
private readonly IParsingService _parsingService;
|
2013-07-15 23:53:06 +00:00
|
|
|
|
private readonly IDiskProvider _diskProvider;
|
2013-07-06 21:47:49 +00:00
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
2013-07-15 23:53:06 +00:00
|
|
|
|
public ImportDecisionMaker(IEnumerable<IRejectWithReason> specifications,
|
|
|
|
|
IParsingService parsingService,
|
|
|
|
|
IDiskProvider diskProvider,
|
|
|
|
|
Logger logger)
|
2013-07-06 21:47:49 +00:00
|
|
|
|
{
|
|
|
|
|
_specifications = specifications;
|
|
|
|
|
_parsingService = parsingService;
|
2013-07-15 23:53:06 +00:00
|
|
|
|
_diskProvider = diskProvider;
|
2013-07-06 21:47:49 +00:00
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<ImportDecision> GetImportDecisions(IEnumerable<String> videoFiles, Series series)
|
|
|
|
|
{
|
|
|
|
|
return GetDecisions(videoFiles, series).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerable<ImportDecision> GetDecisions(IEnumerable<String> videoFiles, Series series)
|
|
|
|
|
{
|
|
|
|
|
foreach (var file in videoFiles)
|
|
|
|
|
{
|
|
|
|
|
ImportDecision decision = null;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2013-07-13 08:11:04 +00:00
|
|
|
|
var parsedEpisode = _parsingService.GetEpisodes(file, series);
|
2013-07-15 23:53:06 +00:00
|
|
|
|
|
2013-07-06 21:47:49 +00:00
|
|
|
|
if (parsedEpisode != null)
|
|
|
|
|
{
|
2013-07-15 23:53:06 +00:00
|
|
|
|
parsedEpisode.Size = _diskProvider.GetFileSize(file);
|
2013-07-06 21:47:49 +00:00
|
|
|
|
decision = GetDecision(parsedEpisode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
parsedEpisode = new LocalEpisode();
|
|
|
|
|
parsedEpisode.Path = file;
|
|
|
|
|
|
|
|
|
|
decision = new ImportDecision(parsedEpisode, "Unable to parse file");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2013-07-17 00:53:33 +00:00
|
|
|
|
_logger.ErrorException("Couldn't import file." + file, e);
|
2013-07-06 21:47:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (decision != null)
|
|
|
|
|
{
|
|
|
|
|
yield return decision;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ImportDecision GetDecision(LocalEpisode localEpisode)
|
|
|
|
|
{
|
|
|
|
|
var reasons = _specifications.Select(c => EvaluateSpec(c, localEpisode))
|
|
|
|
|
.Where(c => !string.IsNullOrWhiteSpace(c));
|
|
|
|
|
|
|
|
|
|
return new ImportDecision(localEpisode, reasons.ToArray());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string EvaluateSpec(IRejectWithReason spec, LocalEpisode localEpisode)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(spec.RejectionReason))
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("[Need Rejection Text]");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var generalSpecification = spec as IImportDecisionEngineSpecification;
|
|
|
|
|
if (generalSpecification != null && !generalSpecification.IsSatisfiedBy(localEpisode))
|
|
|
|
|
{
|
|
|
|
|
return spec.RejectionReason;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
//e.Data.Add("report", remoteEpisode.Report.ToJson());
|
|
|
|
|
//e.Data.Add("parsed", remoteEpisode.ParsedEpisodeInfo.ToJson());
|
|
|
|
|
_logger.ErrorException("Couldn't evaluate decision on " + localEpisode.Path, e);
|
|
|
|
|
return string.Format("{0}: {1}", spec.GetType().Name, e.Message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|