mirror of
https://github.com/Radarr/Radarr.git
synced 2026-04-23 22:25:14 -04:00
Added: Ability to add custom formats, working similar to qualities. (#2669)
Originally called project metis, this feature allows you to do a lot of cool stuff, such as upgrading to a x265 encode, downloading releases with multiple languages, etc. Check out the wiki page at: https://github.com/Radarr/Radarr/wiki/Custom-Formats to learn more! Note: This feature is currently in "beta" and will get more tags and features in the future. Please let me know, if you have any issues and I hope this will allow for a lot of customization!
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using FluentValidation;
|
||||
using Nancy;
|
||||
using NzbDrone.Api.Extensions;
|
||||
using NzbDrone.Core.CustomFormats;
|
||||
using NzbDrone.Core.Parser;
|
||||
|
||||
namespace NzbDrone.Api.Qualities
|
||||
{
|
||||
public class CustomFormatModule : NzbDroneRestModule<CustomFormatResource>
|
||||
{
|
||||
private readonly ICustomFormatService _formatService;
|
||||
private readonly IParsingService _parsingService;
|
||||
|
||||
public CustomFormatModule(ICustomFormatService formatService, IParsingService parsingService)
|
||||
{
|
||||
_formatService = formatService;
|
||||
_parsingService = parsingService;
|
||||
|
||||
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).Must((v, c) => c.All(s => FormatTag.QualityTagRegex.IsMatch(s))).WithMessage("Invalid format.");
|
||||
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.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.");
|
||||
|
||||
GetResourceAll = GetAll;
|
||||
|
||||
GetResourceById = GetById;
|
||||
|
||||
UpdateResource = Update;
|
||||
|
||||
CreateResource = Create;
|
||||
|
||||
Get["/test"] = x => Test();
|
||||
|
||||
Post["/test"] = x => TestWithNewModel();
|
||||
|
||||
Get["schema"] = x => GetTemplates();
|
||||
}
|
||||
|
||||
private int Create(CustomFormatResource customFormatResource)
|
||||
{
|
||||
var model = customFormatResource.ToModel();
|
||||
return _formatService.Insert(model).Id;
|
||||
}
|
||||
|
||||
private void Update(CustomFormatResource resource)
|
||||
{
|
||||
var model = resource.ToModel();
|
||||
_formatService.Update(model);
|
||||
}
|
||||
|
||||
private CustomFormatResource GetById(int id)
|
||||
{
|
||||
return _formatService.GetById(id).ToResource();
|
||||
}
|
||||
|
||||
private List<CustomFormatResource> GetAll()
|
||||
{
|
||||
return _formatService.All().ToResource();
|
||||
}
|
||||
|
||||
private Response GetTemplates()
|
||||
{
|
||||
return CustomFormatService.Templates.SelectMany(t =>
|
||||
{
|
||||
return t.Value.Select(m =>
|
||||
{
|
||||
var r = m.ToResource();
|
||||
r.Simplicity = t.Key;
|
||||
return r;
|
||||
});
|
||||
}).AsResponse();
|
||||
}
|
||||
|
||||
private CustomFormatTestResource Test()
|
||||
{
|
||||
var parsed = _parsingService.ParseMovieInfo((string) Request.Query.title, new List<object>());
|
||||
if (parsed == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new CustomFormatTestResource
|
||||
{
|
||||
Matches = _parsingService.MatchFormatTags(parsed).ToResource(),
|
||||
MatchedFormats = parsed.Quality.CustomFormats.ToResource()
|
||||
};
|
||||
}
|
||||
|
||||
private CustomFormatTestResource TestWithNewModel()
|
||||
{
|
||||
var queryTitle = (string) Request.Query.title;
|
||||
|
||||
var resource = ReadResourceFromRequest();
|
||||
|
||||
var model = resource.ToModel();
|
||||
|
||||
var parsed = _parsingService.ParseMovieInfo((string) Request.Query.title, new List<object>{model});
|
||||
if (parsed == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new CustomFormatTestResource
|
||||
{
|
||||
Matches = _parsingService.MatchFormatTags(parsed).ToResource(),
|
||||
MatchedFormats = parsed.Quality.CustomFormats.ToResource()
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user