From 2a3243f37ce8e2d99f99c3cd1ca16cc8cd1a60a5 Mon Sep 17 00:00:00 2001 From: nitsua Date: Mon, 12 Oct 2020 12:39:33 -0400 Subject: [PATCH] Add support for discord notifier --- .../DiscordNotifier/DiscordNotifier.cs | 103 ++++++++++++++++++ .../DiscordNotifierException.cs | 18 +++ .../DiscordNotifier/DiscordNotifierProxy.cs | 99 +++++++++++++++++ .../DiscordNotifierSettings.cs | 28 +++++ 4 files changed, 248 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..e95fcb34f --- /dev/null +++ b/src/NzbDrone.Core/Notifications/DiscordNotifier/DiscordNotifier.cs @@ -0,0 +1,103 @@ +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 => "Discord Notifier"; + + public override void OnGrab(GrabMessage message) + { + var author = message.Author; + var remoteBook = message.Book; + var releaseGroup = remoteBook.ParsedBookInfo.ReleaseGroup; + var variables = new StringDictionary(); + + variables.Add("Readarr_EventType", "Grab"); + variables.Add("Readarr_Author_Id", author.Id.ToString()); + variables.Add("Readarr_Author_Name", author.Metadata.Value.Name); + variables.Add("Readarr_Author_MBId", author.Metadata.Value.ForeignAuthorId); + variables.Add("Readarr_Release_BookCount", remoteBook.Books.Count.ToString()); + variables.Add("Readarr_Release_BookReleaseDates", string.Join(",", remoteBook.Books.Select(e => e.ReleaseDate))); + variables.Add("Readarr_Release_BookTitles", string.Join("|", remoteBook.Books.Select(e => e.Title))); + variables.Add("Readarr_Release_BookIds", string.Join("|", remoteBook.Books.Select(e => e.Id.ToString()))); + variables.Add("Readarr_Release_Title", remoteBook.Release.Title); + variables.Add("Readarr_Release_Indexer", remoteBook.Release.Indexer ?? string.Empty); + variables.Add("Readarr_Release_Size", remoteBook.Release.Size.ToString()); + variables.Add("Readarr_Release_Quality", remoteBook.ParsedBookInfo.Quality.Quality.Name); + variables.Add("Readarr_Release_QualityVersion", remoteBook.ParsedBookInfo.Quality.Revision.Version.ToString()); + variables.Add("Readarr_Release_ReleaseGroup", releaseGroup ?? string.Empty); + variables.Add("Readarr_Download_Client", message.DownloadClient ?? string.Empty); + variables.Add("Readarr_Download_Id", message.DownloadId ?? string.Empty); + + _proxy.SendNotification(variables, Settings); + } + + public override void OnReleaseImport(BookDownloadMessage message) + { + var author = message.Author; + var book = message.Book; + var variables = new StringDictionary(); + + variables.Add("Readarr_EventType", "Download"); + variables.Add("Readarr_Author_Id", author.Id.ToString()); + variables.Add("Readarr_Author_Name", author.Metadata.Value.Name); + variables.Add("Readarr_Author_Path", author.Path); + variables.Add("Readarr_Author_MBId", author.Metadata.Value.ForeignAuthorId); + variables.Add("Readarr_Book_Id", book.Id.ToString()); + variables.Add("Readarr_Book_Title", book.Title); + variables.Add("Readarr_Book_MBId", book.ForeignBookId); + variables.Add("Readarr_Book_ReleaseDate", book.ReleaseDate.ToString()); + variables.Add("Readarr_Download_Client", message.DownloadClient ?? string.Empty); + variables.Add("Readarr_Download_Id", message.DownloadId ?? string.Empty); + + if (message.BookFiles.Any()) + { + variables.Add("Readarr_AddedTrackPaths", string.Join("|", message.BookFiles.Select(e => e.Path))); + } + + if (message.OldFiles.Any()) + { + variables.Add("Readarr_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("Readarr_EventType", "HealthIssue"); + variables.Add("Readarr_Health_Issue_Level", nameof(healthCheck.Type)); + variables.Add("Readarr_Health_Issue_Message", healthCheck.Message); + variables.Add("Readarr_Health_Issue_Type", healthCheck.Source.Name); + variables.Add("Readarr_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..bf237e20b --- /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("Readarr_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..e1c2216d1 --- /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)); + } + } +}