mirror of
https://github.com/lidarr/lidarr.git
synced 2025-08-14 02:37:08 -07:00
Improved PushBullet implementation (v2 API, multiple devices, channels)
New: PushBullet supports multiple devices New: PushBullet channels Closes #641
This commit is contained in:
parent
120be567d3
commit
77e9493ccf
12 changed files with 312 additions and 37 deletions
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue