1
0
mirror of https://github.com/Radarr/Radarr.git synced 2026-04-19 21:46:50 -04:00

New: Better interface for creating custom formats

This commit is contained in:
ta264
2020-02-18 21:03:05 +00:00
committed by Qstick
parent a5bac30ef3
commit 241bf85f15
74 changed files with 2259 additions and 1090 deletions
@@ -1,9 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using FluentValidation;
using Nancy;
using NzbDrone.Core.CustomFormats;
using NzbDrone.Core.Parser;
using Radarr.Http;
namespace Radarr.Api.V3.CustomFormats
@@ -11,34 +10,18 @@ namespace Radarr.Api.V3.CustomFormats
public class CustomFormatModule : RadarrRestModule<CustomFormatResource>
{
private readonly ICustomFormatService _formatService;
private readonly ICustomFormatCalculationService _formatCalculator;
private readonly IParsingService _parsingService;
private readonly List<ICustomFormatSpecification> _specifications;
public CustomFormatModule(ICustomFormatService formatService,
ICustomFormatCalculationService formatCalculator,
IParsingService parsingService)
List<ICustomFormatSpecification> specifications)
{
_formatService = formatService;
_formatCalculator = formatCalculator;
_parsingService = parsingService;
_specifications = specifications;
SharedValidator.RuleFor(c => c.Name).NotEmpty();
SharedValidator.RuleFor(c => c.Name)
.Must((v, c) => !_formatService.All().Any(f => f.Name == c && f.Id != v.Id)).WithMessage("Must be unique.");
SharedValidator.RuleFor(c => c.FormatTags).SetValidator(new FormatTagValidator());
SharedValidator.RuleFor(c => c.FormatTags).Must((v, c) =>
{
var allFormats = _formatService.All();
return !allFormats.Any(f =>
{
var allTags = f.FormatTags.Select(t => t.Raw.ToLower());
var allNewTags = c.Split(',').Select(t => t.ToLower());
var enumerable = allTags.ToList();
var newTags = allNewTags.ToList();
return enumerable.All(newTags.Contains) && f.Id != v.Id && enumerable.Count == newTags.Count;
});
})
.WithMessage("Should be unique.");
SharedValidator.RuleFor(c => c.Specifications).NotEmpty();
GetResourceAll = GetAll;
@@ -50,22 +33,18 @@ namespace Radarr.Api.V3.CustomFormats
DeleteResource = DeleteFormat;
Get("/test", x => Test());
Post("/test", x => TestWithNewModel());
Get("schema", x => GetTemplates());
}
private int Create(CustomFormatResource customFormatResource)
{
var model = customFormatResource.ToModel();
var model = customFormatResource.ToModel(_specifications);
return _formatService.Insert(model).Id;
}
private void Update(CustomFormatResource resource)
{
var model = resource.ToModel();
var model = resource.ToModel(_specifications);
_formatService.Update(model);
}
@@ -86,52 +65,66 @@ namespace Radarr.Api.V3.CustomFormats
private object GetTemplates()
{
return CustomFormatService.Templates.SelectMany(t =>
var schema = _specifications.OrderBy(x => x.Order).Select(x => x.ToSchema()).ToList();
var presets = GetPresets();
foreach (var item in schema)
{
return t.Value.Select(m =>
item.Presets = presets.Where(x => x.GetType().Name == item.Implementation).Select(x => x.ToSchema()).ToList();
}
return schema;
}
private IEnumerable<ICustomFormatSpecification> GetPresets()
{
yield return new ReleaseTitleSpecification
{
Name = "x264",
Value = @"(x|h)\.?264"
};
yield return new ReleaseTitleSpecification
{
Name = "x265",
Value = @"(((x|h)\.?265)|(HEVC))"
};
yield return new ReleaseTitleSpecification
{
Name = "Simple Hardcoded Subs",
Value = @"C_RX_subs?"
};
yield return new ReleaseTitleSpecification
{
Name = "Hardcoded Subs",
Value = @"\b(?<hcsub>(\w+SUBS?)\b)|(?<hc>(HC|SUBBED))\b"
};
yield return new ReleaseTitleSpecification
{
Name = "Surround Sound",
Value = @"DTS.?(HD|ES|X(?!\D))|TRUEHD|ATMOS|DD(\+|P).?([5-9])|EAC3.?([5-9])"
};
yield return new ReleaseTitleSpecification
{
Name = "Preferred Words",
Value = @"\b(SPARKS|Framestor)\b"
};
var formats = _formatService.All();
foreach (var format in formats)
{
foreach (var condition in format.Specifications)
{
var r = m.ToResource();
r.Simplicity = t.Key;
return r;
});
});
}
private CustomFormatTestResource Test()
{
var parsed = _parsingService.ParseMovieInfo((string)Request.Query.title, new List<object>());
if (parsed == null)
{
return null;
var preset = condition.Clone();
preset.Name = $"{format.Name}: {preset.Name}";
yield return preset;
}
}
return new CustomFormatTestResource
{
Matches = _formatCalculator.MatchFormatTags(parsed).ToResource(),
MatchedFormats = _formatCalculator.ParseCustomFormat(parsed).ToResource()
};
}
private CustomFormatTestResource TestWithNewModel()
{
var queryTitle = (string)Request.Query.title;
var resource = ReadResourceFromRequest();
var model = resource.ToModel();
model.Name = model.Name += " (New)";
var parsed = _parsingService.ParseMovieInfo(queryTitle, new List<object> { model });
if (parsed == null)
{
return null;
}
return new CustomFormatTestResource
{
Matches = _formatCalculator.MatchFormatTags(parsed).ToResource(),
MatchedFormats = _formatCalculator.ParseCustomFormat(parsed).ToResource()
};
}
}
}