New: Add support for Discord Notifier connect type (#4611)

This commit is contained in:
nitsua 2020-09-09 23:46:24 -04:00 committed by GitHub
parent bba2b01980
commit ea37bc2cb7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 256 additions and 0 deletions

View File

@ -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<DiscordNotifierSettings>
{
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<ValidationFailure>();
failures.AddIfNotNull(_proxy.Test(Settings));
return new ValidationResult(failures);
}
}
}

View File

@ -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)
{
}
}
}

View File

@ -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);
}
}
}
}

View File

@ -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<DiscordNotifierSettings>
{
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));
}
}
}