2013-04-15 03:41:39 +02:00
|
|
|
|
using System;
|
2013-07-06 23:47:49 +02:00
|
|
|
|
using System.Collections.Generic;
|
2013-04-15 03:41:39 +02:00
|
|
|
|
using System.IO;
|
2013-07-18 06:50:58 +02:00
|
|
|
|
using System.Linq;
|
2013-04-15 03:41:39 +02:00
|
|
|
|
using NLog;
|
|
|
|
|
using NzbDrone.Common;
|
2013-05-15 04:57:57 +02:00
|
|
|
|
using NzbDrone.Common.Messaging;
|
|
|
|
|
using NzbDrone.Core.Configuration;
|
|
|
|
|
using NzbDrone.Core.MediaFiles.Commands;
|
2013-07-06 23:47:49 +02:00
|
|
|
|
using NzbDrone.Core.MediaFiles.EpisodeImport;
|
2013-07-16 08:43:45 +02:00
|
|
|
|
using NzbDrone.Core.MediaFiles.EpisodeImport.Specifications;
|
2013-04-15 03:41:39 +02:00
|
|
|
|
using NzbDrone.Core.Parser;
|
|
|
|
|
using NzbDrone.Core.Tv;
|
|
|
|
|
|
2013-05-15 04:57:57 +02:00
|
|
|
|
namespace NzbDrone.Core.MediaFiles
|
2013-04-15 03:41:39 +02:00
|
|
|
|
{
|
2013-05-15 04:57:57 +02:00
|
|
|
|
public class DownloadedEpisodesImportService : IExecute<DownloadedEpisodesScanCommand>
|
2013-04-15 03:41:39 +02:00
|
|
|
|
{
|
2013-05-11 01:53:50 +02:00
|
|
|
|
private readonly IDiskProvider _diskProvider;
|
2013-04-15 07:27:32 +02:00
|
|
|
|
private readonly IDiskScanService _diskScanService;
|
2013-04-15 03:41:39 +02:00
|
|
|
|
private readonly ISeriesService _seriesService;
|
|
|
|
|
private readonly IParsingService _parsingService;
|
2013-05-15 04:57:57 +02:00
|
|
|
|
private readonly IConfigService _configService;
|
2013-07-06 23:47:49 +02:00
|
|
|
|
private readonly IMakeImportDecision _importDecisionMaker;
|
|
|
|
|
private readonly IImportApprovedEpisodes _importApprovedEpisodes;
|
2013-04-15 03:41:39 +02:00
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
2013-05-15 04:57:57 +02:00
|
|
|
|
public DownloadedEpisodesImportService(IDiskProvider diskProvider,
|
2013-04-15 07:27:32 +02:00
|
|
|
|
IDiskScanService diskScanService,
|
2013-04-15 03:41:39 +02:00
|
|
|
|
ISeriesService seriesService,
|
|
|
|
|
IParsingService parsingService,
|
2013-05-15 04:57:57 +02:00
|
|
|
|
IConfigService configService,
|
2013-07-06 23:47:49 +02:00
|
|
|
|
IMakeImportDecision importDecisionMaker,
|
|
|
|
|
IImportApprovedEpisodes importApprovedEpisodes,
|
2013-04-15 03:41:39 +02:00
|
|
|
|
Logger logger)
|
|
|
|
|
{
|
|
|
|
|
_diskProvider = diskProvider;
|
2013-04-15 07:27:32 +02:00
|
|
|
|
_diskScanService = diskScanService;
|
2013-04-15 03:41:39 +02:00
|
|
|
|
_seriesService = seriesService;
|
|
|
|
|
_parsingService = parsingService;
|
2013-05-15 04:57:57 +02:00
|
|
|
|
_configService = configService;
|
2013-07-06 23:47:49 +02:00
|
|
|
|
_importDecisionMaker = importDecisionMaker;
|
|
|
|
|
_importApprovedEpisodes = importApprovedEpisodes;
|
2013-04-15 03:41:39 +02:00
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-16 19:55:11 +02:00
|
|
|
|
private void ProcessDownloadedEpisodesFolder()
|
2013-05-16 02:25:51 +02: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;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-16 19:55:11 +02:00
|
|
|
|
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 02:25:51 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2013-07-18 06:50:58 +02:00
|
|
|
|
if (_seriesService.SeriesPathExists(subFolder))
|
2013-05-16 02:25:51 +02:00
|
|
|
|
{
|
2013-07-18 06:50:58 +02:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var importedFiles = ProcessSubFolder(new DirectoryInfo(subFolder));
|
2013-05-16 02:29:26 +02:00
|
|
|
|
|
2013-07-18 06:50:58 +02:00
|
|
|
|
if (importedFiles.Any())
|
|
|
|
|
{
|
2013-07-16 19:55:11 +02:00
|
|
|
|
if (_diskProvider.GetFolderSize(subFolder) < NotSampleSpecification.SampleSizeLimit)
|
2013-05-16 02:29:26 +02:00
|
|
|
|
{
|
2013-07-16 19:55:11 +02:00
|
|
|
|
_diskProvider.DeleteFolder(subFolder, true);
|
2013-05-16 02:29:26 +02:00
|
|
|
|
}
|
2013-05-16 02:25:51 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2013-07-16 19:55:11 +02:00
|
|
|
|
_logger.ErrorException("An error has occurred while importing folder: " + subFolder, e);
|
2013-05-16 02:25:51 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var videoFile in _diskScanService.GetVideoFiles(downloadedEpisodesFolder, false))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2013-07-18 06:50:58 +02:00
|
|
|
|
ProcessVideoFile(videoFile);
|
2013-05-16 02:25:51 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("An error has occurred while importing video file" + videoFile, ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-18 06:50:58 +02:00
|
|
|
|
private List<ImportDecision> ProcessSubFolder(DirectoryInfo subfolderInfo)
|
2013-04-15 03:41:39 +02:00
|
|
|
|
{
|
|
|
|
|
var series = _parsingService.GetSeries(subfolderInfo.Name);
|
|
|
|
|
|
|
|
|
|
if (series == null)
|
|
|
|
|
{
|
2013-07-17 02:53:33 +02:00
|
|
|
|
_logger.Debug("Unknown Series {0}", subfolderInfo.Name);
|
2013-07-18 06:50:58 +02:00
|
|
|
|
return new List<ImportDecision>();
|
2013-04-15 03:41:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-06 23:47:49 +02:00
|
|
|
|
var videoFiles = _diskScanService.GetVideoFiles(subfolderInfo.FullName);
|
2013-04-15 03:41:39 +02:00
|
|
|
|
|
2013-07-18 06:50:58 +02:00
|
|
|
|
return ProcessFiles(videoFiles, series);
|
2013-04-15 03:41:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-18 06:50:58 +02:00
|
|
|
|
private void ProcessVideoFile(string videoFile)
|
2013-04-15 03:41:39 +02:00
|
|
|
|
{
|
2013-07-18 06:50:58 +02:00
|
|
|
|
var series = _parsingService.GetSeries(Path.GetFileNameWithoutExtension(videoFile));
|
|
|
|
|
|
|
|
|
|
if (series == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("Unknown Series for file: {0}", videoFile);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-15 03:41:39 +02:00
|
|
|
|
if (_diskProvider.IsFileLocked(new FileInfo(videoFile)))
|
|
|
|
|
{
|
2013-07-17 02:53:33 +02:00
|
|
|
|
_logger.Debug("[{0}] is currently locked by another process, skipping", videoFile);
|
2013-04-15 03:41:39 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-16 19:55:11 +02:00
|
|
|
|
ProcessFiles(new[] { videoFile }, series);
|
2013-07-06 23:47:49 +02:00
|
|
|
|
}
|
2013-04-15 03:41:39 +02:00
|
|
|
|
|
2013-07-18 06:50:58 +02:00
|
|
|
|
private List<ImportDecision> ProcessFiles(IEnumerable<string> videoFiles, Series series)
|
2013-07-06 23:47:49 +02:00
|
|
|
|
{
|
|
|
|
|
var decisions = _importDecisionMaker.GetImportDecisions(videoFiles, series);
|
2013-07-18 06:50:58 +02:00
|
|
|
|
return _importApprovedEpisodes.Import(decisions, true);
|
2013-04-15 03:41:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-15 04:57:57 +02:00
|
|
|
|
public void Execute(DownloadedEpisodesScanCommand message)
|
|
|
|
|
{
|
2013-06-09 08:20:38 +02:00
|
|
|
|
ProcessDownloadedEpisodesFolder();
|
2013-05-15 04:57:57 +02:00
|
|
|
|
}
|
2013-04-15 03:41:39 +02:00
|
|
|
|
}
|
|
|
|
|
}
|