From e0e7abb62b4be17bbf23e430338c8fb843f041a7 Mon Sep 17 00:00:00 2001 From: glax Date: Sat, 3 Jun 2023 15:32:54 +0200 Subject: [PATCH] #33 Added custom serializer for LibraryManager Added Code for Kavita --- Tranga-CLI/Tranga_Cli.cs | 49 +++++++++++++++++++++----- Tranga/LibraryManager.cs | 40 +++++++++++++++++++++ Tranga/LibraryManagers/Kavita.cs | 60 ++++++++++++++++++++++++++++++-- Tranga/LibraryManagers/Komga.cs | 2 ++ Tranga/TrangaSettings.cs | 2 +- 5 files changed, 142 insertions(+), 11 deletions(-) diff --git a/Tranga-CLI/Tranga_Cli.cs b/Tranga-CLI/Tranga_Cli.cs index f845a8b..fdaa42f 100644 --- a/Tranga-CLI/Tranga_Cli.cs +++ b/Tranga-CLI/Tranga_Cli.cs @@ -40,13 +40,11 @@ public static class Tranga_Cli if (tmpPath.Length > 0) settings.downloadLocation = tmpPath; - Komga? komga = (Komga?)settings.libraryManagers.FirstOrDefault(lm => lm.GetType() == typeof(Komga)); - - Console.WriteLine($"Komga BaseURL [{komga?.baseUrl}]:"); - string? tmpUrl = Console.ReadLine(); - while (tmpUrl is null) - tmpUrl = Console.ReadLine(); - if (tmpUrl.Length > 0) + Console.WriteLine($"Komga BaseURL [{settings.libraryManagers.FirstOrDefault(lm => lm.GetType() == typeof(Komga))?.baseUrl}]:"); + string? tmpUrlKomga = Console.ReadLine(); + while (tmpUrlKomga is null) + tmpUrlKomga = Console.ReadLine(); + if (tmpUrlKomga.Length > 0) { Console.WriteLine("Username:"); string? tmpUser = Console.ReadLine(); @@ -74,7 +72,42 @@ public static class Tranga_Cli } while (key != ConsoleKey.Enter); settings.libraryManagers.RemoveWhere(lm => lm.GetType() == typeof(Komga)); - settings.libraryManagers.Add(new Komga(tmpUrl, tmpUser, tmpPass, logger)); + settings.libraryManagers.Add(new Komga(tmpUrlKomga, tmpUser, tmpPass, logger)); + } + + Console.WriteLine($"Kavita BaseURL [{settings.libraryManagers.FirstOrDefault(lm => lm.GetType() == typeof(Kavita))?.baseUrl}]:"); + string? tmpUrlKavita = Console.ReadLine(); + while (tmpUrlKavita is null) + tmpUrlKavita = Console.ReadLine(); + if (tmpUrlKavita.Length > 0) + { + Console.WriteLine("Username:"); + string? tmpUser = Console.ReadLine(); + while (tmpUser is null || tmpUser.Length < 1) + tmpUser = Console.ReadLine(); + + Console.WriteLine("Password:"); + string tmpPass = string.Empty; + ConsoleKey key; + do + { + var keyInfo = Console.ReadKey(intercept: true); + key = keyInfo.Key; + + if (key == ConsoleKey.Backspace && tmpPass.Length > 0) + { + Console.Write("\b \b"); + tmpPass = tmpPass[0..^1]; + } + else if (!char.IsControl(keyInfo.KeyChar)) + { + Console.Write("*"); + tmpPass += keyInfo.KeyChar; + } + } while (key != ConsoleKey.Enter); + + settings.libraryManagers.RemoveWhere(lm => lm.GetType() == typeof(Kavita)); + settings.libraryManagers.Add(new Kavita(tmpUrlKavita, tmpUser, tmpPass, logger)); } logger.WriteLine("Tranga_CLI", "Loaded."); diff --git a/Tranga/LibraryManager.cs b/Tranga/LibraryManager.cs index 8ddef3d..2c917e4 100644 --- a/Tranga/LibraryManager.cs +++ b/Tranga/LibraryManager.cs @@ -1,11 +1,21 @@ using System.Net; using System.Net.Http.Headers; using Logging; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using Tranga.LibraryManagers; namespace Tranga; public abstract class LibraryManager { + public enum LibraryType : byte + { + Komga = 0, + Kavita = 1 + } + + public LibraryType libraryType; public string baseUrl { get; } protected string auth { get; } //Base64 encoded, if you use your password everywhere, you have problems protected Logger? logger; @@ -79,4 +89,34 @@ public abstract class LibraryManager return false; } } + + public class LibraryManagerJsonConverter : JsonConverter + { + public override bool CanConvert(Type objectType) + { + return (objectType == typeof(LibraryManager)); + } + + public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer) + { + JObject jo = JObject.Load(reader); + if (jo["libraryType"]!.Value() == (Int64)LibraryType.Komga) + return jo.ToObject(serializer)!; + + if (jo["libraryType"]!.Value() == (Int64)LibraryType.Kavita) + return jo.ToObject(serializer)!; + + throw new Exception(); + } + + public override bool CanWrite => false; + + /// + /// Don't call this + /// + public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer) + { + throw new Exception("Dont call this"); + } + } } \ No newline at end of file diff --git a/Tranga/LibraryManagers/Kavita.cs b/Tranga/LibraryManagers/Kavita.cs index 49817ad..8e63c03 100644 --- a/Tranga/LibraryManagers/Kavita.cs +++ b/Tranga/LibraryManagers/Kavita.cs @@ -1,15 +1,71 @@ -using Logging; +using System.Text.Json; +using System.Text.Json.Nodes; +using Logging; namespace Tranga.LibraryManagers; public class Kavita : LibraryManager { + public Kavita(string baseUrl, string username, string password, Logger? logger) + : base(baseUrl, Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes($"{username}:{password}")), logger) + { + this.libraryType = LibraryType.Kavita; + } + public Kavita(string baseUrl, string auth, Logger? logger) : base(baseUrl, auth, logger) { + this.libraryType = LibraryType.Kavita; } public override void UpdateLibrary() { - throw new NotImplementedException(); + logger?.WriteLine(this.GetType().ToString(), $"Updating Libraries"); + foreach (KavitaLibrary lib in GetLibraries()) + NetClient.MakePost($"{baseUrl}/api/Library/scan?libraryId={lib.id}", auth, logger); + } + + /// + /// Fetches all libraries available to the user + /// + /// Array of KavitaLibrary + private IEnumerable GetLibraries() + { + logger?.WriteLine(this.GetType().ToString(), $"Getting Libraries"); + Stream data = NetClient.MakeRequest($"{baseUrl}/api/Library", auth, logger); + if (data == Stream.Null) + { + logger?.WriteLine(this.GetType().ToString(), $"No libraries returned"); + return Array.Empty(); + } + JsonArray? result = JsonSerializer.Deserialize(data); + if (result is null) + { + logger?.WriteLine(this.GetType().ToString(), $"No libraries returned"); + return Array.Empty(); + } + + HashSet ret = new(); + + foreach (JsonNode? jsonNode in result) + { + var jObject = (JsonObject?)jsonNode; + string libraryId = jObject!["id"]!.GetValue(); + string libraryName = jObject!["name"]!.GetValue(); + ret.Add(new KavitaLibrary(libraryId, libraryName)); + } + + return ret; + } + + private struct KavitaLibrary + { + public string id { get; } + public string name { get; } + + public KavitaLibrary(string id, string name) + { + this.id = id; + this.name = name; + } } } \ No newline at end of file diff --git a/Tranga/LibraryManagers/Komga.cs b/Tranga/LibraryManagers/Komga.cs index 87b24b8..656d5b5 100644 --- a/Tranga/LibraryManagers/Komga.cs +++ b/Tranga/LibraryManagers/Komga.cs @@ -14,11 +14,13 @@ public class Komga : LibraryManager public Komga(string baseUrl, string username, string password, Logger? logger) : base(baseUrl, Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes($"{username}:{password}")), logger) { + this.libraryType = LibraryType.Komga; } [JsonConstructor] public Komga(string baseUrl, string auth, Logger? logger) : base(baseUrl, auth, logger) { + this.libraryType = LibraryType.Komga; } public override void UpdateLibrary() diff --git a/Tranga/TrangaSettings.cs b/Tranga/TrangaSettings.cs index ccc1ec0..09a269e 100644 --- a/Tranga/TrangaSettings.cs +++ b/Tranga/TrangaSettings.cs @@ -29,7 +29,7 @@ public class TrangaSettings return new TrangaSettings(Path.Join(Directory.GetCurrentDirectory(), "Downloads"), Directory.GetCurrentDirectory(), new HashSet()); string toRead = File.ReadAllText(importFilePath); - TrangaSettings settings = JsonConvert.DeserializeObject(toRead)!; + TrangaSettings settings = JsonConvert.DeserializeObject(toRead, new JsonSerializerSettings() { Converters = { new LibraryManager.LibraryManagerJsonConverter()} })!; if(logger is not null) foreach(LibraryManager lm in settings.libraryManagers) lm.AddLogger(logger);