From e96e1b10db5b4a106fb07b881d6116b29d3e6595 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Thu, 25 Jul 2013 00:24:09 -0700 Subject: [PATCH] Added pushover notifications --- .../Pushover/InvalidResponseException.cs | 15 ++++++ .../Notifications/Pushover/Pushover.cs | 44 ++++++++++++++++ .../Pushover/PushoverPriority.cs | 10 ++++ .../Notifications/Pushover/PushoverService.cs | 52 +++++++++++++++++++ .../Pushover/PushoverSettings.cs | 22 ++++++++ .../Pushover/TestPushoverCommand.cs | 10 ++++ NzbDrone.Core/NzbDrone.Core.csproj | 6 +++ 7 files changed, 159 insertions(+) create mode 100644 NzbDrone.Core/Notifications/Pushover/InvalidResponseException.cs create mode 100644 NzbDrone.Core/Notifications/Pushover/Pushover.cs create mode 100644 NzbDrone.Core/Notifications/Pushover/PushoverPriority.cs create mode 100644 NzbDrone.Core/Notifications/Pushover/PushoverService.cs create mode 100644 NzbDrone.Core/Notifications/Pushover/PushoverSettings.cs create mode 100644 NzbDrone.Core/Notifications/Pushover/TestPushoverCommand.cs diff --git a/NzbDrone.Core/Notifications/Pushover/InvalidResponseException.cs b/NzbDrone.Core/Notifications/Pushover/InvalidResponseException.cs new file mode 100644 index 000000000..7e6fc1f4a --- /dev/null +++ b/NzbDrone.Core/Notifications/Pushover/InvalidResponseException.cs @@ -0,0 +1,15 @@ +using System; + +namespace NzbDrone.Core.Notifications.Pushover +{ + public class InvalidResponseException : Exception + { + public InvalidResponseException() + { + } + + public InvalidResponseException(string message) : base(message) + { + } + } +} diff --git a/NzbDrone.Core/Notifications/Pushover/Pushover.cs b/NzbDrone.Core/Notifications/Pushover/Pushover.cs new file mode 100644 index 000000000..1dac1ff2a --- /dev/null +++ b/NzbDrone.Core/Notifications/Pushover/Pushover.cs @@ -0,0 +1,44 @@ +using NzbDrone.Core.Notifications.Prowl; +using NzbDrone.Core.Tv; +using Prowlin; + +namespace NzbDrone.Core.Notifications.Pushover +{ + public class Pushover : NotificationBase + { + private readonly IPushoverService _pushoverService; + + public Pushover(IPushoverService pushoverService) + { + _pushoverService = pushoverService; + } + + public override string Name + { + get { return "Pushover"; } + } + + public override string ImplementationName + { + get { return "Pushover"; } + } + + public override void OnGrab(string message) + { + const string title = "Episode Grabbed"; + + _pushoverService.SendNotification(title, message, Settings.UserKey, (PushoverPriority)Settings.Priority); + } + + public override void OnDownload(string message, Series series) + { + const string title = "Episode Downloaded"; + + _pushoverService.SendNotification(title, message, Settings.UserKey, (PushoverPriority)Settings.Priority); + } + + public override void AfterRename(Series series) + { + } + } +} diff --git a/NzbDrone.Core/Notifications/Pushover/PushoverPriority.cs b/NzbDrone.Core/Notifications/Pushover/PushoverPriority.cs new file mode 100644 index 000000000..2e159b200 --- /dev/null +++ b/NzbDrone.Core/Notifications/Pushover/PushoverPriority.cs @@ -0,0 +1,10 @@ +namespace NzbDrone.Core.Notifications.Pushover +{ + public enum PushoverPriority + { + Quiet = -1, + Normal = 0, + High = 1, + Emergency = 2 + } +} diff --git a/NzbDrone.Core/Notifications/Pushover/PushoverService.cs b/NzbDrone.Core/Notifications/Pushover/PushoverService.cs new file mode 100644 index 000000000..c0d944348 --- /dev/null +++ b/NzbDrone.Core/Notifications/Pushover/PushoverService.cs @@ -0,0 +1,52 @@ +using System; +using System.Net; +using NLog; +using NzbDrone.Common.Messaging; +using RestSharp; + +namespace NzbDrone.Core.Notifications.Pushover +{ + public interface IPushoverService + { + void SendNotification(string title, string message, string userKey, PushoverPriority priority); + } + + public class PushoverService : IPushoverService, IExecute + { + private readonly Logger _logger; + + private const string TOKEN = "yz9b4U215iR4vrKFRfjNXP24NMNPKJ"; + private const string URL = "https://api.pushover.net/1/messages.json"; + + public PushoverService(Logger logger) + { + _logger = logger; + } + + public void SendNotification(string title, string message, string userKey, PushoverPriority priority) + { + var client = new RestClient(URL); + var request = new RestRequest(Method.POST); + request.AddParameter("token", TOKEN); + request.AddParameter("user", userKey); + request.AddParameter("title", title); + request.AddParameter("message", message); + request.AddParameter("priority", (int)priority); + + var response = client.Execute(request); + + if (response.StatusCode != HttpStatusCode.OK) + { + throw new InvalidResponseException(response.Content); + } + } + + public void Execute(TestPushoverCommand message) + { + const string title = "Test Notification"; + const string body = "This is a test message from NzbDrone"; + + SendNotification(title, body, message.UserKey, (PushoverPriority)message.Priority); + } + } +} diff --git a/NzbDrone.Core/Notifications/Pushover/PushoverSettings.cs b/NzbDrone.Core/Notifications/Pushover/PushoverSettings.cs new file mode 100644 index 000000000..20ab99339 --- /dev/null +++ b/NzbDrone.Core/Notifications/Pushover/PushoverSettings.cs @@ -0,0 +1,22 @@ +using System; +using NzbDrone.Core.Annotations; + +namespace NzbDrone.Core.Notifications.Pushover +{ + public class PushoverSettings : INotifcationSettings + { + [FieldDefinition(0, Label = "User Key")] + public String UserKey { get; set; } + + [FieldDefinition(1, Label = "Priority", Type = FieldType.Select, SelectOptions = typeof(PushoverPriority) )] + public Int32 Priority { get; set; } + + public bool IsValid + { + get + { + return !string.IsNullOrWhiteSpace(UserKey) && Priority != null & Priority >= -1 && Priority <= 2; + } + } + } +} diff --git a/NzbDrone.Core/Notifications/Pushover/TestPushoverCommand.cs b/NzbDrone.Core/Notifications/Pushover/TestPushoverCommand.cs new file mode 100644 index 000000000..31bc034f9 --- /dev/null +++ b/NzbDrone.Core/Notifications/Pushover/TestPushoverCommand.cs @@ -0,0 +1,10 @@ +using NzbDrone.Common.Messaging; + +namespace NzbDrone.Core.Notifications.Pushover +{ + public class TestPushoverCommand : ICommand + { + public string UserKey { get; set; } + public int Priority { get; set; } + } +} diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index 1c5d48159..e3c37d666 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -356,6 +356,12 @@ + + + + + +