mirror of
https://github.com/Readarr/Readarr
synced 2025-12-25 17:54:14 +01:00
Add support for discord notifier
This commit is contained in:
parent
d096536be3
commit
2a3243f37c
4 changed files with 248 additions and 0 deletions
|
|
@ -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<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 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<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("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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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…
Reference in a new issue