1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-05 02:22:31 +01:00

Updated: Webhook Improvements (#2220) (Fixes #1751)

This commit is contained in:
fhscholl 2017-10-28 11:18:52 -04:00 committed by Leonardo Galli
parent b521ffb588
commit 5e121c78e1
13 changed files with 207 additions and 179 deletions

87
src/NzbDrone.Core/Notifications/Webhook/Webhook.cs Normal file → Executable file
View File

@ -1,35 +1,64 @@

using System.Collections.Generic;
using System.Linq;
using FluentValidation.Results;
using NzbDrone.Core.Tv;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.Validation;
namespace NzbDrone.Core.Notifications.Webhook
{
public class Webhook : NotificationBase<WebhookSettings>
{
private readonly IWebhookService _service;
private readonly IWebhookProxy _proxy;
public Webhook(IWebhookService service)
public Webhook(IWebhookProxy proxy)
{
_service = service;
_proxy = proxy;
}
public override string Link => "https://github.com/Radarr/Radarr/wiki/Webhook";
public override void OnGrab(GrabMessage message)
{
_service.OnGrab(message.Movie, message.RemoteMovie, message.Quality, Settings);
var remoteMovie = message.RemoteMovie;
var quality = message.Quality;
var payload = new WebhookGrabPayload
{
EventType = "Grab",
Movie = new WebhookMovie(message.Movie),
RemoteMovie = new WebhookRemoteMovie(remoteMovie),
Release = new WebhookRelease(quality, remoteMovie)
};
_proxy.SendWebhook(payload, Settings);
}
public override void OnDownload(DownloadMessage message)
{
_service.OnDownload(message.Movie, message.MovieFile, Settings);
var movieFile = message.MovieFile;
var payload = new WebhookImportPayload
{
EventType = "Download",
Movie = new WebhookMovie(message.Movie),
RemoteMovie = new WebhookRemoteMovie(message.Movie),
MovieFile = new WebhookMovieFile(movieFile),
IsUpgrade = message.OldMovieFiles.Any()
};
_proxy.SendWebhook(payload, Settings);
}
public override void OnMovieRename(Movie movie)
{
_service.OnRename(movie, Settings);
var payload = new WebhookPayload
{
EventType = "Rename",
Movie = new WebhookMovie(movie)
};
_proxy.SendWebhook(payload, Settings);
}
public override void OnRename(Series series)
@ -42,9 +71,51 @@ public override ValidationResult Test()
{
var failures = new List<ValidationFailure>();
failures.AddIfNotNull(_service.Test(Settings));
failures.AddIfNotNull(SendWebhookTest());
return new ValidationResult(failures);
}
private ValidationFailure SendWebhookTest()
{
try
{
var payload = new WebhookGrabPayload
{
EventType = "Test",
Movie = new WebhookMovie
{
Id = 1,
Title = "Test Title",
FilePath = "C:\\testpath",
ReleaseDate = "1970-01-01"
},
RemoteMovie = new WebhookRemoteMovie
{
TmdbId = 1234,
ImdbId = "5678",
Title = "Test title",
Year = 1970
},
Release = new WebhookRelease
{
Indexer = "Test Indexer",
Quality = "Test Quality",
QualityVersion = 1,
ReleaseGroup = "Test Group",
ReleaseTitle = "Test Title",
Size = 9999999
}
};
_proxy.SendWebhook(payload, Settings);
}
catch (WebhookException ex)
{
return new NzbDroneValidationFailure("Url", ex.Message);
}
return null;
}
}
}

View File

@ -1,32 +0,0 @@
using NzbDrone.Core.Tv;
using System;
namespace NzbDrone.Core.Notifications.Webhook
{
public class WebhookEpisode
{
public WebhookEpisode() { }
public WebhookEpisode(Episode episode)
{
Id = episode.Id;
SeasonNumber = episode.SeasonNumber;
EpisodeNumber = episode.EpisodeNumber;
Title = episode.Title;
AirDate = episode.AirDate;
AirDateUtc = episode.AirDateUtc;
}
public int Id { get; set; }
public int EpisodeNumber { get; set; }
public int SeasonNumber { get; set; }
public string Title { get; set; }
public string AirDate { get; set; }
public DateTime? AirDateUtc { get; set; }
public string Quality { get; set; }
public int QualityVersion { get; set; }
public string ReleaseGroup { get; set; }
public string SceneName { get; set; }
}
}

View File

@ -0,0 +1,8 @@
namespace NzbDrone.Core.Notifications.Webhook
{
class WebhookGrabPayload : WebhookPayload
{
public WebhookRemoteMovie RemoteMovie { get; set; }
public WebhookRelease Release { get; set; }
}
}

View File

@ -0,0 +1,9 @@
namespace NzbDrone.Core.Notifications.Webhook
{
class WebhookImportPayload : WebhookPayload
{
public WebhookRemoteMovie RemoteMovie { get; set; }
public WebhookMovieFile MovieFile { get; set; }
public bool IsUpgrade { get; set; }
}
}

View File

@ -1,8 +1,10 @@
namespace NzbDrone.Core.Notifications.Webhook
using NzbDrone.Common.Http;
namespace NzbDrone.Core.Notifications.Webhook
{
public enum WebhookMethod
{
POST = RestSharp.Method.POST,
PUT = RestSharp.Method.PUT
POST = HttpMethod.POST,
PUT = HttpMethod.PUT
}
}

View File

@ -9,6 +9,7 @@ public class WebhookMovie
public int Id { get; set; }
public string Title { get; set; }
public string FilePath { get; set; }
public string ReleaseDate { get; set; }
public WebhookMovie() { }
@ -16,12 +17,11 @@ public WebhookMovie(Movie movie)
{
Id = movie.Id;
Title = movie.Title;
ReleaseDate = movie.PhysicalReleaseDate().ToString("yyyy-MM-dd");
}
public WebhookMovie(Movie movie, MovieFile movieFile)
public WebhookMovie(Movie movie, MovieFile movieFile) : this(movie)
{
Id = movie.Id;
Title = movie.Title;
FilePath = Path.Combine(movie.Path, movieFile.RelativePath);
}
}

