From 1072c5247c1d983afdc91e756564ab2377df1120 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Tue, 15 Mar 2016 15:43:21 -0700 Subject: [PATCH] On grab for custom scripts New: On Grab handling for Custom Scripts --- .../CustomScript/CustomScript.cs | 91 +++++++++++++++--- .../CustomScript/CustomScriptService.cs | 94 ------------------- .../Notifications/GrabMessage.cs | 7 +- src/NzbDrone.Core/NzbDrone.Core.csproj | 1 - 4 files changed, 81 insertions(+), 112 deletions(-) delete mode 100644 src/NzbDrone.Core/Notifications/CustomScript/CustomScriptService.cs diff --git a/src/NzbDrone.Core/Notifications/CustomScript/CustomScript.cs b/src/NzbDrone.Core/Notifications/CustomScript/CustomScript.cs index 17446c9ac..2cd0b33b7 100644 --- a/src/NzbDrone.Core/Notifications/CustomScript/CustomScript.cs +++ b/src/NzbDrone.Core/Notifications/CustomScript/CustomScript.cs @@ -1,16 +1,27 @@ using System.Collections.Generic; +using System.Collections.Specialized; +using System.IO; +using System.Linq; using FluentValidation.Results; +using NLog; +using NzbDrone.Common.Disk; +using NzbDrone.Common.Processes; using NzbDrone.Core.Tv; +using NzbDrone.Core.Validation; namespace NzbDrone.Core.Notifications.CustomScript { public class CustomScript : NotificationBase { - private readonly ICustomScriptService _customScriptService; + private readonly IDiskProvider _diskProvider; + private readonly IProcessProvider _processProvider; + private readonly Logger _logger; - public CustomScript(ICustomScriptService customScriptService) + public CustomScript(IDiskProvider diskProvider, IProcessProvider processProvider, Logger logger) { - _customScriptService = customScriptService; + _diskProvider = diskProvider; + _processProvider = processProvider; + _logger = logger; } public override string Link @@ -18,18 +29,67 @@ namespace NzbDrone.Core.Notifications.CustomScript get { return "https://github.com/Sonarr/Sonarr/wiki/Custom-Post-Processing-Scripts"; } } - public override void OnGrab(GrabMessage grabMessage) + public override void OnGrab(GrabMessage message) { + var series = message.Series; + var remoteEpisode = message.Episode; + var releaseGroup = remoteEpisode.ParsedEpisodeInfo.ReleaseGroup; + var environmentVariables = new StringDictionary(); + + environmentVariables.Add("Sonarr_EventType", "Grab"); + environmentVariables.Add("Sonarr_Series_Id", series.Id.ToString()); + environmentVariables.Add("Sonarr_Series_Title", series.Title); + environmentVariables.Add("Sonarr_Series_TvdbId", series.TvdbId.ToString()); + environmentVariables.Add("Sonarr_Release_SeasonNumber", remoteEpisode.ParsedEpisodeInfo.SeasonNumber.ToString()); + environmentVariables.Add("Sonarr_Release_EpisodeNumbers", string.Join(",", remoteEpisode.Episodes.Select(e => e.EpisodeNumber))); + environmentVariables.Add("Sonarr_Release_Title", remoteEpisode.Release.Title); + environmentVariables.Add("Sonarr_Release_Indexer", remoteEpisode.Release.Indexer); + environmentVariables.Add("Sonarr_Release_Size", remoteEpisode.Release.Size.ToString()); + environmentVariables.Add("Sonarr_Release_ReleaseGroup", releaseGroup); + + ExecuteScript(environmentVariables); } public override void OnDownload(DownloadMessage message) { - _customScriptService.OnDownload(message.Series, message.EpisodeFile, message.SourcePath, Settings); + var series = message.Series; + var episodeFile = message.EpisodeFile; + var sourcePath = message.SourcePath; + var environmentVariables = new StringDictionary(); + + environmentVariables.Add("Sonarr_EventType", "Download"); + environmentVariables.Add("Sonarr_Series_Id", series.Id.ToString()); + environmentVariables.Add("Sonarr_Series_Title", series.Title); + environmentVariables.Add("Sonarr_Series_Path", series.Path); + environmentVariables.Add("Sonarr_Series_TvdbId", series.TvdbId.ToString()); + environmentVariables.Add("Sonarr_EpisodeFile_Id", episodeFile.Id.ToString()); + environmentVariables.Add("Sonarr_EpisodeFile_RelativePath", episodeFile.RelativePath); + environmentVariables.Add("Sonarr_EpisodeFile_Path", Path.Combine(series.Path, episodeFile.RelativePath)); + environmentVariables.Add("Sonarr_EpisodeFile_SeasonNumber", episodeFile.SeasonNumber.ToString()); + environmentVariables.Add("Sonarr_EpisodeFile_EpisodeNumbers", string.Join(",", episodeFile.Episodes.Value.Select(e => e.EpisodeNumber))); + environmentVariables.Add("Sonarr_EpisodeFile_EpisodeAirDates", string.Join(",", episodeFile.Episodes.Value.Select(e => e.AirDate))); + environmentVariables.Add("Sonarr_EpisodeFile_EpisodeAirDatesUtc", string.Join(",", episodeFile.Episodes.Value.Select(e => e.AirDateUtc))); + environmentVariables.Add("Sonarr_EpisodeFile_Quality", episodeFile.Quality.Quality.Name); + environmentVariables.Add("Sonarr_EpisodeFile_QualityVersion", episodeFile.Quality.Revision.Version.ToString()); + environmentVariables.Add("Sonarr_EpisodeFile_ReleaseGroup", episodeFile.ReleaseGroup ?? string.Empty); + environmentVariables.Add("Sonarr_EpisodeFile_SceneName", episodeFile.SceneName ?? string.Empty); + environmentVariables.Add("Sonarr_EpisodeFile_SourcePath", sourcePath); + environmentVariables.Add("Sonarr_EpisodeFile_SourceFolder", Path.GetDirectoryName(sourcePath)); + + ExecuteScript(environmentVariables); } public override void OnRename(Series series) { - _customScriptService.OnRename(series, Settings); + var environmentVariables = new StringDictionary(); + + environmentVariables.Add("Sonarr_EventType", "Rename"); + environmentVariables.Add("Sonarr_Series_Id", series.Id.ToString()); + environmentVariables.Add("Sonarr_Series_Title", series.Title); + environmentVariables.Add("Sonarr_Series_Path", series.Path); + environmentVariables.Add("Sonarr_Series_TvdbId", series.TvdbId.ToString()); + + ExecuteScript(environmentVariables); } public override string Name @@ -40,19 +100,26 @@ namespace NzbDrone.Core.Notifications.CustomScript } } - public override bool SupportsOnGrab + public override ValidationResult Test() { - get + var failures = new List(); + + if (!_diskProvider.FileExists(Settings.Path)) { - return false; + failures.Add(new NzbDroneValidationFailure("Path", "File does not exist")); } + + return new ValidationResult(failures); } - public override ValidationResult Test() + private void ExecuteScript(StringDictionary environmentVariables) { - var failures = new List(); + _logger.Debug("Executing external script: {0}", Settings.Path); - return new ValidationResult(failures); + var process = _processProvider.StartAndCapture(Settings.Path, Settings.Arguments, environmentVariables); + + _logger.Debug("Executed external script: {0} - Status: {1}", Settings.Path, process.ExitCode); + _logger.Debug("Script Output: \r\n{0}", string.Join("\r\n", process.Lines)); } } } diff --git a/src/NzbDrone.Core/Notifications/CustomScript/CustomScriptService.cs b/src/NzbDrone.Core/Notifications/CustomScript/CustomScriptService.cs deleted file mode 100644 index 964ba30d6..000000000 --- a/src/NzbDrone.Core/Notifications/CustomScript/CustomScriptService.cs +++ /dev/null @@ -1,94 +0,0 @@ -using System; -using System.Collections.Specialized; -using System.IO; -using System.Linq; -using FluentValidation.Results; -using NLog; -using NzbDrone.Common.Disk; -using NzbDrone.Common.Processes; -using NzbDrone.Core.MediaFiles; -using NzbDrone.Core.Tv; -using NzbDrone.Core.Validation; - -namespace NzbDrone.Core.Notifications.CustomScript -{ - public interface ICustomScriptService - { - void OnDownload(Series series, EpisodeFile episodeFile, string sourcePath, CustomScriptSettings settings); - void OnRename(Series series, CustomScriptSettings settings); - ValidationFailure Test(CustomScriptSettings settings); - } - - public class CustomScriptService : ICustomScriptService - { - private readonly IProcessProvider _processProvider; - private readonly IDiskProvider _diskProvider; - private readonly Logger _logger; - - public CustomScriptService(IProcessProvider processProvider, IDiskProvider diskProvider, Logger logger) - { - _processProvider = processProvider; - _diskProvider = diskProvider; - _logger = logger; - } - - public void OnDownload(Series series, EpisodeFile episodeFile, string sourcePath, CustomScriptSettings settings) - { - var environmentVariables = new StringDictionary(); - - environmentVariables.Add("Sonarr_EventType", "Download"); - environmentVariables.Add("Sonarr_Series_Id", series.Id.ToString()); - environmentVariables.Add("Sonarr_Series_Title", series.Title); - environmentVariables.Add("Sonarr_Series_Path", series.Path); - environmentVariables.Add("Sonarr_Series_TvdbId", series.TvdbId.ToString()); - environmentVariables.Add("Sonarr_EpisodeFile_Id", episodeFile.Id.ToString()); - environmentVariables.Add("Sonarr_EpisodeFile_RelativePath", episodeFile.RelativePath); - environmentVariables.Add("Sonarr_EpisodeFile_Path", Path.Combine(series.Path, episodeFile.RelativePath)); - environmentVariables.Add("Sonarr_EpisodeFile_SeasonNumber", episodeFile.SeasonNumber.ToString()); - environmentVariables.Add("Sonarr_EpisodeFile_EpisodeNumbers", string.Join(",", episodeFile.Episodes.Value.Select(e => e.EpisodeNumber))); - environmentVariables.Add("Sonarr_EpisodeFile_EpisodeAirDates", string.Join(",", episodeFile.Episodes.Value.Select(e => e.AirDate))); - environmentVariables.Add("Sonarr_EpisodeFile_EpisodeAirDatesUtc", string.Join(",", episodeFile.Episodes.Value.Select(e => e.AirDateUtc))); - environmentVariables.Add("Sonarr_EpisodeFile_Quality", episodeFile.Quality.Quality.Name); - environmentVariables.Add("Sonarr_EpisodeFile_QualityVersion", episodeFile.Quality.Revision.Version.ToString()); - environmentVariables.Add("Sonarr_EpisodeFile_ReleaseGroup", episodeFile.ReleaseGroup ?? string.Empty); - environmentVariables.Add("Sonarr_EpisodeFile_SceneName", episodeFile.SceneName ?? string.Empty); - environmentVariables.Add("Sonarr_EpisodeFile_SourcePath", sourcePath); - environmentVariables.Add("Sonarr_EpisodeFile_SourceFolder", Path.GetDirectoryName(sourcePath)); - - ExecuteScript(environmentVariables, settings); - } - - public void OnRename(Series series, CustomScriptSettings settings) - { - var environmentVariables = new StringDictionary(); - - environmentVariables.Add("Sonarr_EventType", "Rename"); - environmentVariables.Add("Sonarr_Series_Id", series.Id.ToString()); - environmentVariables.Add("Sonarr_Series_Title", series.Title); - environmentVariables.Add("Sonarr_Series_Path", series.Path); - environmentVariables.Add("Sonarr_Series_TvdbId", series.TvdbId.ToString()); - - ExecuteScript(environmentVariables, settings); - } - - public ValidationFailure Test(CustomScriptSettings settings) - { - if (!_diskProvider.FileExists(settings.Path)) - { - return new NzbDroneValidationFailure("Path", "File does not exist"); - } - - return null; - } - - private void ExecuteScript(StringDictionary environmentVariables, CustomScriptSettings settings) - { - _logger.Debug("Executing external script: {0}", settings.Path); - - var process = _processProvider.StartAndCapture(settings.Path, settings.Arguments, environmentVariables); - - _logger.Debug("Executed external script: {0} - Status: {1}", settings.Path, process.ExitCode); - _logger.Debug("Script Output: \r\n{0}", string.Join("\r\n", process.Lines)); - } - } -} diff --git a/src/NzbDrone.Core/Notifications/GrabMessage.cs b/src/NzbDrone.Core/Notifications/GrabMessage.cs index 6984040ae..e62dbe701 100644 --- a/src/NzbDrone.Core/Notifications/GrabMessage.cs +++ b/src/NzbDrone.Core/Notifications/GrabMessage.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; -using NzbDrone.Core.MediaFiles; -using NzbDrone.Core.Parser.Model; +using NzbDrone.Core.Parser.Model; using NzbDrone.Core.Qualities; using NzbDrone.Core.Tv; @@ -12,7 +9,7 @@ namespace NzbDrone.Core.Notifications public string Message { get; set; } public Series Series { get; set; } public RemoteEpisode Episode { get; set; } - public QualityModel Quality { get; set; } + public QualityModel Quality { get; set; } public override string ToString() { diff --git a/src/NzbDrone.Core/NzbDrone.Core.csproj b/src/NzbDrone.Core/NzbDrone.Core.csproj index eeb1bd094..ae947c2f4 100644 --- a/src/NzbDrone.Core/NzbDrone.Core.csproj +++ b/src/NzbDrone.Core/NzbDrone.Core.csproj @@ -772,7 +772,6 @@ -