This sets the groundwork for making Radarr guide data available for other usages beyond syncing to Radarr, such as spitting out information to the console.pull/92/head
parent
87830303ae
commit
9eebc227c5
@ -0,0 +1,46 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using TrashLib.Radarr.CustomFormat.Models;
|
||||
using TrashLib.Radarr.CustomFormat.Models.Cache;
|
||||
|
||||
namespace TrashLib.TestLibrary;
|
||||
|
||||
public static class NewCf
|
||||
{
|
||||
public static CustomFormatData Data(string name, string trashId, int? score = null)
|
||||
{
|
||||
var json = JObject.Parse($"{{'name':'{name}'}}");
|
||||
return new CustomFormatData(name, trashId, score, new JObject(json));
|
||||
}
|
||||
|
||||
public static ProcessedCustomFormatData Processed(string name, string trashId, int? score = null)
|
||||
{
|
||||
return new ProcessedCustomFormatData(Data(name, trashId, score));
|
||||
}
|
||||
|
||||
public static ProcessedCustomFormatData Processed(string name, string trashId, int? score, JObject json)
|
||||
{
|
||||
return new ProcessedCustomFormatData(new CustomFormatData(name, trashId, score, json));
|
||||
}
|
||||
|
||||
public static ProcessedCustomFormatData Processed(string name, string trashId, JObject json)
|
||||
{
|
||||
return Processed(name, trashId, null, json);
|
||||
}
|
||||
|
||||
public static ProcessedCustomFormatData Processed(string name, string trashId, TrashIdMapping cacheEntry)
|
||||
{
|
||||
return new ProcessedCustomFormatData(Data(name, trashId))
|
||||
{
|
||||
CacheEntry = cacheEntry
|
||||
};
|
||||
}
|
||||
|
||||
public static ProcessedCustomFormatData Processed(string name, string trashId, JObject json,
|
||||
TrashIdMapping? cacheEntry)
|
||||
{
|
||||
return new ProcessedCustomFormatData(new CustomFormatData(name, trashId, null, json))
|
||||
{
|
||||
CacheEntry = cacheEntry
|
||||
};
|
||||
}
|
||||
}
|
@ -1,6 +1,8 @@
|
||||
using TrashLib.Radarr.CustomFormat.Models;
|
||||
|
||||
namespace TrashLib.Radarr.CustomFormat.Guide;
|
||||
|
||||
public interface IRadarrGuideService
|
||||
{
|
||||
IEnumerable<string> GetCustomFormatJson();
|
||||
IEnumerable<CustomFormatData> GetCustomFormatData();
|
||||
}
|
||||
|
@ -1,24 +1,72 @@
|
||||
using System.IO.Abstractions;
|
||||
using System.Reactive.Linq;
|
||||
using System.Reactive.Threading.Tasks;
|
||||
using Common.Extensions;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Serilog;
|
||||
using TrashLib.Radarr.CustomFormat.Models;
|
||||
|
||||
namespace TrashLib.Radarr.CustomFormat.Guide;
|
||||
|
||||
public class LocalRepoCustomFormatJsonParser : IRadarrGuideService
|
||||
{
|
||||
private readonly IFileSystem _fileSystem;
|
||||
private readonly IFileSystem _fs;
|
||||
private readonly IAppPaths _paths;
|
||||
private readonly ILogger _log;
|
||||
|
||||
public LocalRepoCustomFormatJsonParser(IFileSystem fileSystem, IAppPaths paths)
|
||||
public LocalRepoCustomFormatJsonParser(IFileSystem fs, IAppPaths paths, ILogger log)
|
||||
{
|
||||
_fileSystem = fileSystem;
|
||||
_fs = fs;
|
||||
_paths = paths;
|
||||
_log = log;
|
||||
}
|
||||
|
||||
public IEnumerable<string> GetCustomFormatJson()
|
||||
public IEnumerable<CustomFormatData> GetCustomFormatData()
|
||||
{
|
||||
var jsonDir = Path.Combine(_paths.RepoDirectory, "docs/json/radarr");
|
||||
var tasks = _fileSystem.Directory.GetFiles(jsonDir, "*.json")
|
||||
.Select(f => _fileSystem.File.ReadAllTextAsync(f));
|
||||
var jsonDir = _fs.DirectoryInfo.FromDirectoryName(_paths.RepoDirectory)
|
||||
.SubDirectory("docs")
|
||||
.SubDirectory("json")
|
||||
.SubDirectory("radarr");
|
||||
|
||||
return Task.WhenAll(tasks).Result;
|
||||
return jsonDir.EnumerateFiles("*.json").ToObservable()
|
||||
.Select(x => Observable.Defer(() => LoadJsonFromFile(x)))
|
||||
.Merge(8)
|
||||
.NotNull()
|
||||
.ToEnumerable()
|
||||
.ToList();
|
||||
}
|
||||
|
||||
private IObservable<CustomFormatData?> LoadJsonFromFile(IFileInfo file)
|
||||
{
|
||||
return Observable.Using(file.OpenText, x => x.ReadToEndAsync().ToObservable())
|
||||
.Do(_ => _log.Debug("Parsing CF Json: {Name}", file.Name))
|
||||
.Select(ParseCustomFormatData)
|
||||
.Catch((JsonException e) =>
|
||||
{
|
||||
_log.Warning("Failed to parse JSON file: {File} ({Reason})", file.Name, e.Message);
|
||||
return Observable.Empty<CustomFormatData>();
|
||||
});
|
||||
}
|
||||
|
||||
public static CustomFormatData ParseCustomFormatData(string guideData)
|
||||
{
|
||||
var obj = JObject.Parse(guideData);
|
||||
|
||||
var name = obj.ValueOrThrow<string>("name");
|
||||
var trashId = obj.ValueOrThrow<string>("trash_id");
|
||||
int? finalScore = null;
|
||||
|
||||
if (obj.TryGetValue("trash_score", out var score))
|
||||
{
|
||||
finalScore = (int) score;
|
||||
obj.Property("trash_score")?.Remove();
|
||||
}
|
||||
|
||||
// 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.
|
||||
obj.Property("trash_id")?.Remove();
|
||||
|
||||
return new CustomFormatData(name, trashId, finalScore, obj);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,11 @@
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace TrashLib.Radarr.CustomFormat.Models;
|
||||
|
||||
public record CustomFormatData(
|
||||
string Name,
|
||||
string TrashId,
|
||||
int? Score,
|
||||
[property: JsonExtensionData] JObject ExtraJson
|
||||
);
|
Loading…
Reference in new issue