mirror of
https://github.com/Prowlarr/Prowlarr.git
synced 2025-10-03 01:01:34 +02:00
Added NMA support, also added generic HttpStatusCode handling
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
using NzbDrone.Core.Tv;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.NotifyMyAndroid
|
||||
{
|
||||
public class NotifyMyAndroid : NotificationBase<NotifyMyAndroidSettings>
|
||||
{
|
||||
private readonly INotifyMyAndroidProxy _notifyMyAndroidProxy;
|
||||
|
||||
public NotifyMyAndroid(INotifyMyAndroidProxy notifyMyAndroidProxy)
|
||||
{
|
||||
_notifyMyAndroidProxy = notifyMyAndroidProxy;
|
||||
}
|
||||
|
||||
public override string Name
|
||||
{
|
||||
get { return "NotifyMyAndroid"; }
|
||||
}
|
||||
|
||||
public override string ImplementationName
|
||||
{
|
||||
get { return "NotifyMyAndroid"; }
|
||||
}
|
||||
|
||||
public override string Link
|
||||
{
|
||||
get { return "http://www.notifymyandroid.com/"; }
|
||||
}
|
||||
|
||||
public override void OnGrab(string message)
|
||||
{
|
||||
const string title = "Episode Grabbed";
|
||||
|
||||
_notifyMyAndroidProxy.SendNotification(title, message, Settings.ApiKey, (NotifyMyAndroidPriority)Settings.Priority);
|
||||
}
|
||||
|
||||
public override void OnDownload(string message, Series series)
|
||||
{
|
||||
const string title = "Episode Downloaded";
|
||||
|
||||
_notifyMyAndroidProxy.SendNotification(title, message, Settings.ApiKey, (NotifyMyAndroidPriority)Settings.Priority);
|
||||
}
|
||||
|
||||
public override void AfterRename(Series series)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
namespace NzbDrone.Core.Notifications.NotifyMyAndroid
|
||||
{
|
||||
public enum NotifyMyAndroidPriority
|
||||
{
|
||||
VeryLow = -2,
|
||||
Moderate = -1,
|
||||
Normal = 0,
|
||||
High = 1,
|
||||
Emergency = 2
|
||||
}
|
||||
}
|
@@ -0,0 +1,69 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Xml.Linq;
|
||||
using NLog.LayoutRenderers;
|
||||
using NzbDrone.Core.Exceptions;
|
||||
using NzbDrone.Core.Messaging;
|
||||
using NzbDrone.Core.Messaging.Commands;
|
||||
using RestSharp;
|
||||
using NzbDrone.Core.Rest;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.NotifyMyAndroid
|
||||
{
|
||||
public interface INotifyMyAndroidProxy
|
||||
{
|
||||
void SendNotification(string title, string message, string apiKye, NotifyMyAndroidPriority priority);
|
||||
}
|
||||
|
||||
public class NotifyMyAndroidProxy : INotifyMyAndroidProxy, IExecute<TestNotifyMyAndroidCommand>
|
||||
{
|
||||
private const string URL = "https://www.notifymyandroid.com/publicapi";
|
||||
|
||||
public void SendNotification(string title, string message, string apiKey, NotifyMyAndroidPriority priority)
|
||||
{
|
||||
var client = new RestClient(URL);
|
||||
var request = new RestRequest("notify", Method.POST);
|
||||
request.RequestFormat = DataFormat.Xml;
|
||||
request.AddParameter("apikey", apiKey);
|
||||
request.AddParameter("application", "NzbDrone");
|
||||
request.AddParameter("event", title);
|
||||
request.AddParameter("description", message);
|
||||
request.AddParameter("priority", (int)priority);
|
||||
|
||||
var response = client.ExecuteAndValidate(request);
|
||||
ValidateResponse(response);
|
||||
}
|
||||
|
||||
private void Verify(string apiKey)
|
||||
{
|
||||
var client = new RestClient(URL);
|
||||
var request = new RestRequest("verify", Method.GET);
|
||||
request.RequestFormat = DataFormat.Xml;
|
||||
request.AddParameter("apikey", apiKey, ParameterType.GetOrPost);
|
||||
|
||||
var response = client.ExecuteAndValidate(request);
|
||||
ValidateResponse(response);
|
||||
}
|
||||
|
||||
private void ValidateResponse(IRestResponse response)
|
||||
{
|
||||
var xDoc = XDocument.Parse(response.Content);
|
||||
var nma = xDoc.Descendants("nma").Single();
|
||||
var error = nma.Descendants("error").SingleOrDefault();
|
||||
|
||||
if (error != null)
|
||||
{
|
||||
((HttpStatusCode)Convert.ToInt32(error.Attribute("code").Value)).VerifyStatusCode(error.Value);
|
||||
}
|
||||
}
|
||||
|
||||
public void Execute(TestNotifyMyAndroidCommand message)
|
||||
{
|
||||
const string title = "Test Notification";
|
||||
const string body = "This is a test message from NzbDrone";
|
||||
Verify(message.ApiKey);
|
||||
SendNotification(title, body, message.ApiKey, (NotifyMyAndroidPriority)message.Priority);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using NzbDrone.Core.Annotations;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.NotifyMyAndroid
|
||||
{
|
||||
public class NotifyMyAndroidSettings : INotifcationSettings
|
||||
{
|
||||
[FieldDefinition(0, Label = "API Key", HelpLink = "http://www.notifymyandroid.com/")]
|
||||
public String ApiKey { get; set; }
|
||||
|
||||
[FieldDefinition(1, Label = "Priority", Type = FieldType.Select, SelectOptions = typeof(NotifyMyAndroidPriority))]
|
||||
public Int32 Priority { get; set; }
|
||||
|
||||
public bool IsValid
|
||||
{
|
||||
get
|
||||
{
|
||||
return !String.IsNullOrWhiteSpace(ApiKey) && Priority != null & Priority >= -1 && Priority <= 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,18 @@
|
||||
using NzbDrone.Core.Messaging.Commands;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.NotifyMyAndroid
|
||||
{
|
||||
public class TestNotifyMyAndroidCommand : Command
|
||||
{
|
||||
|
||||
public override bool SendUpdatesToClient
|
||||
{
|
||||
get
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
public string ApiKey { get; set; }
|
||||
public int Priority { get; set; }
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user