2013-07-06 21:47:49 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using NLog;
|
|
|
|
|
using NzbDrone.Common;
|
|
|
|
|
using NzbDrone.Common.Messaging;
|
|
|
|
|
using NzbDrone.Core.MediaFiles.Events;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.MediaFiles.EpisodeImport
|
|
|
|
|
{
|
|
|
|
|
public interface IImportApprovedEpisodes
|
|
|
|
|
{
|
|
|
|
|
List<ImportDecision> Import(List<ImportDecision> decisions, bool newDownloads = false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ImportApprovedEpisodes : IImportApprovedEpisodes
|
|
|
|
|
{
|
|
|
|
|
private readonly IMoveEpisodeFiles _episodeFileMover;
|
2013-07-12 05:57:24 +00:00
|
|
|
|
private readonly IMediaFileService _mediaFileService;
|
|
|
|
|
private readonly IDiskProvider _diskProvider;
|
2013-07-06 21:47:49 +00:00
|
|
|
|
private readonly IMessageAggregator _messageAggregator;
|
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
|
|
|
|
public ImportApprovedEpisodes(IMoveEpisodeFiles episodeFileMover,
|
2013-07-12 05:57:24 +00:00
|
|
|
|
IMediaFileService mediaFileService,
|
|
|
|
|
IDiskProvider diskProvider,
|
2013-07-06 21:47:49 +00:00
|
|
|
|
IMessageAggregator messageAggregator,
|
|
|
|
|
Logger logger)
|
|
|
|
|
{
|
|
|
|
|
_episodeFileMover = episodeFileMover;
|
|
|
|
|
_mediaFileService = mediaFileService;
|
|
|
|
|
_diskProvider = diskProvider;
|
|
|
|
|
_messageAggregator = messageAggregator;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<ImportDecision> Import(List<ImportDecision> decisions, bool newDownload = false)
|
|
|
|
|
{
|
2013-07-12 05:57:24 +00:00
|
|
|
|
var qualifiedImports = GetQualifiedImports(decisions);
|
2013-07-06 21:47:49 +00:00
|
|
|
|
var imported = new List<ImportDecision>();
|
|
|
|
|
|
2013-07-12 05:57:24 +00:00
|
|
|
|
foreach (var importDecision in qualifiedImports)
|
2013-07-06 21:47:49 +00:00
|
|
|
|
{
|
2013-07-12 05:57:24 +00:00
|
|
|
|
var localEpisode = importDecision.LocalEpisode;
|
2013-07-06 21:47:49 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (imported.SelectMany(r => r.LocalEpisode.Episodes)
|
|
|
|
|
.Select(e => e.Id)
|
|
|
|
|
.ToList()
|
|
|
|
|
.Intersect(localEpisode.Episodes.Select(e => e.Id))
|
|
|
|
|
.Any())
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var episodeFile = new EpisodeFile();
|
|
|
|
|
episodeFile.DateAdded = DateTime.UtcNow;
|
|
|
|
|
episodeFile.SeriesId = localEpisode.Series.Id;
|
|
|
|
|
episodeFile.Path = localEpisode.Path.CleanPath();
|
|
|
|
|
episodeFile.Size = _diskProvider.GetFileSize(localEpisode.Path);
|
|
|
|
|
episodeFile.Quality = localEpisode.Quality;
|
|
|
|
|
episodeFile.SeasonNumber = localEpisode.SeasonNumber;
|
|
|
|
|
episodeFile.SceneName = Path.GetFileNameWithoutExtension(localEpisode.Path.CleanPath());
|
|
|
|
|
episodeFile.Episodes = localEpisode.Episodes;
|
|
|
|
|
|
|
|
|
|
if (newDownload)
|
|
|
|
|
{
|
|
|
|
|
episodeFile = _episodeFileMover.MoveEpisodeFile(episodeFile, localEpisode);
|
2013-07-12 02:19:37 +00:00
|
|
|
|
_messageAggregator.PublishEvent(new EpisodeImportedEvent(episodeFile));
|
2013-07-06 21:47:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_mediaFileService.Add(episodeFile);
|
2013-07-12 05:57:24 +00:00
|
|
|
|
imported.Add(importDecision);
|
2013-07-06 21:47:49 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2013-07-13 08:11:04 +00:00
|
|
|
|
_logger.WarnException("Couldn't import episode " + localEpisode, e);
|
2013-07-06 21:47:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return imported;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-12 05:57:24 +00:00
|
|
|
|
private List<ImportDecision> GetQualifiedImports(List<ImportDecision> decisions)
|
2013-07-06 21:47:49 +00:00
|
|
|
|
{
|
|
|
|
|
return decisions.Where(c => c.Approved)
|
|
|
|
|
.OrderByDescending(c => c.LocalEpisode.Quality)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|