View File

@ -0,0 +1,28 @@
using NzbDrone.Core.MediaFiles;
namespace NzbDrone.Core.Notifications.Webhook
{
class WebhookMovieFile
{
public WebhookMovieFile() { }
public WebhookMovieFile(MovieFile movieFile)
{
Id = movieFile.Id;
RelativePath = movieFile.RelativePath;
Path = movieFile.Path;
Quality = movieFile.Quality.Quality.Name;
QualityVersion = movieFile.Quality.Revision.Version;
ReleaseGroup = movieFile.ReleaseGroup;
SceneName = movieFile.SceneName;
}
public int Id { get; set; }
public string RelativePath { get; set; }
public string Path { get; set; }
public string Quality { get; set; }
public int QualityVersion { get; set; }
public string ReleaseGroup { get; set; }
public string SceneName { get; set; }
}
}

View File

@ -1,11 +1,8 @@
using System.Collections.Generic;
namespace NzbDrone.Core.Notifications.Webhook
{
public class WebhookPayload
{
public string EventType { get; set; }
public WebhookMovie Movie { get; set; }
public WebhookRemoteMovie RemoteMovie { get; set; }
}
}

View File

@ -0,0 +1,41 @@
using NzbDrone.Common.Http;
using NzbDrone.Common.Serializer;
using NzbDrone.Core.Rest;
namespace NzbDrone.Core.Notifications.Webhook
{
public interface IWebhookProxy
{
void SendWebhook(WebhookPayload payload, WebhookSettings settings);
}
class WebhookProxy : IWebhookProxy
{
private readonly IHttpClient _httpClient;
public WebhookProxy(IHttpClient httpClient)
{
_httpClient = httpClient;
}
public void SendWebhook(WebhookPayload body, WebhookSettings settings)
{
try
{
var request = new HttpRequestBuilder(settings.Url)
.Accept(HttpAccept.Json)
.Build();
request.Method = (HttpMethod)settings.Method;
request.Headers.ContentType = "application/json";
request.SetContent(body.ToJson());
_httpClient.Execute(request);
}
catch (RestException ex)
{
throw new WebhookException("Unable to post to webhook: {0}", ex, ex.Message);
}
}
}
}

View File

@ -0,0 +1,27 @@
using NzbDrone.Core.Parser.Model;
using NzbDrone.Core.Qualities;
namespace NzbDrone.Core.Notifications.Webhook
{
public class WebhookRelease
{
public WebhookRelease() { }
public WebhookRelease(QualityModel quality, RemoteMovie remoteMovie)
{
Quality = quality.Quality.Name;
QualityVersion = quality.Revision.Version;
ReleaseGroup = remoteMovie.ParsedMovieInfo.ReleaseGroup;
ReleaseTitle = remoteMovie.Release.Title;
Indexer = remoteMovie.Release.Indexer;
Size = remoteMovie.Release.Size;
}
public string Quality { get; set; }
public int QualityVersion { get; set; }
public string ReleaseGroup { get; set; }
public string ReleaseTitle { get; set; }
public string Indexer { get; set; }
public long Size { get; set; }
}
}

View File

