diff --git a/src/NzbDrone.Core/Notifications/DiscordNotifier/DiscordNotifier.cs b/src/NzbDrone.Core/Notifications/DiscordNotifier/DiscordNotifier.cs new file mode 100644 index 000000000..a31cf5327 --- /dev/null +++ b/src/NzbDrone.Core/Notifications/DiscordNotifier/DiscordNotifier.cs @@ -0,0 +1,111 @@ +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 movie = message.Movie; + var remoteMovie = message.RemoteMovie; + var quality = message.Quality; + var variables = new StringDictionary(); + + variables.Add("Radarr_EventType", "Grab"); + variables.Add("Radarr_Movie_Id", movie.Id.ToString()); + variables.Add("Radarr_Movie_Title", movie.Title); + variables.Add("Radarr_Movie_Year", movie.Year.ToString()); + variables.Add("Radarr_Movie_ImdbId", movie.ImdbId ?? string.Empty); + variables.Add("Radarr_Movie_TmdbId", movie.TmdbId.ToString()); + variables.Add("Radarr_Movie_In_Cinemas_Date", movie.InCinemas.ToString() ?? string.Empty); + variables.Add("Radarr_Movie_Physical_Release_Date", movie.PhysicalRelease.ToString() ?? string.Empty); + variables.Add("Radarr_Release_Title", remoteMovie.Release.Title); + variables.Add("Radarr_Release_Indexer", remoteMovie.Release.Indexer ?? string.Empty); + variables.Add("Radarr_Release_Size", remoteMovie.Release.Size.ToString()); + variables.Add("Radarr_Release_ReleaseGroup", remoteMovie.ParsedMovieInfo.ReleaseGroup ?? string.Empty); + variables.Add("Radarr_Release_Quality", quality.Quality.Name); + variables.Add("Radarr_Release_QualityVersion", quality.Revision.Version.ToString()); + variables.Add("Radarr_IndexerFlags", remoteMovie.Release.IndexerFlags.ToString()); + variables.Add("Radarr_Download_Client", message.DownloadClient ?? string.Empty); + variables.Add("Radarr_Download_Id", message.DownloadId ?? string.Empty); + + _proxy.SendNotification(variables, Settings); + } + + public override void OnDownload(DownloadMessage message) + { + var movie = message.Movie; + var movieFile = message.MovieFile; + var sourcePath = message.SourcePath; + var variables = new StringDictionary(); + + variables.Add("Radarr_EventType", "Download"); + variables.Add("Radarr_IsUpgrade", message.OldMovieFiles.Any().ToString()); + variables.Add("Radarr_Movie_Id", movie.Id.ToString()); + variables.Add("Radarr_Movie_Title", movie.Title); + variables.Add("Radarr_Movie_Year", movie.Year.ToString()); + variables.Add("Radarr_Movie_Path", movie.Path); + variables.Add("Radarr_Movie_ImdbId", movie.ImdbId ?? string.Empty); + variables.Add("Radarr_Movie_TmdbId", movie.TmdbId.ToString()); + variables.Add("Radarr_Movie_In_Cinemas_Date", movie.InCinemas.ToString() ?? string.Empty); + variables.Add("Radarr_Movie_Physical_Release_Date", movie.PhysicalRelease.ToString() ?? string.Empty); + variables.Add("Radarr_MovieFile_Id", movieFile.Id.ToString()); + variables.Add("Radarr_MovieFile_RelativePath", movieFile.RelativePath); + variables.Add("Radarr_MovieFile_Path", Path.Combine(movie.Path, movieFile.RelativePath)); + variables.Add("Radarr_MovieFile_Quality", movieFile.Quality.Quality.Name); + variables.Add("Radarr_MovieFile_QualityVersion", movieFile.Quality.Revision.Version.ToString()); + variables.Add("Radarr_MovieFile_ReleaseGroup", movieFile.ReleaseGroup ?? string.Empty); + variables.Add("Radarr_MovieFile_SceneName", movieFile.SceneName ?? string.Empty); + variables.Add("Radarr_MovieFile_SourcePath", sourcePath); + variables.Add("Radarr_MovieFile_SourceFolder", Path.GetDirectoryName(sourcePath)); + variables.Add("Radarr_Download_Id", message.DownloadId ?? string.Empty); + variables.Add("Radarr_Download_Client", message.DownloadClient ?? string.Empty); + + if (message.OldMovieFiles.Any()) + { + variables.Add("Radarr_DeletedRelativePaths", string.Join("|", message.OldMovieFiles.Select(e => e.RelativePath))); + variables.Add("Radarr_DeletedPaths", string.Join("|", message.OldMovieFiles.Select(e => Path.Combine(movie.Path, e.RelativePath)))); + } + + _proxy.SendNotification(variables, Settings); + } + + public override void OnHealthIssue(HealthCheck.HealthCheck healthCheck) + { + var variables = new StringDictionary(); + + variables.Add("Radarr_EventType", "HealthIssue"); + variables.Add("Radarr_Health_Issue_Level", nameof(healthCheck.Type)); + variables.Add("Radarr_Health_Issue_Message", healthCheck.Message); + variables.Add("Radarr_Health_Issue_Type", healthCheck.Source.Name); + variables.Add("Radarr_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..015ed469e --- /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("Radarr_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)); + } + } +}