mirror of
https://github.com/Radarr/Radarr.git
synced 2026-04-25 22:37:27 -04:00
Improved PushBullet implementation (v2 API, multiple devices, channels)
New: PushBullet supports multiple devices New: PushBullet channels Closes #641
This commit is contained in:
@@ -23,14 +23,14 @@ namespace NzbDrone.Core.Notifications.PushBullet
|
||||
{
|
||||
const string title = "Sonarr - Episode Grabbed";
|
||||
|
||||
_proxy.SendNotification(title, message, Settings.ApiKey, Settings.DeviceId);
|
||||
_proxy.SendNotification(title, message, Settings);
|
||||
}
|
||||
|
||||
public override void OnDownload(DownloadMessage message)
|
||||
{
|
||||
const string title = "Sonarr - Episode Downloaded";
|
||||
|
||||
_proxy.SendNotification(title, message.Message, Settings.ApiKey, Settings.DeviceId);
|
||||
_proxy.SendNotification(title, message.Message, Settings);
|
||||
}
|
||||
|
||||
public override void AfterRename(Series series)
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using NzbDrone.Common.Exceptions;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.PushBullet
|
||||
{
|
||||
public class PushBulletException : NzbDroneException
|
||||
{
|
||||
public PushBulletException(string message) : base(message)
|
||||
{
|
||||
}
|
||||
|
||||
public PushBulletException(string message, Exception innerException, params object[] args) : base(message, innerException, args)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,10 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using FluentValidation.Results;
|
||||
using NLog;
|
||||
using NzbDrone.Common.Extensions;
|
||||
using RestSharp;
|
||||
using NzbDrone.Core.Rest;
|
||||
|
||||
@@ -9,49 +12,82 @@ namespace NzbDrone.Core.Notifications.PushBullet
|
||||
{
|
||||
public interface IPushBulletProxy
|
||||
{
|
||||
void SendNotification(string title, string message, string apiKey, string deviceId);
|
||||
void SendNotification(string title, string message, PushBulletSettings settings);
|
||||
ValidationFailure Test(PushBulletSettings settings);
|
||||
}
|
||||
|
||||
public class PushBulletProxy : IPushBulletProxy
|
||||
{
|
||||
private readonly Logger _logger;
|
||||
private const string URL = "https://api.pushbullet.com/api/pushes";
|
||||
private const string URL = "https://api.pushbullet.com/v2/pushes";
|
||||
|
||||
public PushBulletProxy(Logger logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void SendNotification(string title, string message, string apiKey, string deviceId)
|
||||
public void SendNotification(string title, string message, PushBulletSettings settings)
|
||||
{
|
||||
var client = RestClientFactory.BuildClient(URL);
|
||||
var request = BuildRequest(deviceId);
|
||||
|
||||
request.AddParameter("type", "note");
|
||||
request.AddParameter("title", title);
|
||||
request.AddParameter("body", message);
|
||||
var channelTags = GetIds(settings.ChannelTags);
|
||||
var deviceIds = GetIds(settings.DeviceIds);
|
||||
var error = false;
|
||||
|
||||
client.Authenticator = new HttpBasicAuthenticator(apiKey, String.Empty);
|
||||
client.ExecuteAndValidate(request);
|
||||
}
|
||||
|
||||
public RestRequest BuildRequest(string deviceId)
|
||||
{
|
||||
var request = new RestRequest(Method.POST);
|
||||
long integerId;
|
||||
|
||||
if (Int64.TryParse(deviceId, out integerId))
|
||||
if (channelTags.Any())
|
||||
{
|
||||
request.AddParameter("device_id", integerId);
|
||||
}
|
||||
foreach (var channelTag in channelTags)
|
||||
{
|
||||
var request = BuildChannelRequest(channelTag);
|
||||
|
||||
try
|
||||
{
|
||||
SendNotification(title, message, request, settings);
|
||||
}
|
||||
catch (PushBulletException ex)
|
||||
{
|
||||
_logger.ErrorException("Unable to send test message to: " + channelTag, ex);
|
||||
error = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
request.AddParameter("device_iden", deviceId);
|
||||
if (deviceIds.Any())
|
||||
{
|
||||
foreach (var deviceId in deviceIds)
|
||||
{
|
||||
var request = BuildDeviceRequest(deviceId);
|
||||
|
||||
try
|
||||
{
|
||||
SendNotification(title, message, request, settings);
|
||||
}
|
||||
catch (PushBulletException ex)
|
||||
{
|
||||
_logger.ErrorException("Unable to send test message to: " + deviceId, ex);
|
||||
error = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var request = BuildDeviceRequest(null);
|
||||
|
||||
try
|
||||
{
|
||||
SendNotification(title, message, request, settings);
|
||||
}
|
||||
catch (PushBulletException ex)
|
||||
{
|
||||
_logger.ErrorException("Unable to send test message to all devices", ex);
|
||||
error = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return request;
|
||||
if (error)
|
||||
{
|
||||
throw new PushBulletException("Unable to send PushBullet notifications to all channels or devices");
|
||||
}
|
||||
}
|
||||
|
||||
public ValidationFailure Test(PushBulletSettings settings)
|
||||
@@ -61,7 +97,7 @@ namespace NzbDrone.Core.Notifications.PushBullet
|
||||
const string title = "Sonarr - Test Notification";
|
||||
const string body = "This is a test message from Sonarr";
|
||||
|
||||
SendNotification(title, body, settings.ApiKey, settings.DeviceId);
|
||||
SendNotification(title, body, settings);
|
||||
}
|
||||
catch (RestException ex)
|
||||
{
|
||||
@@ -82,5 +118,63 @@ namespace NzbDrone.Core.Notifications.PushBullet
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private RestRequest BuildDeviceRequest(string deviceId)
|
||||
{
|
||||
var request = new RestRequest(Method.POST);
|
||||
long integerId;
|
||||
|
||||
if (Int64.TryParse(deviceId, out integerId))
|
||||
{
|
||||
request.AddParameter("device_id", integerId);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
request.AddParameter("device_iden", deviceId);
|
||||
}
|
||||
|
||||
return request;
|
||||
}
|
||||
|
||||
private RestRequest BuildChannelRequest(string channelTag)
|
||||
{
|
||||
var request = new RestRequest(Method.POST);
|
||||
request.AddParameter("channel_tag", channelTag);
|
||||
|
||||
return request;
|
||||
}
|
||||
|
||||
private void SendNotification(string title, string message, RestRequest request, PushBulletSettings settings)
|
||||
{
|
||||
try
|
||||
{
|
||||
var client = RestClientFactory.BuildClient(URL);
|
||||
|
||||
request.AddParameter("type", "note");
|
||||
request.AddParameter("title", title);
|
||||
request.AddParameter("body", message);
|
||||
|
||||
client.Authenticator = new HttpBasicAuthenticator(settings.ApiKey, String.Empty);
|
||||
client.ExecuteAndValidate(request);
|
||||
}
|
||||
catch (RestException ex)
|
||||
{
|
||||
if (ex.Response.StatusCode == HttpStatusCode.Unauthorized)
|
||||
{
|
||||
_logger.ErrorException("API Key is invalid: " + ex.Message, ex);
|
||||
throw;
|
||||
}
|
||||
|
||||
throw new PushBulletException("Unable to send text message: {0}", ex, ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
private List<string> GetIds(string input)
|
||||
{
|
||||
if (input.IsNullOrWhiteSpace()) return new List<string>();
|
||||
|
||||
return input.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,16 +21,11 @@ namespace NzbDrone.Core.Notifications.PushBullet
|
||||
[FieldDefinition(0, Label = "API Key", HelpLink = "https://www.pushbullet.com/")]
|
||||
public String ApiKey { get; set; }
|
||||
|
||||
[FieldDefinition(1, Label = "Device ID", HelpText = "device_iden in the device's URL on pushbullet.com (leave blank to send to all devices)")]
|
||||
public String DeviceId { get; set; }
|
||||
[FieldDefinition(1, Label = "Device IDs", HelpText = "List of device IDs, use device_iden in the device's URL on pushbullet.com (leave blank to send to all devices)", Type = FieldType.Tag)]
|
||||
public String DeviceIds { get; set; }
|
||||
|
||||
public bool IsValid
|
||||
{
|
||||
get
|
||||
{
|
||||
return !String.IsNullOrWhiteSpace(ApiKey) && !String.IsNullOrWhiteSpace(DeviceId);
|
||||
}
|
||||
}
|
||||
[FieldDefinition(2, Label = "Channel Tags", HelpText = "List of Channel Tags to send notifications to", Type = FieldType.Tag)]
|
||||
public String ChannelTags { get; set; }
|
||||
|
||||
public NzbDroneValidationResult Validate()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user