Add a connect for Discord Notifier

pull/1795/head
nitsua 4 years ago committed by Qstick
parent 0148418732
commit 4107d6aa3f

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

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

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

@ -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));
}
}
}
Loading…
Cancel
Save