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