From 45cf774adabc0f73d5048e0e3d86bd1fd1c6355f Mon Sep 17 00:00:00 2001 From: Robert Dailey Date: Wed, 26 May 2021 22:20:16 -0500 Subject: [PATCH] feat(radarr): support trash_score in custom format json Trash has started putting scores in the actual importable JSON data. If a score is found in the JSON data, that takes precedence over any score parsed from the guide markdown. The `trash_score` field is not required for backward compatibility. --- CHANGELOG.md | 5 +++ .../GuideSteps/CustomFormatStepTest.cs | 37 +++++++++++++++++++ .../Processors/GuideSteps/CustomFormatStep.cs | 17 +++++++-- 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index af0c0fe4..ef424262 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Changed + +- Support `trash_score` property in Custom Format JSON from the guide. This property is optional and + takes precedence over a score mentioned in the guide markdown. + ## [1.5.0] - 2021-05-16 ### Added diff --git a/src/Trash.Tests/Radarr/CustomFormat/Processors/GuideSteps/CustomFormatStepTest.cs b/src/Trash.Tests/Radarr/CustomFormat/Processors/GuideSteps/CustomFormatStepTest.cs index badb58dd..19f2daf1 100644 --- a/src/Trash.Tests/Radarr/CustomFormat/Processors/GuideSteps/CustomFormatStepTest.cs +++ b/src/Trash.Tests/Radarr/CustomFormat/Processors/GuideSteps/CustomFormatStepTest.cs @@ -378,5 +378,42 @@ namespace Trash.Tests.Radarr.CustomFormat.Processors.GuideSteps processor.DeletedCustomFormatsInCache.Should().BeEmpty(); processor.ProcessedCustomFormats.Should().BeEmpty(); } + + [Test] + public void Score_from_json_takes_precedence_over_score_from_guide() + { + var guideData = new List + { + new() {Json = @"{'name': 'name1', 'trash_id': 'id1', 'trash_score': 100}"} + }; + + var testConfig = new List + { + new() + { + Names = new List {"name1"}, + QualityProfiles = new List + { + new() {Name = "profile", Score = 200} + } + } + }; + + var processor = new CustomFormatStep(); + processor.Process(guideData, testConfig, null); + + processor.DuplicatedCustomFormats.Should().BeEmpty(); + processor.CustomFormatsWithOutdatedNames.Should().BeEmpty(); + processor.DeletedCustomFormatsInCache.Should().BeEmpty(); + processor.ProcessedCustomFormats.Should() + .BeEquivalentTo(new List + { + new("name1", "id1", JObject.FromObject(new {name = "name1"})) + { + Score = 100 + } + }, + op => op.Using(new JsonEquivalencyStep())); + } } } diff --git a/src/Trash/Radarr/CustomFormat/Processors/GuideSteps/CustomFormatStep.cs b/src/Trash/Radarr/CustomFormat/Processors/GuideSteps/CustomFormatStep.cs index 8564a9b8..2e1ee5d9 100644 --- a/src/Trash/Radarr/CustomFormat/Processors/GuideSteps/CustomFormatStep.cs +++ b/src/Trash/Radarr/CustomFormat/Processors/GuideSteps/CustomFormatStep.cs @@ -97,8 +97,19 @@ namespace Trash.Radarr.CustomFormat.Processors.GuideSteps CustomFormatCache? cache) { JObject obj = JObject.Parse(guideData.Json); - var name = obj["name"].Value(); - var trashId = obj["trash_id"].Value(); + var name = (string) obj["name"]; + var trashId = (string) obj["trash_id"]; + int? finalScore; + + if (obj.TryGetValue("trash_score", out var score)) + { + finalScore = (int) score; + obj.Property("trash_score").Remove(); + } + else + { + finalScore = guideData.Score; + } // Remove trash_id, it's metadata that is not meant for Radarr itself // Radarr supposedly drops this anyway, but I prefer it to be removed by TrashUpdater @@ -106,7 +117,7 @@ namespace Trash.Radarr.CustomFormat.Processors.GuideSteps return new ProcessedCustomFormatData(name, trashId, obj) { - Score = guideData.Score, + Score = finalScore, CacheEntry = cache?.TrashIdMappings.FirstOrDefault(c => c.TrashId == trashId) }; }