diff --git a/NzbDrone.Core.Test/CentralDispatchFixture.cs b/NzbDrone.Core.Test/CentralDispatchFixture.cs index 76546ff52..95f273d44 100644 --- a/NzbDrone.Core.Test/CentralDispatchFixture.cs +++ b/NzbDrone.Core.Test/CentralDispatchFixture.cs @@ -7,10 +7,10 @@ using FluentAssertions; using NCrunch.Framework; using NUnit.Framework; using NzbDrone.Common; +using NzbDrone.Core.ExternalNotification; using NzbDrone.Core.Indexers; using NzbDrone.Core.Jobs; using NzbDrone.Core.Providers; -using NzbDrone.Core.Providers.ExternalNotification; using NzbDrone.Core.Providers.Metadata; using NzbDrone.Core.Test.Framework; @@ -115,12 +115,6 @@ namespace NzbDrone.Core.Test kernel.Resolve().All().Select(c => c.Type).Should().BeEquivalentTo(indexers); } - [Test] - public void externalNotifiers_are_initialized() - { - kernel.Resolve().All().Should().HaveSameCount(extNotifications); - } - [Test] public void metadata_clients_are_initialized() { diff --git a/NzbDrone.Core.Test/ProviderTests/DiskScanProviderTests/MoveEpisodeFileFixture.cs b/NzbDrone.Core.Test/ProviderTests/DiskScanProviderTests/MoveEpisodeFileFixture.cs index f3d785d35..daa315801 100644 --- a/NzbDrone.Core.Test/ProviderTests/DiskScanProviderTests/MoveEpisodeFileFixture.cs +++ b/NzbDrone.Core.Test/ProviderTests/DiskScanProviderTests/MoveEpisodeFileFixture.cs @@ -9,6 +9,7 @@ using Moq; using NUnit.Framework; using NzbDrone.Common; using NzbDrone.Core.Download; +using NzbDrone.Core.ExternalNotification; using NzbDrone.Core.Tv; using NzbDrone.Core.Model; using NzbDrone.Core.Providers; @@ -117,16 +118,9 @@ namespace NzbDrone.Core.Test.ProviderTests.DiskScanProviderTests .Setup(s => s.FileExists(currentFilename)) .Returns(true); - Mocker.GetMock() - .Setup(e => e.OnDownload("30 Rock - 1x01 - [WEBDL]", It.IsAny())); - - //Act var result = Mocker.Resolve().MoveEpisodeFile(file, true); - //Assert - result.Should().NotBeNull(); - Mocker.GetMock() - .Verify(e => e.OnDownload("30 Rock - 1x01 - [WEBDL]", It.IsAny()), Times.Once()); + } [Test] @@ -172,13 +166,8 @@ namespace NzbDrone.Core.Test.ProviderTests.DiskScanProviderTests .Setup(e => e.CalculateFilePath(It.IsAny(), fakeEpisode.First().SeasonNumber, filename, ".mkv")) .Returns(fi); - Mocker.GetMock() - .Setup(e => e.OnDownload("30 Rock - 1x01 - [WEBDL]", It.IsAny())); - - //Act var result = Mocker.Resolve().MoveEpisodeFile(file, true); - //Assert result.Should().BeNull(); ExceptionVerification.ExpectedErrors(1); } diff --git a/NzbDrone.Core.Test/ProviderTests/GrowlProviderTest.cs b/NzbDrone.Core.Test/ProviderTests/GrowlProviderTest.cs index a2e89ca7c..6954ab326 100644 --- a/NzbDrone.Core.Test/ProviderTests/GrowlProviderTest.cs +++ b/NzbDrone.Core.Test/ProviderTests/GrowlProviderTest.cs @@ -4,6 +4,7 @@ using FizzWare.NBuilder; using FluentAssertions; using Moq; using NUnit.Framework; +using NzbDrone.Core.ExternalNotification; using NzbDrone.Core.Model; using NzbDrone.Core.Providers; using NzbDrone.Core.Repository; diff --git a/NzbDrone.Core.Test/ProviderTests/PlexProviderTest.cs b/NzbDrone.Core.Test/ProviderTests/PlexProviderTest.cs index be7357d1a..4b942134f 100644 --- a/NzbDrone.Core.Test/ProviderTests/PlexProviderTest.cs +++ b/NzbDrone.Core.Test/ProviderTests/PlexProviderTest.cs @@ -11,6 +11,7 @@ using Moq; using NUnit.Framework; using NzbDrone.Common; using NzbDrone.Core.Configuration; +using NzbDrone.Core.ExternalNotification; using NzbDrone.Core.Model.Xbmc; using NzbDrone.Core.Providers; using NzbDrone.Core.Providers.Core; diff --git a/NzbDrone.Core/ContainerExtentions.cs b/NzbDrone.Core/ContainerExtentions.cs index 9bebe7808..80b8f7058 100644 --- a/NzbDrone.Core/ContainerExtentions.cs +++ b/NzbDrone.Core/ContainerExtentions.cs @@ -6,9 +6,9 @@ using Autofac.Core; using NLog; using NzbDrone.Common; using NzbDrone.Core.Datastore; +using NzbDrone.Core.ExternalNotification; using NzbDrone.Core.Indexers; using NzbDrone.Core.Instrumentation; -using NzbDrone.Core.Providers.ExternalNotification; using NzbDrone.Core.Providers.Metadata; using NzbDrone.Core.Providers.Search; using PetaPoco; diff --git a/NzbDrone.Core/Download/EpisodeDownloadedEvent.cs b/NzbDrone.Core/Download/EpisodeDownloadedEvent.cs new file mode 100644 index 000000000..f7ded7c8c --- /dev/null +++ b/NzbDrone.Core/Download/EpisodeDownloadedEvent.cs @@ -0,0 +1,16 @@ +using System.Linq; +using NzbDrone.Common.Eventing; +using NzbDrone.Core.Model; + +namespace NzbDrone.Core.Download +{ + public class EpisodeDownloadedEvent : IEvent + { + public EpisodeParseResult ParseResult { get; private set; } + + public EpisodeDownloadedEvent(EpisodeParseResult parseResult) + { + ParseResult = parseResult; + } + } +} \ No newline at end of file diff --git a/NzbDrone.Core/Download/SeriesRenamedEvent.cs b/NzbDrone.Core/Download/SeriesRenamedEvent.cs new file mode 100644 index 000000000..32f8036d4 --- /dev/null +++ b/NzbDrone.Core/Download/SeriesRenamedEvent.cs @@ -0,0 +1,16 @@ +using System.Linq; +using NzbDrone.Common.Eventing; +using NzbDrone.Core.Tv; + +namespace NzbDrone.Core.Download +{ + public class SeriesRenamedEvent : IEvent + { + public Series Series { get; private set; } + + public SeriesRenamedEvent(Series series) + { + Series = series; + } + } +} \ No newline at end of file diff --git a/NzbDrone.Core/ExternalNotification/ExternalNotificationBase.cs b/NzbDrone.Core/ExternalNotification/ExternalNotificationBase.cs new file mode 100644 index 000000000..784bd137f --- /dev/null +++ b/NzbDrone.Core/ExternalNotification/ExternalNotificationBase.cs @@ -0,0 +1,149 @@ +using System; +using System.Linq; +using NLog; +using NzbDrone.Common.Eventing; +using NzbDrone.Core.Download; +using NzbDrone.Core.Tv; + +namespace NzbDrone.Core.ExternalNotification +{ + public abstract class ExternalNotificationBase + : IHandle, + IHandle, + IHandle + { + private readonly IExternalNotificationRepository _externalNotificationRepository; + private readonly Logger _logger; + + protected ExternalNotificationBase(IExternalNotificationRepository externalNotificationRepository, Logger logger) + { + _externalNotificationRepository = externalNotificationRepository; + _logger = logger; + } + + public abstract string Name { get; } + + public bool NotifyOnGrab + { + get + { + return GetEnableStatus(c => c.OnGrab); + } + set + { + SetEnableStatus(c => c.OnGrab = value); + } + } + + public bool NotifyOnDownload + { + get + { + return GetEnableStatus(c => c.OnDownload); + } + set + { + SetEnableStatus(c => c.OnDownload = value); + } + } + + public bool NotifyOnRename + { + get + { + return GetEnableStatus(c => c.OnRename); + } + set + { + SetEnableStatus(c => c.OnRename = value); + } + } + + private void SetEnableStatus(Action updateAction) + { + var def = _externalNotificationRepository.Get(Name) ?? + new ExternalNotificationDefinition { Name = Name }; + + updateAction(def); + _externalNotificationRepository.Upsert(def); + } + + private bool GetEnableStatus(Func readFunction) + { + var def = _externalNotificationRepository.Get(Name) ?? + new ExternalNotificationDefinition { Name = Name }; + + return readFunction(def); + } + + + + public void Handle(EpisodeGrabbedEvent message) + { + if (NotifyOnGrab) + { + try + { + _logger.Trace("Sending grab notification to {0}", Name); + OnGrab(message.ParseResult.GetDownloadTitle()); + + } + catch (Exception e) + { + _logger.WarnException("Couldn't send grab notification to " + Name, e); + } + } + } + + public void Handle(EpisodeDownloadedEvent message) + { + if (NotifyOnDownload) + { + try + { + _logger.Trace("Sending download notification to {0}", Name); + OnDownload(message.ParseResult.GetDownloadTitle(), message.ParseResult.Series); + } + catch (Exception e) + { + _logger.WarnException("Couldn't send download notification to " + Name, e); + } + } + } + + public void Handle(SeriesRenamedEvent message) + { + if (NotifyOnRename) + { + try + { + _logger.Trace("Sending rename notification to {0}", Name); + AfterRename(message.Series); + + } + catch (Exception e) + { + _logger.WarnException("Couldn't send rename notification to " + Name, e); + } + } + } + + + protected virtual void OnGrab(string message) + { + + } + + protected virtual void OnDownload(string message, Series series) + { + + } + + protected virtual void AfterRename(Series series) + { + + } + + + } +} diff --git a/NzbDrone.Core/ExternalNotification/ExternalNotificationDefinition.cs b/NzbDrone.Core/ExternalNotification/ExternalNotificationDefinition.cs new file mode 100644 index 000000000..e1c9326fa --- /dev/null +++ b/NzbDrone.Core/ExternalNotification/ExternalNotificationDefinition.cs @@ -0,0 +1,14 @@ +using System.Linq; +using NzbDrone.Core.Datastore; +using PetaPoco; + +namespace NzbDrone.Core.ExternalNotification +{ + public class ExternalNotificationDefinition : ModelBase + { + public string Name { get; set; } + public bool OnGrab { get; set; } + public bool OnDownload { get; set; } + public bool OnRename { get; set; } + } +} \ No newline at end of file diff --git a/NzbDrone.Core/ExternalNotification/ExternalNotificationRepository.cs b/NzbDrone.Core/ExternalNotification/ExternalNotificationRepository.cs new file mode 100644 index 000000000..e9bba4f92 --- /dev/null +++ b/NzbDrone.Core/ExternalNotification/ExternalNotificationRepository.cs @@ -0,0 +1,23 @@ +using System.Linq; +using NzbDrone.Core.Datastore; + +namespace NzbDrone.Core.ExternalNotification +{ + public interface IExternalNotificationRepository : IBasicRepository + { + ExternalNotificationDefinition Get(string name); + } + + public class ExternalNotificationRepository : BasicRepository, IExternalNotificationRepository + { + public ExternalNotificationRepository(IObjectDatabase objectDatabase) + : base(objectDatabase) + { + } + + public ExternalNotificationDefinition Get(string name) + { + return Queryable.SingleOrDefault(c => c.Name.ToLower() == name.ToLower()); + } + } +} \ No newline at end of file diff --git a/NzbDrone.Core/ExternalNotification/Growl.cs b/NzbDrone.Core/ExternalNotification/Growl.cs new file mode 100644 index 000000000..9b28df48b --- /dev/null +++ b/NzbDrone.Core/ExternalNotification/Growl.cs @@ -0,0 +1,48 @@ +using System.Linq; +using System; +using NLog; +using NzbDrone.Core.Configuration; +using NzbDrone.Core.Tv; + +namespace NzbDrone.Core.ExternalNotification +{ + public class Growl : ExternalNotificationBase + { + private readonly IConfigService _configService; + private readonly GrowlProvider _growlProvider; + + public Growl(IExternalNotificationRepository repository, IConfigService configService, GrowlProvider growlProvider, Logger logger) + : base(repository, logger) + { + _configService = configService; + _growlProvider = growlProvider; + } + + public override string Name + { + get { return "Growl"; } + } + + protected override void OnGrab(string message) + { + const string title = "Episode Grabbed"; + + var growlHost = _configService.GrowlHost.Split(':'); + var host = growlHost[0]; + var port = Convert.ToInt32(growlHost[1]); + + _growlProvider.SendNotification(title, message, "GRAB", host, port, _configService.GrowlPassword); + } + + protected override void OnDownload(string message, Series series) + { + const string title = "Episode Downloaded"; + + var growlHost = _configService.GrowlHost.Split(':'); + var host = growlHost[0]; + var port = Convert.ToInt32(growlHost[1]); + + _growlProvider.SendNotification(title, message, "DOWNLOAD", host, port, _configService.GrowlPassword); + } + } +} diff --git a/NzbDrone.Core/Providers/GrowlProvider.cs b/NzbDrone.Core/ExternalNotification/GrowlProvider.cs similarity index 98% rename from NzbDrone.Core/Providers/GrowlProvider.cs rename to NzbDrone.Core/ExternalNotification/GrowlProvider.cs index 05cd42942..bdbb12046 100644 --- a/NzbDrone.Core/Providers/GrowlProvider.cs +++ b/NzbDrone.Core/ExternalNotification/GrowlProvider.cs @@ -4,7 +4,7 @@ using System.Linq; using Growl.Connector; using NLog; -namespace NzbDrone.Core.Providers +namespace NzbDrone.Core.ExternalNotification { public class GrowlProvider { diff --git a/NzbDrone.Core/ExternalNotification/Plex.cs b/NzbDrone.Core/ExternalNotification/Plex.cs new file mode 100644 index 000000000..b9169b742 --- /dev/null +++ b/NzbDrone.Core/ExternalNotification/Plex.cs @@ -0,0 +1,51 @@ +using System.Linq; +using NLog; +using NzbDrone.Core.Configuration; +using NzbDrone.Core.Tv; + +namespace NzbDrone.Core.ExternalNotification +{ + public class Plex : ExternalNotificationBase + { + private readonly IConfigService _configService; + private readonly PlexProvider _plexProvider; + + public Plex(IConfigService configService, IExternalNotificationRepository repository, PlexProvider plexProvider, Logger logger) + : base(repository, logger) + { + _configService = configService; + _plexProvider = plexProvider; + } + + public override string Name + { + get { return "Plex"; } + } + + protected override void OnGrab(string message) + { + const string header = "NzbDrone [TV] - Grabbed"; + _plexProvider.Notify(header, message); + } + + protected override void OnDownload(string message, Series series) + { + const string header = "NzbDrone [TV] - Downloaded"; + _plexProvider.Notify(header, message); + UpdateIfEnabled(); + } + + protected override void AfterRename( Series series) + { + UpdateIfEnabled(); + } + + private void UpdateIfEnabled() + { + if (_configService.PlexUpdateLibrary) + { + _plexProvider.UpdateLibrary(); + } + } + } +} diff --git a/NzbDrone.Core/Providers/PlexProvider.cs b/NzbDrone.Core/ExternalNotification/PlexProvider.cs similarity index 96% rename from NzbDrone.Core/Providers/PlexProvider.cs rename to NzbDrone.Core/ExternalNotification/PlexProvider.cs index 41cef0c52..c2f21a1ce 100644 --- a/NzbDrone.Core/Providers/PlexProvider.cs +++ b/NzbDrone.Core/ExternalNotification/PlexProvider.cs @@ -1,15 +1,12 @@ using System; using System.Collections.Generic; -using System.IO; using System.Linq; -using System.Text; using System.Xml.Linq; using NLog; using NzbDrone.Common; using NzbDrone.Core.Configuration; -using NzbDrone.Core.Providers.Core; -namespace NzbDrone.Core.Providers +namespace NzbDrone.Core.ExternalNotification { public class PlexProvider { @@ -51,6 +48,7 @@ namespace NzbDrone.Core.Providers try { + logger.Trace("Sending Update Request to Plex Server"); var sections = GetSectionKeys(host); sections.ForEach(s => UpdateSection(host, s)); } diff --git a/NzbDrone.Core/ExternalNotification/Prowl.cs b/NzbDrone.Core/ExternalNotification/Prowl.cs new file mode 100644 index 000000000..df4d5f261 --- /dev/null +++ b/NzbDrone.Core/ExternalNotification/Prowl.cs @@ -0,0 +1,48 @@ +using System.Linq; +using System; +using NLog; +using NzbDrone.Core.Configuration; +using NzbDrone.Core.Providers; +using NzbDrone.Core.Tv; +using Prowlin; + +namespace NzbDrone.Core.ExternalNotification +{ + public class Prowl : ExternalNotificationBase + { + private readonly IConfigService _configService; + private readonly ProwlProvider _prowlProvider; + + public Prowl(IConfigService configService, IExternalNotificationRepository repository, ProwlProvider prowlProvider, Logger logger) + : base(repository, logger) + { + _configService = configService; + _prowlProvider = prowlProvider; + } + + public override string Name + { + get { return "Prowl"; } + } + + protected override void OnGrab(string message) + { + const string title = "Episode Grabbed"; + + var apiKeys = _configService.ProwlApiKeys; + var priority = _configService.ProwlPriority; + + _prowlProvider.SendNotification(title, message, apiKeys, (NotificationPriority)priority); + } + + protected override void OnDownload(string message, Series series) + { + const string title = "Episode Downloaded"; + + var apiKeys = _configService.ProwlApiKeys; + var priority = _configService.ProwlPriority; + + _prowlProvider.SendNotification(title, message, apiKeys, (NotificationPriority)priority); + } + } +} diff --git a/NzbDrone.Core/ExternalNotification/Smtp.cs b/NzbDrone.Core/ExternalNotification/Smtp.cs new file mode 100644 index 000000000..b9e511b01 --- /dev/null +++ b/NzbDrone.Core/ExternalNotification/Smtp.cs @@ -0,0 +1,39 @@ +using System.Linq; +using System; +using NLog; +using NzbDrone.Core.Providers; +using NzbDrone.Core.Tv; + +namespace NzbDrone.Core.ExternalNotification +{ + public class Smtp : ExternalNotificationBase + { + private readonly SmtpProvider _smtpProvider; + + public Smtp(IExternalNotificationRepository repository, SmtpProvider smtpProvider, Logger logger) + : base(repository, logger) + { + _smtpProvider = smtpProvider; + } + + public override string Name + { + get { return "SMTP"; } + } + + protected override void OnGrab(string message) + { + const string subject = "NzbDrone [TV] - Grabbed"; + var body = String.Format("{0} sent to SABnzbd queue.", message); + _smtpProvider.SendEmail(subject, body); + } + + protected override void OnDownload(string message, Series series) + { + const string subject = "NzbDrone [TV] - Downloaded"; + var body = String.Format("{0} Downloaded and sorted.", message); + + _smtpProvider.SendEmail(subject, body); + } + } +} diff --git a/NzbDrone.Core/ExternalNotification/Twitter.cs b/NzbDrone.Core/ExternalNotification/Twitter.cs new file mode 100644 index 000000000..73a6c335b --- /dev/null +++ b/NzbDrone.Core/ExternalNotification/Twitter.cs @@ -0,0 +1,35 @@ +using System.Linq; +using NLog; +using NzbDrone.Core.Configuration; +using NzbDrone.Core.Providers; +using NzbDrone.Core.Tv; + +namespace NzbDrone.Core.ExternalNotification +{ + public class Twitter : ExternalNotificationBase + { + private readonly TwitterProvider _twitterProvider; + + public Twitter(IExternalNotificationRepository repository, TwitterProvider twitterProvider, Logger logger) + : base(repository, logger) + { + _twitterProvider = twitterProvider; + } + + public override string Name + { + get { return "Twitter"; } + } + + protected override void OnGrab(string message) + { + _twitterProvider.SendTweet("Download Started: " + message); + } + + protected override void OnDownload(string message, Series series) + { + _twitterProvider.SendTweet("Download Completed: " + message); + } + + } +} diff --git a/NzbDrone.Core/ExternalNotification/Xbmc.cs b/NzbDrone.Core/ExternalNotification/Xbmc.cs new file mode 100644 index 000000000..7cc22ae27 --- /dev/null +++ b/NzbDrone.Core/ExternalNotification/Xbmc.cs @@ -0,0 +1,59 @@ +using System.Linq; +using NLog; +using NzbDrone.Core.Configuration; +using NzbDrone.Core.Providers; +using NzbDrone.Core.Tv; + +namespace NzbDrone.Core.ExternalNotification +{ + public class Xbmc : ExternalNotificationBase + { + private readonly IConfigService _configService; + private readonly XbmcProvider _xbmcProvider; + + public Xbmc(IConfigService configService, IExternalNotificationRepository repository, XbmcProvider xbmcProvider, Logger logger) + : base(repository, logger) + { + _configService = configService; + _xbmcProvider = xbmcProvider; + } + + public override string Name + { + get { return "XBMC"; } + } + + protected override void OnGrab(string message) + { + const string header = "NzbDrone [TV] - Grabbed"; + + _xbmcProvider.Notify(header, message); + } + + protected override void OnDownload(string message, Series series) + { + const string header = "NzbDrone [TV] - Downloaded"; + + _xbmcProvider.Notify(header, message); + UpdateAndClean(series); + } + + protected override void AfterRename(Series series) + { + UpdateAndClean(series); + } + + private void UpdateAndClean(Series series) + { + if (_configService.XbmcUpdateLibrary) + { + _xbmcProvider.Update(series); + } + + if (_configService.XbmcCleanLibrary) + { + _xbmcProvider.Clean(); + } + } + } +} diff --git a/NzbDrone.Core/Jobs/RenameSeasonJob.cs b/NzbDrone.Core/Jobs/RenameSeasonJob.cs index f594ba55f..387fc1b11 100644 --- a/NzbDrone.Core/Jobs/RenameSeasonJob.cs +++ b/NzbDrone.Core/Jobs/RenameSeasonJob.cs @@ -2,6 +2,9 @@ using System.Collections.Generic; using System.Linq; using System; using NLog; +using NzbDrone.Common.Eventing; +using NzbDrone.Core.Download; +using NzbDrone.Core.ExternalNotification; using NzbDrone.Core.Tv; using NzbDrone.Core.Model.Notification; using NzbDrone.Core.Providers; @@ -12,23 +15,22 @@ namespace NzbDrone.Core.Jobs { private readonly MediaFileProvider _mediaFileProvider; private readonly DiskScanProvider _diskScanProvider; - private readonly ExternalNotificationProvider _externalNotificationProvider; private readonly ISeriesService _seriesService; private readonly MetadataProvider _metadataProvider; private readonly ISeriesRepository _seriesRepository; + private readonly IEventAggregator _eventAggregator; private static readonly Logger logger = LogManager.GetCurrentClassLogger(); - public RenameSeasonJob(MediaFileProvider mediaFileProvider, DiskScanProvider diskScanProvider, - ExternalNotificationProvider externalNotificationProvider, ISeriesService seriesService, - MetadataProvider metadataProvider, ISeriesRepository seriesRepository) + public RenameSeasonJob(MediaFileProvider mediaFileProvider, DiskScanProvider diskScanProvider, ISeriesService seriesService, + MetadataProvider metadataProvider, ISeriesRepository seriesRepository, IEventAggregator eventAggregator) { _mediaFileProvider = mediaFileProvider; _diskScanProvider = diskScanProvider; - _externalNotificationProvider = externalNotificationProvider; _seriesService = seriesService; _metadataProvider = metadataProvider; _seriesRepository = seriesRepository; + _eventAggregator = eventAggregator; } public string Name @@ -100,7 +102,8 @@ namespace NzbDrone.Core.Jobs //Start AfterRename var message = String.Format("Renamed: Series {0}, Season: {1}", series.Title, options.SeasonNumber); - _externalNotificationProvider.AfterRename(message, series); + + _eventAggregator.Publish(new SeriesRenamedEvent(series)); notification.CurrentMessage = String.Format("Rename completed for {0} Season {1}", series.Title, options.SeasonNumber); } diff --git a/NzbDrone.Core/Jobs/RenameSeriesJob.cs b/NzbDrone.Core/Jobs/RenameSeriesJob.cs index 22e13577d..4800c6d62 100644 --- a/NzbDrone.Core/Jobs/RenameSeriesJob.cs +++ b/NzbDrone.Core/Jobs/RenameSeriesJob.cs @@ -2,11 +2,11 @@ using System.Collections.Generic; using System.Linq; using System; using NLog; -using NzbDrone.Core.Datastore; +using NzbDrone.Common.Eventing; +using NzbDrone.Core.Download; using NzbDrone.Core.Tv; using NzbDrone.Core.Model.Notification; using NzbDrone.Core.Providers; -using NzbDrone.Core.Repository; namespace NzbDrone.Core.Jobs { @@ -14,23 +14,20 @@ namespace NzbDrone.Core.Jobs { private readonly MediaFileProvider _mediaFileProvider; private readonly DiskScanProvider _diskScanProvider; - private readonly ExternalNotificationProvider _externalNotificationProvider; - private readonly ISeriesService _seriesService; private readonly MetadataProvider _metadataProvider; private readonly ISeriesRepository _seriesRepository; + private readonly IEventAggregator _eventAggregator; private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); public RenameSeriesJob(MediaFileProvider mediaFileProvider, DiskScanProvider diskScanProvider, - ExternalNotificationProvider externalNotificationProvider, ISeriesService seriesService, - MetadataProvider metadataProvider,ISeriesRepository seriesRepository) + MetadataProvider metadataProvider,ISeriesRepository seriesRepository,IEventAggregator eventAggregator) { _mediaFileProvider = mediaFileProvider; _diskScanProvider = diskScanProvider; - _externalNotificationProvider = externalNotificationProvider; - _seriesService = seriesService; _metadataProvider = metadataProvider; _seriesRepository = seriesRepository; + _eventAggregator = eventAggregator; } public string Name @@ -99,8 +96,7 @@ namespace NzbDrone.Core.Jobs //Start AfterRename - var message = String.Format("Renamed: Series {0}", series.Title); - _externalNotificationProvider.AfterRename(message, series); + _eventAggregator.Publish(new SeriesRenamedEvent(series)); notification.CurrentMessage = String.Format("Rename completed for {0}", series.Title); } diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index 113b97f2d..f25fec41a 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -260,7 +260,10 @@ + + + @@ -464,31 +467,28 @@ Code - + Code - + Code - + Code - + Code - + Code - + Code - + Code - - Code - - + Code @@ -521,7 +521,7 @@ Code - + Code @@ -578,7 +578,7 @@ Code - + diff --git a/NzbDrone.Core/Providers/DiskScanProvider.cs b/NzbDrone.Core/Providers/DiskScanProvider.cs index a80f2d721..6d2dce004 100644 --- a/NzbDrone.Core/Providers/DiskScanProvider.cs +++ b/NzbDrone.Core/Providers/DiskScanProvider.cs @@ -4,7 +4,10 @@ using System.IO; using System.Linq; using NLog; using NzbDrone.Common; +using NzbDrone.Common.Eventing; using NzbDrone.Core.Configuration; +using NzbDrone.Core.Download; +using NzbDrone.Core.ExternalNotification; using NzbDrone.Core.Tv; using NzbDrone.Core.Model; @@ -17,27 +20,26 @@ namespace NzbDrone.Core.Providers private readonly DiskProvider _diskProvider; private readonly IEpisodeService _episodeService; private readonly MediaFileProvider _mediaFileProvider; - private readonly ExternalNotificationProvider _externalNotificationProvider; private readonly SignalRProvider _signalRProvider; private readonly IConfigService _configService; private readonly RecycleBinProvider _recycleBinProvider; private readonly MediaInfoProvider _mediaInfoProvider; private readonly ISeriesRepository _seriesRepository; + private readonly IEventAggregator _eventAggregator; public DiskScanProvider(DiskProvider diskProvider, IEpisodeService episodeService, MediaFileProvider mediaFileProvider, - ExternalNotificationProvider externalNotificationProvider, SignalRProvider signalRProvider, IConfigService configService, - RecycleBinProvider recycleBinProvider, MediaInfoProvider mediaInfoProvider, ISeriesRepository seriesRepository) + RecycleBinProvider recycleBinProvider, MediaInfoProvider mediaInfoProvider, ISeriesRepository seriesRepository, IEventAggregator eventAggregator) { _diskProvider = diskProvider; _episodeService = episodeService; _mediaFileProvider = mediaFileProvider; - _externalNotificationProvider = externalNotificationProvider; _signalRProvider = signalRProvider; _configService = configService; _recycleBinProvider = recycleBinProvider; _mediaInfoProvider = mediaInfoProvider; _seriesRepository = seriesRepository; + _eventAggregator = eventAggregator; } public DiskScanProvider() @@ -228,11 +230,10 @@ namespace NzbDrone.Core.Providers parseResult.Quality = new QualityModel { Quality = episodeFile.Quality, Proper = episodeFile.Proper }; parseResult.Episodes = episodes; - var message = parseResult.GetDownloadTitle(); if (newDownload) { - _externalNotificationProvider.OnDownload(message, series); + _eventAggregator.Publish(new EpisodeDownloadedEvent(parseResult)); foreach (var episode in episodes) _signalRProvider.UpdateEpisodeStatus(episode.OID, EpisodeStatusType.Ready, parseResult.Quality); diff --git a/NzbDrone.Core/Providers/ExternalNotification/ExternalNotificationBase.cs b/NzbDrone.Core/Providers/ExternalNotification/ExternalNotificationBase.cs deleted file mode 100644 index 687a398d4..000000000 --- a/NzbDrone.Core/Providers/ExternalNotification/ExternalNotificationBase.cs +++ /dev/null @@ -1,43 +0,0 @@ -using NLog; -using NzbDrone.Core.Configuration; -using NzbDrone.Core.Tv; - -namespace NzbDrone.Core.Providers.ExternalNotification -{ - public abstract class ExternalNotificationBase - { - protected readonly Logger _logger; - protected readonly IConfigService _configService; - - protected ExternalNotificationBase(IConfigService configService) - { - _configService = configService; - _logger = LogManager.GetLogger(GetType().ToString()); - } - - /// - /// Gets the name for the notification provider - /// - public abstract string Name { get; } - - /// - /// Performs the on grab action - /// - /// The message to send to the receiver - public abstract void OnGrab(string message); - - /// - /// Performs the on download action - /// - /// The message to send to the receiver - /// The Series for the new download - public abstract void OnDownload(string message, Series series); - - /// - /// Performs the after rename action, this will be handled after all renaming for episode/season/series - /// - /// The message to send to the receiver - /// The Series for the new download - public abstract void AfterRename(string message, Series series); - } -} diff --git a/NzbDrone.Core/Providers/ExternalNotification/Growl.cs b/NzbDrone.Core/Providers/ExternalNotification/Growl.cs deleted file mode 100644 index c69105553..000000000 --- a/NzbDrone.Core/Providers/ExternalNotification/Growl.cs +++ /dev/null @@ -1,77 +0,0 @@ -using System; -using NLog; -using NzbDrone.Core.Configuration; -using NzbDrone.Core.Tv; -using NzbDrone.Core.Providers.Core; -using NzbDrone.Core.Repository; - -namespace NzbDrone.Core.Providers.ExternalNotification -{ - public class Growl : ExternalNotificationBase - { - private readonly GrowlProvider _growlProvider; - - public Growl(IConfigService configService, GrowlProvider growlProvider) - : base(configService) - { - _growlProvider = growlProvider; - } - - public override string Name - { - get { return "Growl"; } - } - - public override void OnGrab(string message) - { - try - { - if(_configService.GrowlNotifyOnGrab) - { - _logger.Trace("Sending Notification to Growl"); - const string title = "Episode Grabbed"; - - var growlHost = _configService.GrowlHost.Split(':'); - var host = growlHost[0]; - var port = Convert.ToInt32(growlHost[1]); - - _growlProvider.SendNotification(title, message, "GRAB", host, port, _configService.GrowlPassword); - } - } - - catch (Exception ex) - { - _logger.WarnException(ex.Message, ex); - } - } - - public override void OnDownload(string message, Series series) - { - try - { - if (_configService.GrowlNotifyOnDownload) - { - _logger.Trace("Sending Notification to Growl"); - const string title = "Episode Downloaded"; - - var growlHost = _configService.GrowlHost.Split(':'); - var host = growlHost[0]; - var port = Convert.ToInt32(growlHost[1]); - - _growlProvider.SendNotification(title, message, "DOWNLOAD", host, port, _configService.GrowlPassword); - } - } - - catch (Exception ex) - { - _logger.WarnException(ex.Message, ex); - } - } - - - public override void AfterRename(string message, Series series) - { - - } - } -} diff --git a/NzbDrone.Core/Providers/ExternalNotification/Plex.cs b/NzbDrone.Core/Providers/ExternalNotification/Plex.cs deleted file mode 100644 index 63e9dc713..000000000 --- a/NzbDrone.Core/Providers/ExternalNotification/Plex.cs +++ /dev/null @@ -1,63 +0,0 @@ -using System; -using NLog; -using NzbDrone.Core.Configuration; -using NzbDrone.Core.Tv; -using NzbDrone.Core.Providers.Core; -using NzbDrone.Core.Repository; - -namespace NzbDrone.Core.Providers.ExternalNotification -{ - public class Plex : ExternalNotificationBase - { - private readonly PlexProvider _plexProvider; - - public Plex(IConfigService configService, PlexProvider plexProvider) - : base(configService) - { - _plexProvider = plexProvider; - } - - public override string Name - { - get { return "Plex"; } - } - - public override void OnGrab(string message) - { - const string header = "NzbDrone [TV] - Grabbed"; - - if (_configService.PlexNotifyOnGrab) - { - _logger.Trace("Sending Notification to Plex Clients"); - _plexProvider.Notify(header, message); - } - } - - public override void OnDownload(string message, Series series) - { - const string header = "NzbDrone [TV] - Downloaded"; - - if (_configService.PlexNotifyOnDownload) - { - _logger.Trace("Sending Notification to Plex Clients"); - _plexProvider.Notify(header, message); - } - - UpdateIfEnabled(); - } - - public override void AfterRename(string message, Series series) - { - UpdateIfEnabled(); - } - - private void UpdateIfEnabled() - { - if (_configService.PlexUpdateLibrary) - { - _logger.Trace("Sending Update Request to Plex Server"); - _plexProvider.UpdateLibrary(); - } - } - } -} diff --git a/NzbDrone.Core/Providers/ExternalNotification/Prowl.cs b/NzbDrone.Core/Providers/ExternalNotification/Prowl.cs deleted file mode 100644 index 8fc5963e1..000000000 --- a/NzbDrone.Core/Providers/ExternalNotification/Prowl.cs +++ /dev/null @@ -1,75 +0,0 @@ -using System; -using NLog; -using NzbDrone.Core.Configuration; -using NzbDrone.Core.Tv; -using NzbDrone.Core.Providers.Core; -using NzbDrone.Core.Repository; -using Prowlin; - -namespace NzbDrone.Core.Providers.ExternalNotification -{ - public class Prowl : ExternalNotificationBase - { - private readonly ProwlProvider _prowlProvider; - - public Prowl(IConfigService configService, ProwlProvider prowlProvider) - : base(configService) - { - _prowlProvider = prowlProvider; - } - - public override string Name - { - get { return "Prowl"; } - } - - public override void OnGrab(string message) - { - try - { - if(_configService.GrowlNotifyOnGrab) - { - _logger.Trace("Sending Notification to Prowl"); - const string title = "Episode Grabbed"; - - var apiKeys = _configService.ProwlApiKeys; - var priority = _configService.ProwlPriority; - - _prowlProvider.SendNotification(title, message, apiKeys, (NotificationPriority)priority); - } - } - - catch (Exception ex) - { - _logger.WarnException(ex.Message, ex); - } - } - - public override void OnDownload(string message, Series series) - { - try - { - if (_configService.GrowlNotifyOnDownload) - { - _logger.Trace("Sending Notification to Prowl"); - const string title = "Episode Downloaded"; - - var apiKeys = _configService.ProwlApiKeys; - var priority = _configService.ProwlPriority; - - _prowlProvider.SendNotification(title, message, apiKeys, (NotificationPriority)priority); - } - } - - catch (Exception ex) - { - _logger.WarnException(ex.Message, ex); - } - } - - public override void AfterRename(string message, Series series) - { - - } - } -} diff --git a/NzbDrone.Core/Providers/ExternalNotification/Smtp.cs b/NzbDrone.Core/Providers/ExternalNotification/Smtp.cs deleted file mode 100644 index bab398ac7..000000000 --- a/NzbDrone.Core/Providers/ExternalNotification/Smtp.cs +++ /dev/null @@ -1,53 +0,0 @@ -using System; -using NzbDrone.Core.Configuration; -using NzbDrone.Core.Tv; - -namespace NzbDrone.Core.Providers.ExternalNotification -{ - public class Smtp: ExternalNotificationBase - { - private readonly SmtpProvider _smtpProvider; - - public Smtp(IConfigService configService, SmtpProvider smtpProvider) - : base(configService) - { - _smtpProvider = smtpProvider; - } - - public override string Name - { - get { return "SMTP"; } - } - - public override void OnGrab(string message) - { - const string subject = "NzbDrone [TV] - Grabbed"; - var body = String.Format("{0} sent to SABnzbd queue.", message); - - if (_configService.SmtpNotifyOnGrab) - { - _logger.Trace("Sending SMTP Notification"); - _smtpProvider.SendEmail(subject, body); - } - } - - public override void OnDownload(string message, Series series) - { - const string subject = "NzbDrone [TV] - Downloaded"; - var body = String.Format("{0} Downloaded and sorted.", message); - - if (_configService.SmtpNotifyOnDownload) - { - _logger.Trace("Sending SMTP Notification"); - _smtpProvider.SendEmail(subject, body); - } - } - - - - public override void AfterRename(string message, Series series) - { - - } - } -} diff --git a/NzbDrone.Core/Providers/ExternalNotification/Twitter.cs b/NzbDrone.Core/Providers/ExternalNotification/Twitter.cs deleted file mode 100644 index 8b9070c0f..000000000 --- a/NzbDrone.Core/Providers/ExternalNotification/Twitter.cs +++ /dev/null @@ -1,47 +0,0 @@ -using System; -using NzbDrone.Core.Configuration; -using NzbDrone.Core.Tv; -using NzbDrone.Core.Providers.Core; -using NzbDrone.Core.Repository; - -namespace NzbDrone.Core.Providers.ExternalNotification -{ - public class Twitter : ExternalNotificationBase - { - private readonly TwitterProvider _twitterProvider; - - public Twitter(IConfigService configService, TwitterProvider twitterProvider) - : base(configService) - { - _twitterProvider = twitterProvider; - } - - public override string Name - { - get { return "Twitter"; } - } - - public override void OnGrab(string message) - { - if (_configService.TwitterNotifyOnGrab) - { - _logger.Trace("Sending Notification to Twitter (On Grab)"); - _twitterProvider.SendTweet("Download Started: " + message); - } - } - - public override void OnDownload(string message, Series series) - { - if (_configService.TwitterNotifyOnDownload) - { - _logger.Trace("Sending Notification to Twitter (On Grab)"); - _twitterProvider.SendTweet("Download Completed: " + message); - } - } - - public override void AfterRename(string message, Series series) - { - - } - } -} diff --git a/NzbDrone.Core/Providers/ExternalNotification/Xbmc.cs b/NzbDrone.Core/Providers/ExternalNotification/Xbmc.cs deleted file mode 100644 index 309621b9c..000000000 --- a/NzbDrone.Core/Providers/ExternalNotification/Xbmc.cs +++ /dev/null @@ -1,68 +0,0 @@ -using System; -using NzbDrone.Core.Configuration; -using NzbDrone.Core.Tv; -using NzbDrone.Core.Providers.Core; -using NzbDrone.Core.Repository; - -namespace NzbDrone.Core.Providers.ExternalNotification -{ - public class Xbmc : ExternalNotificationBase - { - private readonly XbmcProvider _xbmcProvider; - - public Xbmc(IConfigService configService, XbmcProvider xbmcProvider) - : base(configService) - { - _xbmcProvider = xbmcProvider; - } - - public override string Name - { - get { return "XBMC"; } - } - - public override void OnGrab(string message) - { - const string header = "NzbDrone [TV] - Grabbed"; - - if (_configService.XbmcNotifyOnGrab) - { - _logger.Trace("Sending Notification to XBMC"); - _xbmcProvider.Notify(header, message); - } - } - - public override void OnDownload(string message, Series series) - { - const string header = "NzbDrone [TV] - Downloaded"; - - if (_configService.XbmcNotifyOnDownload) - { - _logger.Trace("Sending Notification to XBMC"); - _xbmcProvider.Notify(header, message); - } - - UpdateAndClean(series); - } - - public override void AfterRename(string message, Series series) - { - UpdateAndClean(series); - } - - private void UpdateAndClean(Series series) - { - if (_configService.XbmcUpdateLibrary) - { - _logger.Trace("Sending Update Request to XBMC"); - _xbmcProvider.Update(series); - } - - if (_configService.XbmcCleanLibrary) - { - _logger.Trace("Sending Clean DB Request to XBMC"); - _xbmcProvider.Clean(); - } - } - } -} diff --git a/NzbDrone.Core/Providers/ExternalNotificationProvider.cs b/NzbDrone.Core/Providers/ExternalNotificationProvider.cs deleted file mode 100644 index 1f8ed07aa..000000000 --- a/NzbDrone.Core/Providers/ExternalNotificationProvider.cs +++ /dev/null @@ -1,114 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using NLog; -using NzbDrone.Common.Eventing; -using NzbDrone.Core.Download; -using NzbDrone.Core.Tv; -using NzbDrone.Core.Providers.ExternalNotification; -using NzbDrone.Core.Repository; -using PetaPoco; - -namespace NzbDrone.Core.Providers -{ - public class ExternalNotificationProvider : IHandle - { - private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); - private readonly IDatabase _database; - - private IList _notifiers; - - public ExternalNotificationProvider(IDatabase database, IEnumerable notifiers) - { - _database = database; - _notifiers = notifiers.ToList(); - - InitializeNotifiers(_notifiers); - } - - public ExternalNotificationProvider() - { - - } - - public virtual List All() - { - return _database.Fetch(); - } - - public virtual void SaveSettings(ExternalNotificationDefinition settings) - { - if (settings.Id == 0) - { - Logger.Debug("Adding External Notification definition for {0}", settings.Name); - _database.Insert(settings); - } - - else - { - Logger.Debug("Updating External Notification definition for {0}", settings.Name); - _database.Update(settings); - } - } - - public virtual ExternalNotificationDefinition GetSettings(Type type) - { - return _database.SingleOrDefault("WHERE ExternalNotificationProviderType = @0", type.ToString()); - } - - public virtual IList GetEnabledExternalNotifiers() - { - var all = All(); - return _notifiers.Where(i => all.Exists(c => c.ExternalNotificationProviderType == i.GetType().ToString() && c.Enable)).ToList(); - } - - private void InitializeNotifiers(IList notifiers) - { - Logger.Debug("Initializing notifiers. Count {0}", notifiers.Count); - - _notifiers = notifiers; - - var currentNotifiers = All(); - - foreach (var notificationProvider in notifiers) - { - ExternalNotificationBase externalNotificationProviderLocal = notificationProvider; - if (!currentNotifiers.Exists(c => c.ExternalNotificationProviderType == externalNotificationProviderLocal.GetType().ToString())) - { - var settings = new ExternalNotificationDefinition - { - Enable = false, - ExternalNotificationProviderType = externalNotificationProviderLocal.GetType().ToString(), - Name = externalNotificationProviderLocal.Name - }; - - SaveSettings(settings); - } - } - } - - public virtual void OnDownload(string message, Series series) - { - foreach (var notifier in _notifiers.Where(i => GetSettings(i.GetType()).Enable)) - { - notifier.OnDownload(message, series); - } - } - - public virtual void AfterRename(string message, Series series) - { - foreach (var notifier in _notifiers.Where(i => GetSettings(i.GetType()).Enable)) - { - notifier.AfterRename(message, series); - } - } - - public void Handle(EpisodeGrabbedEvent message) - { - foreach (var notifier in _notifiers.Where(i => GetSettings(i.GetType()).Enable)) - { - notifier.OnGrab(message.ParseResult.GetDownloadTitle()); - } - } - } -} \ No newline at end of file diff --git a/NzbDrone.Core/Providers/MetadataProvider.cs b/NzbDrone.Core/Providers/MetadataProvider.cs index f68c56b35..c95ba6f15 100644 --- a/NzbDrone.Core/Providers/MetadataProvider.cs +++ b/NzbDrone.Core/Providers/MetadataProvider.cs @@ -5,7 +5,6 @@ using NLog; using NzbDrone.Core.Datastore; using NzbDrone.Core.Tv; using NzbDrone.Core.Model; -using NzbDrone.Core.Providers.ExternalNotification; using NzbDrone.Core.Providers.Metadata; using NzbDrone.Core.Repository; using PetaPoco; diff --git a/NzbDrone.Core/Repository/ExternalNotificationDefinition.cs b/NzbDrone.Core/Repository/ExternalNotificationDefinition.cs deleted file mode 100644 index 64a3abba1..000000000 --- a/NzbDrone.Core/Repository/ExternalNotificationDefinition.cs +++ /dev/null @@ -1,17 +0,0 @@ -using PetaPoco; - -namespace NzbDrone.Core.Repository -{ - [TableName("ExternalNotificationDefinitions")] - [PrimaryKey("Id", autoIncrement = true)] - public class ExternalNotificationDefinition - { - public int Id { get; set; } - - public bool Enable { get; set; } - - public string ExternalNotificationProviderType { get; set; } - - public string Name { get; set; } - } -} \ No newline at end of file