From 4107d6aa3f0f49918b529c23dd45df6560623d8c Mon Sep 17 00:00:00 2001 From: nitsua Date: Sat, 17 Oct 2020 19:18:13 -0400 Subject: [PATCH] Add a connect for Discord Notifier --- .../DiscordNotifier/DiscordNotifier.cs | 107 ++++++++++++++++++ .../DiscordNotifierException.cs | 18 +++ .../DiscordNotifier/DiscordNotifierProxy.cs | 99 ++++++++++++++++ .../DiscordNotifierSettings.cs | 28 +++++ 4 files changed, 252 insertions(+) create mode 100644 src/NzbDrone.Core/Notifications/DiscordNotifier/DiscordNotifier.cs create mode 100644 src/NzbDrone.Core/Notifications/DiscordNotifier/DiscordNotifierException.cs create mode 100644 src/NzbDrone.Core/Notifications/DiscordNotifier/DiscordNotifierProxy.cs create mode 100644 src/NzbDrone.Core/Notifications/DiscordNotifier/DiscordNotifierSettings.cs diff --git a/src/NzbDrone.Core/Notifications/DiscordNotifier/DiscordNotifier.cs b/src/NzbDrone.Core/Notifications/DiscordNotifier/DiscordNotifier.cs new file mode 100644 index 000000000..1c4830ca0 --- /dev/null +++ b/src/NzbDrone.Core/Notifications/DiscordNotifier/DiscordNotifier.cs @@ -0,0 +1,107 @@ +using System; +using System.Collections.Generic; +using System.Collections.Specialized; +using System.IO; +using System.Linq; +using FluentValidation.Results; +using NzbDrone.Common.Extensions; + +namespace NzbDrone.Core.Notifications.DiscordNotifier +{ + public class DiscordNotifier : NotificationBase + { + private readonly IDiscordNotifierProxy _proxy; + + public DiscordNotifier(IDiscordNotifierProxy proxy) + { + _proxy = proxy; + } + + public override string Link => "https://discordnotifier.com"; + public override string Name => "DiscordNotifier.com"; + + public override void OnGrab(GrabMessage message) + { + var artist = message.Artist; + var remoteAlbum = message.Album; + var releaseGroup = remoteAlbum.ParsedAlbumInfo.ReleaseGroup; + var variables = new StringDictionary(); + + variables.Add("Lidarr_EventType", "Grab"); + variables.Add("Lidarr_Artist_Id", artist.Id.ToString()); + variables.Add("Lidarr_Artist_Name", artist.Metadata.Value.Name); + variables.Add("Lidarr_Artist_MBId", artist.Metadata.Value.ForeignArtistId); + variables.Add("Lidarr_Artist_Type", artist.Metadata.Value.Type); + variables.Add("Lidarr_Release_AlbumCount", remoteAlbum.Albums.Count.ToString()); + variables.Add("Lidarr_Release_AlbumReleaseDates", string.Join(",", remoteAlbum.Albums.Select(e => e.ReleaseDate))); + variables.Add("Lidarr_Release_AlbumTitles", string.Join("|", remoteAlbum.Albums.Select(e => e.Title))); + variables.Add("Lidarr_Release_AlbumMBIds", string.Join("|", remoteAlbum.Albums.Select(e => e.ForeignAlbumId))); + variables.Add("Lidarr_Release_Title", remoteAlbum.Release.Title); + variables.Add("Lidarr_Release_Indexer", remoteAlbum.Release.Indexer ?? string.Empty); + variables.Add("Lidarr_Release_Size", remoteAlbum.Release.Size.ToString()); + variables.Add("Lidarr_Release_Quality", remoteAlbum.ParsedAlbumInfo.Quality.Quality.Name); + variables.Add("Lidarr_Release_QualityVersion", remoteAlbum.ParsedAlbumInfo.Quality.Revision.Version.ToString()); + variables.Add("Lidarr_Release_ReleaseGroup", releaseGroup ?? string.Empty); + variables.Add("Lidarr_Download_Client", message.DownloadClient ?? string.Empty); + variables.Add("Lidarr_Download_Id", message.DownloadId ?? string.Empty); + + _proxy.SendNotification(variables, Settings); + } + + public override void OnReleaseImport(AlbumDownloadMessage message) + { + var artist = message.Artist; + var album = message.Album; + var release = message.Release; + var variables = new StringDictionary(); + + variables.Add("Lidarr_EventType", "Download"); + variables.Add("Lidarr_Artist_Id", artist.Id.ToString()); + variables.Add("Lidarr_Artist_Name", artist.Metadata.Value.Name); + variables.Add("Lidarr_Artist_Path", artist.Path); + variables.Add("Lidarr_Artist_MBId", artist.Metadata.Value.ForeignArtistId); + variables.Add("Lidarr_Artist_Type", artist.Metadata.Value.Type); + variables.Add("Lidarr_Album_Id", album.Id.ToString()); + variables.Add("Lidarr_Album_Title", album.Title); + variables.Add("Lidarr_Album_MBId", album.ForeignAlbumId); + variables.Add("Lidarr_AlbumRelease_MBId", release.ForeignReleaseId); + variables.Add("Lidarr_Album_ReleaseDate", album.ReleaseDate.ToString()); + variables.Add("Lidarr_Download_Client", message.DownloadClient ?? string.Empty); + variables.Add("Lidarr_Download_Id", message.DownloadId ?? string.Empty); + + if (message.TrackFiles.Any()) + { + variables.Add("Lidarr_AddedTrackPaths", string.Join("|", message.TrackFiles.Select(e => e.Path))); + } + + if (message.OldFiles.Any()) + { + variables.Add("Lidarr_DeletedPaths", string.Join("|", message.OldFiles.Select(e => e.Path))); + } + + _proxy.SendNotification(variables, Settings); + } + + public override void OnHealthIssue(HealthCheck.HealthCheck healthCheck) + { + var variables = new StringDictionary(); + + variables.Add("Lidarr_EventType", "HealthIssue"); + variables.Add("Lidarr_Health_Issue_Level", nameof(healthCheck.Type)); + variables.Add("Lidarr_Health_Issue_Message", healthCheck.Message); + variables.Add("Lidarr_Health_Issue_Type", healthCheck.Source.Name); + variables.Add("Lidarr_Health_Issue_Wiki", healthCheck.WikiUrl.ToString() ?? string.Empty); + + _proxy.SendNotification(variables, Settings); + } + + public override ValidationResult Test() + { + var failures = new List(); + + failures.AddIfNotNull(_proxy.Test(Settings)); + + return new ValidationResult(failures); + } + } +} diff --git a/src/NzbDrone.Core/Notifications/DiscordNotifier/DiscordNotifierException.cs b/src/NzbDrone.Core/Notifications/DiscordNotifier/DiscordNotifierException.cs new file mode 100644 index 000000000..e41b776da --- /dev/null +++ b/src/NzbDrone.Core/Notifications/DiscordNotifier/DiscordNotifierException.cs @@ -0,0 +1,18 @@ +using System; +using NzbDrone.Common.Exceptions; + +namespace NzbDrone.Core.Notifications.DiscordNotifier +{ + public class DiscordNotifierException : NzbDroneException + { + public DiscordNotifierException(string message) + : base(message) + { + } + + public DiscordNotifierException(string message, Exception innerException, params object[] args) + : base(message, innerException, args) + { + } + } +} diff --git a/src/NzbDrone.Core/Notifications/DiscordNotifier/DiscordNotifierProxy.cs b/src/NzbDrone.Core/Notifications/DiscordNotifier/DiscordNotifierProxy.cs new file mode 100644 index 000000000..548ac3bdc --- /dev/null +++ b/src/NzbDrone.Core/Notifications/DiscordNotifier/DiscordNotifierProxy.cs @@ -0,0 +1,99 @@ +using System; +using System.Collections.Generic; +using System.Collections.Specialized; +using System.Net; +using FluentValidation.Results; +using NLog; +using NzbDrone.Common.EnvironmentInfo; +using NzbDrone.Common.Http; + +namespace NzbDrone.Core.Notifications.DiscordNotifier +{ + public interface IDiscordNotifierProxy + { + void SendNotification(StringDictionary message, DiscordNotifierSettings settings); + ValidationFailure Test(DiscordNotifierSettings settings); + } + + public class DiscordNotifierProxy : IDiscordNotifierProxy + { + private const string URL = "https://discordnotifier.com/notifier.php"; + private readonly IHttpClient _httpClient; + private readonly Logger _logger; + + public DiscordNotifierProxy(IHttpClient httpClient, Logger logger) + { + _httpClient = httpClient; + _logger = logger; + } + + public void SendNotification(StringDictionary message, DiscordNotifierSettings settings) + { + try + { + ProcessNotification(message, settings); + } + catch (DiscordNotifierException ex) + { + _logger.Error(ex, "Unable to send notification"); + throw new DiscordNotifierException("Unable to send notification"); + } + } + + public ValidationFailure Test(DiscordNotifierSettings settings) + { + try + { + var variables = new StringDictionary(); + variables.Add("Lidarr_EventType", "Test"); + + SendNotification(variables, settings); + return null; + } + catch (HttpException ex) + { + if (ex.Response.StatusCode == HttpStatusCode.Unauthorized) + { + _logger.Error(ex, "API key is invalid: " + ex.Message); + return new ValidationFailure("APIKey", "API key is invalid"); + } + + _logger.Error(ex, "Unable to send test message: " + ex.Message); + return new ValidationFailure("APIKey", "Unable to send test notification"); + } + catch (Exception ex) + { + _logger.Error(ex, "Unable to send test notification: " + ex.Message); + return new ValidationFailure("", "Unable to send test notification"); + } + } + + private void ProcessNotification(StringDictionary message, DiscordNotifierSettings settings) + { + try + { + var requestBuilder = new HttpRequestBuilder(URL).Post(); + requestBuilder.AddFormParameter("api", settings.APIKey).Build(); + + foreach (string key in message.Keys) + { + requestBuilder.AddFormParameter(key, message[key]); + } + + var request = requestBuilder.Build(); + + _httpClient.Post(request); + } + catch (HttpException ex) + { + if (ex.Response.StatusCode == HttpStatusCode.BadRequest) + { + _logger.Error(ex, "API key is invalid"); + throw; + } + + throw new DiscordNotifierException("Unable to send notification", ex); + } + } + } +} diff --git a/src/NzbDrone.Core/Notifications/DiscordNotifier/DiscordNotifierSettings.cs b/src/NzbDrone.Core/Notifications/DiscordNotifier/DiscordNotifierSettings.cs new file mode 100644 index 000000000..ae3f910cc --- /dev/null +++ b/src/NzbDrone.Core/Notifications/DiscordNotifier/DiscordNotifierSettings.cs @@ -0,0 +1,28 @@ +using FluentValidation; +using NzbDrone.Core.Annotations; +using NzbDrone.Core.ThingiProvider; +using NzbDrone.Core.Validation; + +namespace NzbDrone.Core.Notifications.DiscordNotifier +{ + public class DiscordNotifierSettingsValidator : AbstractValidator + { + public DiscordNotifierSettingsValidator() + { + RuleFor(c => c.APIKey).NotEmpty(); + } + } + + public class DiscordNotifierSettings : IProviderConfig + { + private static readonly DiscordNotifierSettingsValidator Validator = new DiscordNotifierSettingsValidator(); + + [FieldDefinition(0, Label = "API Key", HelpText = "Your API key from your profile", HelpLink = "https://discordnotifier.com")] + public string APIKey { get; set; } + + public NzbDroneValidationResult Validate() + { + return new NzbDroneValidationResult(Validator.Validate(this)); + } + } +}