1
0
Fork 0
mirror of https://github.com/lidarr/Lidarr synced 2024-12-27 10:07:10 +00:00
Lidarr/NzbDrone.Core/MediaFiles/DownloadedEpisodesImportService.cs

156 lines
5.5 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NLog;
using NzbDrone.Common;
2013-05-15 02:57:57 +00:00
using NzbDrone.Common.Messaging;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.MediaFiles.Commands;
using NzbDrone.Core.MediaFiles.EpisodeImport;
2013-07-16 06:43:45 +00:00
using NzbDrone.Core.MediaFiles.EpisodeImport.Specifications;
using NzbDrone.Core.Parser;
using NzbDrone.Core.Tv;
2013-05-15 02:57:57 +00:00
namespace NzbDrone.Core.MediaFiles
{
2013-05-15 02:57:57 +00:00
public class DownloadedEpisodesImportService : IExecute<DownloadedEpisodesScanCommand>
{
2013-05-10 23:53:50 +00:00
private readonly IDiskProvider _diskProvider;
2013-04-15 05:27:32 +00:00
private readonly IDiskScanService _diskScanService;
private readonly ISeriesService _seriesService;
private readonly IParsingService _parsingService;
2013-05-15 02:57:57 +00:00
private readonly IConfigService _configService;
private readonly IMakeImportDecision _importDecisionMaker;
private readonly IImportApprovedEpisodes _importApprovedEpisodes;
private readonly Logger _logger;
2013-05-15 02:57:57 +00:00
public DownloadedEpisodesImportService(IDiskProvider diskProvider,
2013-04-15 05:27:32 +00:00
IDiskScanService diskScanService,
ISeriesService seriesService,
IParsingService parsingService,
2013-05-15 02:57:57 +00:00
IConfigService configService,
IMakeImportDecision importDecisionMaker,
IImportApprovedEpisodes importApprovedEpisodes,
Logger logger)
{
_diskProvider = diskProvider;
2013-04-15 05:27:32 +00:00
_diskScanService = diskScanService;
_seriesService = seriesService;
_parsingService = parsingService;
2013-05-15 02:57:57 +00:00
_configService = configService;
_importDecisionMaker = importDecisionMaker;
_importApprovedEpisodes = importApprovedEpisodes;
_logger = logger;
}
private void ProcessDownloadedEpisodesFolder()
2013-05-16 00:25:51 +00:00
{
//TODO: We should also process the download client's category folder
var downloadedEpisodesFolder = _configService.DownloadedEpisodesFolder;
if (String.IsNullOrEmpty(downloadedEpisodesFolder))
{
_logger.Warn("Downloaded Episodes Folder is not configured");
return;
}
if (!_diskProvider.FolderExists(downloadedEpisodesFolder))
{
_logger.Warn("Downloaded Episodes Folder [{0}] doesn't exist.", downloadedEpisodesFolder);
return;
}
foreach (var subFolder in _diskProvider.GetDirectories(downloadedEpisodesFolder))
2013-05-16 00:25:51 +00:00
{
try
{
if (_seriesService.SeriesPathExists(subFolder))
2013-05-16 00:25:51 +00:00
{
continue;
}
var importedFiles = ProcessSubFolder(new DirectoryInfo(subFolder));
2013-05-16 00:29:26 +00:00
if (importedFiles.Any())
{
if (_diskProvider.GetFolderSize(subFolder) < NotSampleSpecification.SampleSizeLimit)
2013-05-16 00:29:26 +00:00
{
_diskProvider.DeleteFolder(subFolder, true);
2013-05-16 00:29:26 +00:00
}
2013-05-16 00:25:51 +00:00
}
}
catch (Exception e)
{
_logger.ErrorException("An error has occurred while importing folder: " + subFolder, e);
2013-05-16 00:25:51 +00:00
}
}
foreach (var videoFile in _diskScanService.GetVideoFiles(downloadedEpisodesFolder, false))
{
try
{
ProcessVideoFile(videoFile);
2013-05-16 00:25:51 +00:00
}
catch (Exception ex)
{
_logger.ErrorException("An error has occurred while importing video file" + videoFile, ex);
}
}
}
private List<ImportDecision> ProcessSubFolder(DirectoryInfo subfolderInfo)
{
var cleanedUpName = GetCleanedUpFolderName(subfolderInfo.Name);
var series = _parsingService.GetSeries(cleanedUpName);
if (series == null)
{
_logger.Debug("Unknown Series {0}", cleanedUpName);
return new List<ImportDecision>();
}
var videoFiles = _diskScanService.GetVideoFiles(subfolderInfo.FullName);
return ProcessFiles(videoFiles, series);
}
private void ProcessVideoFile(string videoFile)
{
var series = _parsingService.GetSeries(Path.GetFileNameWithoutExtension(videoFile));
if (series == null)
{
_logger.Debug("Unknown Series for file: {0}", videoFile);
return;
}
if (_diskProvider.IsFileLocked(new FileInfo(videoFile)))
{
_logger.Debug("[{0}] is currently locked by another process, skipping", videoFile);
return;
}
ProcessFiles(new[] { videoFile }, series);
}
private List<ImportDecision> ProcessFiles(IEnumerable<string> videoFiles, Series series)
{
2013-07-23 05:50:32 +00:00
var decisions = _importDecisionMaker.GetImportDecisions(videoFiles, series, true);
return _importApprovedEpisodes.Import(decisions, true);
}
private string GetCleanedUpFolderName(string folder)
{
folder = folder.Replace("_UNPACK_", "")
.Replace("_FAILED_", "");
return folder;
}
2013-05-15 02:57:57 +00:00
public void Execute(DownloadedEpisodesScanCommand message)
{
2013-06-09 06:20:38 +00:00
ProcessDownloadedEpisodesFolder();
2013-05-15 02:57:57 +00:00
}
}
}