1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2026-04-23 22:25:56 -04:00

Custom scripts

New: Run custom scripts (Connection)

Closes #439
This commit is contained in:
Mark McDowall
2015-05-20 16:22:10 -07:00
parent 492b114510
commit 0f2bba0615
42 changed files with 560 additions and 74 deletions
@@ -0,0 +1,58 @@
using System.Collections.Generic;
using FluentValidation.Results;
using NzbDrone.Core.Tv;
namespace NzbDrone.Core.Notifications.CustomScript
{
public class CustomScript : NotificationBase<CustomScriptSettings>
{
private readonly ICustomScriptService _customScriptService;
public CustomScript(ICustomScriptService customScriptService)
{
_customScriptService = customScriptService;
}
public override string Link
{
get { return "https://github.com/Sonarr/Sonarr/wiki/Custom-Post-Processing-Scripts"; }
}
public override void OnGrab(string message)
{
}
public override void OnDownload(DownloadMessage message)
{
_customScriptService.OnDownload(message.Series, message.EpisodeFile, Settings);
}
public override void OnRename(Series series)
{
_customScriptService.OnRename(series, Settings);
}
public override string Name
{
get
{
return "Custom Script";
}
}
public override bool SupportsOnGrab
{
get
{
return false;
}
}
public override ValidationResult Test()
{
var failures = new List<ValidationFailure>();
return new ValidationResult(failures);
}
}
}
@@ -0,0 +1,92 @@
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, 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, 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);
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));
}
}
}
@@ -0,0 +1,33 @@
using System;
using FluentValidation;
using NzbDrone.Core.Annotations;
using NzbDrone.Core.ThingiProvider;
using NzbDrone.Core.Validation;
using NzbDrone.Core.Validation.Paths;
namespace NzbDrone.Core.Notifications.CustomScript
{
public class CustomScriptSettingsValidator : AbstractValidator<CustomScriptSettings>
{
public CustomScriptSettingsValidator()
{
RuleFor(c => c.Path).IsValidPath();
}
}
public class CustomScriptSettings : IProviderConfig
{
private static readonly CustomScriptSettingsValidator Validator = new CustomScriptSettingsValidator();
[FieldDefinition(0, Label = "Path", Type = FieldType.Path)]
public String Path { get; set; }
[FieldDefinition(0, Label = "Arguments", HelpText = "Arguments to pass to the script")]
public String Arguments { get; set; }
public NzbDroneValidationResult Validate()
{
return new NzbDroneValidationResult(Validator.Validate(this));
}
}
}