2010-10-17 17:22:48 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using NLog;
|
2011-03-10 07:49:59 +00:00
|
|
|
|
using NzbDrone.Core.Model;
|
2010-10-21 01:49:23 +00:00
|
|
|
|
using NzbDrone.Core.Model.Notification;
|
2011-04-04 03:50:12 +00:00
|
|
|
|
using NzbDrone.Core.Providers.Core;
|
2010-10-17 17:22:48 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers
|
|
|
|
|
{
|
|
|
|
|
public class SyncProvider : ISyncProvider
|
|
|
|
|
{
|
2011-04-08 23:55:23 +00:00
|
|
|
|
private readonly SeriesProvider _seriesProvider;
|
2011-04-10 00:14:51 +00:00
|
|
|
|
private readonly EpisodeProvider _episodeProvider;
|
2010-10-30 02:46:32 +00:00
|
|
|
|
private readonly IMediaFileProvider _mediaFileProvider;
|
2011-04-08 23:48:47 +00:00
|
|
|
|
private readonly NotificationProvider _notificationProvider;
|
2011-04-09 00:21:57 +00:00
|
|
|
|
private readonly DiskProvider _diskProvider;
|
2010-10-17 17:22:48 +00:00
|
|
|
|
|
|
|
|
|
private ProgressNotification _seriesSyncNotification;
|
|
|
|
|
private Thread _seriesSyncThread;
|
|
|
|
|
|
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
2011-04-10 00:14:51 +00:00
|
|
|
|
public SyncProvider(SeriesProvider seriesProvider, EpisodeProvider episodeProvider,
|
2011-04-08 23:48:47 +00:00
|
|
|
|
IMediaFileProvider mediaFileProvider, NotificationProvider notificationProvider,
|
2011-04-09 00:21:57 +00:00
|
|
|
|
DiskProvider diskProvider)
|
2010-10-17 17:22:48 +00:00
|
|
|
|
{
|
|
|
|
|
_seriesProvider = seriesProvider;
|
|
|
|
|
_episodeProvider = episodeProvider;
|
2010-10-30 02:46:32 +00:00
|
|
|
|
_mediaFileProvider = mediaFileProvider;
|
2010-10-17 17:22:48 +00:00
|
|
|
|
_notificationProvider = notificationProvider;
|
2011-03-09 07:40:48 +00:00
|
|
|
|
_diskProvider = diskProvider;
|
2010-10-17 17:22:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-03-09 07:40:48 +00:00
|
|
|
|
#region ISyncProvider Members
|
|
|
|
|
|
|
|
|
|
public List<String> GetUnmappedFolders(string path)
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Generating list of unmapped folders");
|
|
|
|
|
if (String.IsNullOrEmpty(path))
|
|
|
|
|
throw new InvalidOperationException("Invalid path provided");
|
|
|
|
|
|
|
|
|
|
if (!_diskProvider.FolderExists(path))
|
|
|
|
|
{
|
|
|
|
|
Logger.Debug("Path supplied does not exist: {0}", path);
|
2011-04-01 06:36:34 +00:00
|
|
|
|
return null;
|
2011-03-09 07:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var results = new List<String>();
|
|
|
|
|
foreach (string seriesFolder in _diskProvider.GetDirectories(path))
|
|
|
|
|
{
|
|
|
|
|
var cleanPath = Parser.NormalizePath(new DirectoryInfo(seriesFolder).FullName);
|
|
|
|
|
|
|
|
|
|
if (!_seriesProvider.SeriesPathExists(cleanPath))
|
|
|
|
|
results.Add(cleanPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Logger.Debug("{0} unmapped folders detected.", results.Count);
|
|
|
|
|
return results;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2011-04-01 06:36:34 +00:00
|
|
|
|
public bool BeginUpdateNewSeries()
|
2010-10-17 17:22:48 +00:00
|
|
|
|
{
|
2011-04-01 06:36:34 +00:00
|
|
|
|
Logger.Debug("User has requested a scan of new series");
|
2010-10-17 17:22:48 +00:00
|
|
|
|
if (_seriesSyncThread == null || !_seriesSyncThread.IsAlive)
|
|
|
|
|
{
|
2011-04-01 06:36:34 +00:00
|
|
|
|
Logger.Debug("Initializing background scan thread");
|
|
|
|
|
_seriesSyncThread = new Thread(SyncNewSeries)
|
2010-10-17 17:22:48 +00:00
|
|
|
|
{
|
2011-04-01 06:36:34 +00:00
|
|
|
|
Name = "SyncNewSeries",
|
2010-10-18 06:06:16 +00:00
|
|
|
|
Priority = ThreadPriority.Lowest
|
2010-10-17 17:22:48 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
_seriesSyncThread.Start();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Logger.Warn("Series folder scan already in progress. Ignoring request.");
|
2011-03-09 07:40:48 +00:00
|
|
|
|
|
|
|
|
|
//return false if sync was already running, then we can tell the user to try again later
|
|
|
|
|
return false;
|
2010-10-17 17:22:48 +00:00
|
|
|
|
}
|
2011-03-09 07:40:48 +00:00
|
|
|
|
|
|
|
|
|
//return true if sync has started
|
|
|
|
|
return true;
|
2010-10-17 17:22:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-04-01 06:36:34 +00:00
|
|
|
|
|
|
|
|
|
private void SyncNewSeries()
|
2011-03-15 15:29:14 +00:00
|
|
|
|
{
|
2011-04-01 06:36:34 +00:00
|
|
|
|
Logger.Info("Syncing new series");
|
|
|
|
|
|
|
|
|
|
try
|
2011-03-15 15:29:14 +00:00
|
|
|
|
{
|
2011-04-01 06:36:34 +00:00
|
|
|
|
using (_seriesSyncNotification = new ProgressNotification("Series Scan"))
|
2011-03-15 15:29:14 +00:00
|
|
|
|
{
|
2011-04-01 06:36:34 +00:00
|
|
|
|
_notificationProvider.Register(_seriesSyncNotification);
|
2011-03-15 15:29:14 +00:00
|
|
|
|
|
2011-04-01 06:36:34 +00:00
|
|
|
|
_seriesSyncNotification.CurrentStatus = "Finding New Series";
|
|
|
|
|
ScanSeries();
|
2011-03-15 15:29:14 +00:00
|
|
|
|
|
2011-04-01 06:36:34 +00:00
|
|
|
|
_seriesSyncNotification.CurrentStatus = "Series Scan Completed";
|
|
|
|
|
Logger.Info("Series folders scan has successfully completed.");
|
|
|
|
|
Thread.Sleep(3000);
|
|
|
|
|
_seriesSyncNotification.Status = ProgressNotificationStatus.Completed;
|
|
|
|
|
}
|
2011-03-15 15:29:14 +00:00
|
|
|
|
}
|
2011-04-01 06:36:34 +00:00
|
|
|
|
catch (Exception e)
|
2011-03-15 15:29:14 +00:00
|
|
|
|
{
|
2011-04-01 06:36:34 +00:00
|
|
|
|
Logger.ErrorException(e.Message, e);
|
2011-03-15 15:29:14 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-01 06:36:34 +00:00
|
|
|
|
private void ScanSeries()
|
2011-03-17 07:40:23 +00:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
2011-04-01 06:36:34 +00:00
|
|
|
|
var syncList = _seriesProvider.GetAllSeries().Where(s => s.LastInfoSync == null).ToList();
|
|
|
|
|
if (syncList.Count == 0) return;
|
2011-03-17 07:40:23 +00:00
|
|
|
|
|
2011-04-01 06:36:34 +00:00
|
|
|
|
_seriesSyncNotification.ProgressMax = syncList.Count;
|
|
|
|
|
|
|
|
|
|
foreach (var currentSeries in syncList)
|
2011-03-17 07:40:23 +00:00
|
|
|
|
{
|
2011-04-01 06:36:34 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_seriesSyncNotification.CurrentStatus = String.Format("Searching For: {0}", new DirectoryInfo(currentSeries.Path).Name);
|
|
|
|
|
var updatedSeries = _seriesProvider.UpdateSeriesInfo(currentSeries.SeriesId);
|
2011-03-17 07:40:23 +00:00
|
|
|
|
|
2011-04-01 06:36:34 +00:00
|
|
|
|
_seriesSyncNotification.CurrentStatus = String.Format("Downloading episode info For: {0}", updatedSeries.Title);
|
|
|
|
|
_episodeProvider.RefreshEpisodeInfo(updatedSeries.SeriesId);
|
2011-03-17 07:40:23 +00:00
|
|
|
|
|
2011-04-01 06:36:34 +00:00
|
|
|
|
_seriesSyncNotification.CurrentStatus = String.Format("Scanning series folder {0}", updatedSeries.Path);
|
|
|
|
|
_mediaFileProvider.Scan(_seriesProvider.GetSeries(updatedSeries.SeriesId));
|
2011-03-17 07:40:23 +00:00
|
|
|
|
|
2011-04-01 06:36:34 +00:00
|
|
|
|
//Todo: Launch Backlog search for this series _backlogProvider.StartSearch(mappedSeries.Id);
|
|
|
|
|
}
|
2010-10-17 17:22:48 +00:00
|
|
|
|
|
2011-04-01 06:36:34 +00:00
|
|
|
|
catch (Exception e)
|
2010-10-17 17:22:48 +00:00
|
|
|
|
{
|
2011-04-01 06:36:34 +00:00
|
|
|
|
Logger.ErrorException(e.Message, e);
|
2010-10-17 17:22:48 +00:00
|
|
|
|
}
|
2011-04-01 06:36:34 +00:00
|
|
|
|
_seriesSyncNotification.ProgressValue++;
|
2010-10-17 17:22:48 +00:00
|
|
|
|
}
|
2011-04-01 06:36:34 +00:00
|
|
|
|
|
|
|
|
|
//Keep scanning until there no more shows left.
|
|
|
|
|
ScanSeries();
|
2010-10-17 17:22:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|