@ -1,22 +0,0 @@
using NzbDrone.Core.Tv;
namespace NzbDrone.Core.Notifications.Webhook
{
public class WebhookSeries
{
public int Id { get; set; }
public string Title { get; set; }
public string Path { get; set; }
public int TvdbId { get; set; }
public WebhookSeries() { }
public WebhookSeries(Series series)
{
Id = series.Id;
Title = series.Title;
Path = series.Path;
TvdbId = series.TvdbId;
}
}
}

View File

@ -1,103 +0,0 @@
using FluentValidation.Results;
using NzbDrone.Core.MediaFiles;
using NzbDrone.Core.Tv;
using NzbDrone.Core.Validation;
using NzbDrone.Core.Rest;
using RestSharp;
using NzbDrone.Core.Qualities;
using NzbDrone.Core.Parser.Model;
using System.Collections.Generic;
namespace NzbDrone.Core.Notifications.Webhook
{
public interface IWebhookService
{
void OnDownload(Movie movie, MovieFile movieFile, WebhookSettings settings);
void OnRename(Movie movie, WebhookSettings settings);
void OnGrab(Movie movie, RemoteMovie remoteMovie, QualityModel quality, WebhookSettings settings);
ValidationFailure Test(WebhookSettings settings);
}
public class WebhookService : IWebhookService
{
public void OnDownload(Movie movie, MovieFile movieFile, WebhookSettings settings)
{
var payload = new WebhookPayload
{
EventType = "Download",
Movie = new WebhookMovie(movie, movieFile),
RemoteMovie = new WebhookRemoteMovie(movie)
};
NotifyWebhook(payload, settings);
}
public void OnRename(Movie movie, WebhookSettings settings)
{
var payload = new WebhookPayload
{
EventType = "Rename",
Movie = new WebhookMovie(movie)
};
NotifyWebhook(payload, settings);
}
public void OnGrab(Movie movie, RemoteMovie remoteMovie, QualityModel quality, WebhookSettings settings)
{
var payload = new WebhookPayload
{
EventType = "Grab",
Movie = new WebhookMovie(movie),
RemoteMovie = new WebhookRemoteMovie(remoteMovie)
};
NotifyWebhook(payload, settings);
}
public void NotifyWebhook(WebhookPayload body, WebhookSettings settings)
{
try {
var client = RestClientFactory.BuildClient(settings.Url);
var request = new RestRequest((Method) settings.Method);
request.RequestFormat = DataFormat.Json;
request.AddBody(body);
client.ExecuteAndValidate(request);
}
catch (RestException ex)
{
throw new WebhookException("Unable to post to webhook: {0}", ex, ex.Message);
}
}
public ValidationFailure Test(WebhookSettings settings)
{
try
{
NotifyWebhook(
new WebhookPayload
{
EventType = "Test",
Movie = new WebhookMovie()
{
Id = 1,
Title = "Test Title",
FilePath = "C:\\testpath",
},
RemoteMovie = new WebhookRemoteMovie(){
ImdbId = "tt012345",
Title = "My Awesome Movie!"
}
},
settings
);
}
catch (WebhookException ex)
{
return new NzbDroneValidationFailure("Url", ex.Message);
}
return null;
}
}
}

View File

@ -986,14 +986,16 @@
<Compile Include="Notifications\Telegram\TelegramSettings.cs" />
<Compile Include="Notifications\Twitter\OAuthToken.cs" />
<Compile Include="Notifications\Twitter\TwitterException.cs" />
<Compile Include="Notifications\Webhook\WebhookEpisode.cs" />
<Compile Include="Notifications\Webhook\WebhookException.cs" />
<Compile Include="Notifications\Webhook\WebhookGrabPayload.cs" />
<Compile Include="Notifications\Webhook\WebhookImportPayload.cs" />
<Compile Include="Notifications\Webhook\WebhookMethod.cs" />
<Compile Include="Notifications\Webhook\WebhookMovieFile.cs" />
<Compile Include="Notifications\Webhook\WebhookPayload.cs" />
<Compile Include="Notifications\Webhook\WebhookSeries.cs" />
<Compile Include="Notifications\Webhook\WebhookProxy.cs" />
<Compile Include="Notifications\Webhook\WebhookRelease.cs" />
<Compile Include="Notifications\Webhook\WebhookMovie.cs" />
<Compile Include="Notifications\Webhook\WebhookRemoteMovie.cs" />
<Compile Include="Notifications\Webhook\WebhookService.cs" />
<Compile Include="Notifications\Webhook\WebhookSettings.cs" />
<Compile Include="Notifications\Webhook\Webhook.cs" />
<Compile Include="Organizer\NamingConfigRepository.cs" />