Added: Device load support for Pushbullet

Co-Authored-By: Mark McDowall <markus101@users.noreply.github.com>
This commit is contained in:
Qstick 2018-09-04 22:41:22 -04:00
commit 23bc5b11cf
19 changed files with 439 additions and 27 deletions

View file

@ -34,6 +34,7 @@ namespace NzbDrone.Core.Annotations
Action,
Url,
Captcha,
OAuth
OAuth,
Device
}
}

View file

@ -1,6 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using FluentValidation.Results;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.Validation;
namespace NzbDrone.Core.Notifications.PushBullet
{
@ -16,7 +19,6 @@ namespace NzbDrone.Core.Notifications.PushBullet
public override string Name => "Pushbullet";
public override string Link => "https://www.pushbullet.com/";
public override void OnGrab(GrabMessage grabMessage)
{
_proxy.SendNotification(ALBUM_GRABBED_TITLE_BRANDED, grabMessage.Message, Settings);
@ -40,5 +42,36 @@ namespace NzbDrone.Core.Notifications.PushBullet
return new ValidationResult(failures);
}
public override object RequestAction(string action, IDictionary<string, string> query)
{
if (action == "getDevices")
{
// Return early if there is not an API key
if (Settings.ApiKey.IsNullOrWhiteSpace())
{
return new
{
devices = new List<object>()
};
}
Settings.Validate().Filter("ApiKey").ThrowOnError();
var devices = _proxy.GetDevices(Settings);
return new
{
devices = devices.Where(d => d.Nickname.IsNotNullOrWhiteSpace())
.OrderBy(d => d.Nickname, StringComparer.InvariantCultureIgnoreCase)
.Select(d => new
{
id = d.Id,
name = d.Nickname
})
};
}
return new { };
}
}
}

View file

@ -0,0 +1,14 @@
using Newtonsoft.Json;
namespace NzbDrone.Core.Notifications.PushBullet
{
public class PushBulletDevice
{
[JsonProperty(PropertyName = "Iden")]
public string Id { get; set; }
public string Nickname { get; set; }
public string Manufacturer { get; set; }
public string Model { get; set; }
}
}

View file

@ -0,0 +1,9 @@
using System.Collections.Generic;
namespace NzbDrone.Core.Notifications.PushBullet
{
public class PushBulletDevicesResponse
{
public List<PushBulletDevice> Devices { get; set; }
}
}

View file

