1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-21 01:49:57 +02:00
Sonarr/NzbDrone.Core/MediaFiles/EpisodeImport/ImportApprovedEpisodes.cs

97 lines
3.8 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NLog;
using NzbDrone.Common;
using NzbDrone.Core.MediaFiles.Events;
2013-09-11 09:33:47 +03:00
using NzbDrone.Core.Messaging;
namespace NzbDrone.Core.MediaFiles.EpisodeImport
{
public interface IImportApprovedEpisodes
{
List<ImportDecision> Import(List<ImportDecision> decisions, bool newDownloads = false);
}
2013-08-30 09:39:41 +03:00
public class ImportApprovedEpisodes : IImportApprovedEpisodes
{
private readonly IUpgradeMediaFiles _episodeFileUpgrader;
2013-07-12 08:57:24 +03:00
private readonly IMediaFileService _mediaFileService;
private readonly IDiskProvider _diskProvider;
private readonly IMessageAggregator _messageAggregator;
private readonly Logger _logger;
public ImportApprovedEpisodes(IUpgradeMediaFiles episodeFileUpgrader,
2013-07-12 08:57:24 +03:00
IMediaFileService mediaFileService,
IDiskProvider diskProvider,
IMessageAggregator messageAggregator,
Logger logger)
{
_episodeFileUpgrader = episodeFileUpgrader;
_mediaFileService = mediaFileService;
_diskProvider = diskProvider;
_messageAggregator = messageAggregator;
_logger = logger;
}
public List<ImportDecision> Import(List<ImportDecision> decisions, bool newDownload = false)
{
2013-07-12 08:57:24 +03:00
var qualifiedImports = GetQualifiedImports(decisions);
var imported = new List<ImportDecision>();
2013-07-12 08:57:24 +03:00
foreach (var importDecision in qualifiedImports)
{
2013-07-12 08:57:24 +03:00
var localEpisode = importDecision.LocalEpisode;
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;
2013-07-26 08:22:22 +03:00
episodeFile.Path = localEpisode.Path.CleanFilePath();
episodeFile.Size = _diskProvider.GetFileSize(localEpisode.Path);
episodeFile.Quality = localEpisode.Quality;
episodeFile.SeasonNumber = localEpisode.SeasonNumber;
episodeFile.Episodes = localEpisode.Episodes;
2013-08-30 09:38:55 +03:00
if (newDownload)
{
episodeFile.SceneName = Path.GetFileNameWithoutExtension(localEpisode.Path.CleanFilePath());
2013-08-30 09:39:41 +03:00
episodeFile.Path = _episodeFileUpgrader.UpgradeEpisodeFile(episodeFile, localEpisode);
2013-08-30 09:38:55 +03:00
_messageAggregator.PublishEvent(new EpisodeImportedEvent(localEpisode, episodeFile));
2013-08-30 09:39:41 +03:00
_messageAggregator.PublishEvent(new EpisodeDownloadedEvent(localEpisode));
}
2013-08-30 09:38:55 +03:00
_mediaFileService.Add(episodeFile);
2013-07-12 08:57:24 +03:00
imported.Add(importDecision);
}
catch (Exception e)
{
_logger.WarnException("Couldn't import episode " + localEpisode, e);
}
}
return imported;
}
2013-07-12 08:57:24 +03:00
private List<ImportDecision> GetQualifiedImports(List<ImportDecision> decisions)
{
return decisions.Where(c => c.Approved)
.OrderByDescending(c => c.LocalEpisode.Quality)
.ToList();
}
}
}