@ -1,4 +1,5 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using FluentValidation.Results;
@ -6,6 +7,7 @@ using NLog;
using RestSharp;
using NzbDrone.Core.Rest;
using NzbDrone.Common.Extensions;
using NzbDrone.Common.Serializer;
using RestSharp.Authenticators;
namespace NzbDrone.Core.Notifications.PushBullet
@ -13,13 +15,15 @@ namespace NzbDrone.Core.Notifications.PushBullet
public interface IPushBulletProxy
{
void SendNotification(string title, string message, PushBulletSettings settings);
List<PushBulletDevice> GetDevices(PushBulletSettings settings);
ValidationFailure Test(PushBulletSettings settings);
}
public class PushBulletProxy : IPushBulletProxy
{
private readonly Logger _logger;
private const string URL = "https://api.pushbullet.com/v2/pushes";
private const string PUSH_URL = "https://api.pushbullet.com/v2/pushes";
private const string DEVICE_URL = "https://api.pushbullet.com/v2/devices";
public PushBulletProxy(Logger logger)
{
@ -88,6 +92,30 @@ namespace NzbDrone.Core.Notifications.PushBullet
}
}
public List<PushBulletDevice> GetDevices(PushBulletSettings settings)
{
try
{
var client = RestClientFactory.BuildClient(DEVICE_URL);
var request = new RestRequest(Method.GET);
client.Authenticator = new HttpBasicAuthenticator(settings.ApiKey, string.Empty);
var response = client.ExecuteAndValidate(request);
return Json.Deserialize<PushBulletDevicesResponse>(response.Content).Devices;
}
catch (RestException ex)
{
if (ex.Response.StatusCode == HttpStatusCode.Unauthorized)
{
_logger.Error(ex, "Access token is invalid");
throw;
}
}
return new List<PushBulletDevice>();
}
public ValidationFailure Test(PushBulletSettings settings)
{
try
@ -147,7 +175,7 @@ namespace NzbDrone.Core.Notifications.PushBullet
{
try
{
var client = RestClientFactory.BuildClient(URL);
var client = RestClientFactory.BuildClient(PUSH_URL);
request.AddParameter("type", "note");
request.AddParameter("title", title);
@ -165,7 +193,7 @@ namespace NzbDrone.Core.Notifications.PushBullet
{
if (ex.Response.StatusCode == HttpStatusCode.Unauthorized)
{
_logger.Error(ex, "API Key is invalid");
_logger.Error(ex, "Access token is invalid");
throw;
}

View file

@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using FluentValidation;
using NzbDrone.Core.Annotations;
using NzbDrone.Core.ThingiProvider;
@ -20,14 +20,14 @@ namespace NzbDrone.Core.Notifications.PushBullet
public PushBulletSettings()
{
DeviceIds = new string[]{};
ChannelTags = new string[]{};
DeviceIds = new string[] { };
ChannelTags = new string[] { };
}
[FieldDefinition(0, Label = "API Key", HelpLink = "https://www.pushbullet.com/")]
[FieldDefinition(0, Label = "Access Token", HelpLink = "https://www.pushbullet.com/#settings/account")]
public string ApiKey { 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)]
[FieldDefinition(1, Label = "Device IDs", HelpText = "List of device IDs (leave blank to send to all devices)", Type = FieldType.Device)]
public IEnumerable<string> DeviceIds { get; set; }
[FieldDefinition(2, Label = "Channel Tags", HelpText = "List of Channel Tags to send notifications to", Type = FieldType.Tag)]

View file

@ -1,4 +1,4 @@
using System;
using System;
using FluentValidation.Results;
using NLog;
using NzbDrone.Common.Extensions;
@ -29,6 +29,7 @@ namespace NzbDrone.Core.Notifications.Pushover
var request = new RestRequest(Method.POST);
request.AddParameter("token", settings.ApiKey);
request.AddParameter("user", settings.UserKey);
request.AddParameter("device", string.Join(",", settings.Devices));
request.AddParameter("title", title);
request.AddParameter("message", message);
request.AddParameter("priority", settings.Priority);

View file

@ -1,7 +1,8 @@
using FluentValidation;
using FluentValidation;
using NzbDrone.Core.Annotations;
using NzbDrone.Core.ThingiProvider;
using NzbDrone.Core.Validation;
using System.Collections.Generic;
namespace NzbDrone.Core.Notifications.Pushover
{
@ -22,25 +23,29 @@ namespace NzbDrone.Core.Notifications.Pushover
public PushoverSettings()
{
Priority = 0;
Devices = new string[] { };
}
//TODO: Get Pushover to change our app name (or create a new app) when we have a new logo
[FieldDefinition(0, Label = "API Key", HelpLink = "https://pushover.net/apps/clone/nzbdrone")]
[FieldDefinition(0, Label = "API Key", HelpLink = "https://pushover.net/apps/clone/lidarr")]
public string ApiKey { get; set; }
[FieldDefinition(1, Label = "User Key", HelpLink = "https://pushover.net/")]
public string UserKey { get; set; }
[FieldDefinition(2, Label = "Priority", Type = FieldType.Select, SelectOptions = typeof(PushoverPriority) )]
[FieldDefinition(2, Label = "Devices", HelpText = "List of device names (leave blank to send to all devices)", Type = FieldType.Tag)]
public IEnumerable<string> Devices { get; set; }
[FieldDefinition(3, Label = "Priority", Type = FieldType.Select, SelectOptions = typeof(PushoverPriority))]
public int Priority { get; set; }
[FieldDefinition(3, Label = "Retry", Type = FieldType.Textbox, HelpText = "Interval to retry Emergency alerts, minimum 30 seconds")]
[FieldDefinition(4, Label = "Retry", Type = FieldType.Textbox, HelpText = "Interval to retry Emergency alerts, minimum 30 seconds")]
public int Retry { get; set; }
[FieldDefinition(4, Label = "Expire", Type = FieldType.Textbox, HelpText = "Maximum time to retry Emergency alerts, maximum 86400 seconds")]
[FieldDefinition(5, Label = "Expire", Type = FieldType.Textbox, HelpText = "Maximum time to retry Emergency alerts, maximum 86400 seconds")]
public int Expire { get; set; }
[FieldDefinition(5, Label = "Sound", Type = FieldType.Textbox, HelpText = "Notification sound, leave blank to use the default", HelpLink = "https://pushover.net/api#sounds")]
[FieldDefinition(6, Label = "Sound", Type = FieldType.Textbox, HelpText = "Notification sound, leave blank to use the default", HelpLink = "https://pushover.net/api#sounds")]
public string Sound { get; set; }
public bool IsValid => !string.IsNullOrWhiteSpace(UserKey) && Priority >= -1 && Priority <= 2;

View file

@ -900,7 +900,10 @@
<Compile Include="Notifications\Plex\HomeTheater\PlexClientService.cs" />
<Compile Include="Notifications\Plex\Server\PlexServer.cs" />
<Compile Include="Notifications\Plex\Server\PlexServerProxy.cs" />
<Compile Include="Notifications\PushBullet\PushBulletDevice.cs" />
<Compile Include="Notifications\PushBullet\PushBulletDevicesResponse.cs" />
<Compile Include="Notifications\PushBullet\PushBulletException.cs" />
<Compile Include="Notifications\Pushover\PushoverProxy.cs" />
<Compile Include="Notifications\Slack\Payloads\Attachment.cs" />
<Compile Include="Notifications\Slack\Payloads\SlackPayload.cs" />
<Compile Include="Notifications\Slack\Slack.cs" />
@ -1027,7 +1030,6 @@
<Compile Include="Notifications\Pushover\InvalidResponseException.cs" />
<Compile Include="Notifications\Pushover\Pushover.cs" />
<Compile Include="Notifications\Pushover\PushoverPriority.cs" />
<Compile Include="Notifications\Pushover\PushoverService.cs" />
<Compile Include="Notifications\Pushover\PushoverSettings.cs" />
<Compile Include="Notifications\Xbmc\XbmcJsonException.cs" />
<Compile Include="Notifications\Xbmc\HttpApiProvider.cs" />