From 52f357021d26d0a18a97f33f6172e7381ccf297b Mon Sep 17 00:00:00 2001 From: glax Date: Sat, 20 May 2023 12:53:19 +0200 Subject: [PATCH] Added KomgaAPI base, Rewrote settings/task storage to only produce single file --- Tranga-CLI/Tranga_Cli.cs | 3 +- Tranga.sln.DotSettings | 1 + Tranga/Komga.cs | 71 ++++++++++++++++++++++++++++++++ Tranga/TaskManager.cs | 88 ++++++++++++++++++++++++---------------- 4 files changed, 127 insertions(+), 36 deletions(-) create mode 100644 Tranga/Komga.cs diff --git a/Tranga-CLI/Tranga_Cli.cs b/Tranga-CLI/Tranga_Cli.cs index 1aea113..b05e894 100644 --- a/Tranga-CLI/Tranga_Cli.cs +++ b/Tranga-CLI/Tranga_Cli.cs @@ -35,7 +35,8 @@ public static class Tranga_Cli private static void TaskMode(string folderPath) { - TaskManager taskManager = new TaskManager(folderPath); + TaskManager.SettingsData settings = TaskManager.ImportData(Directory.GetCurrentDirectory()); + TaskManager taskManager = new TaskManager(settings); ConsoleKey selection = ConsoleKey.NoName; int menu = 0; while (selection != ConsoleKey.Escape && selection != ConsoleKey.Q) diff --git a/Tranga.sln.DotSettings b/Tranga.sln.DotSettings index 96cc940..e445d6b 100644 --- a/Tranga.sln.DotSettings +++ b/Tranga.sln.DotSettings @@ -1,2 +1,3 @@  + True True \ No newline at end of file diff --git a/Tranga/Komga.cs b/Tranga/Komga.cs new file mode 100644 index 0000000..964b7b4 --- /dev/null +++ b/Tranga/Komga.cs @@ -0,0 +1,71 @@ +using System.Text.Json; +using System.Text.Json.Nodes; + +namespace Tranga; + +public class Komga +{ + private string baseUrl { get; } + + public Komga(string baseUrl) + { + this.baseUrl = baseUrl; + } + + public KomgaLibrary[] GetLibraries() + { + Stream data = NetClient.MakeRequest($"{baseUrl}/api/v1/libraries"); + JsonArray? result = JsonSerializer.Deserialize(data); + if (result is null) + 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 KomgaLibrary(libraryId, libraryName)); + } + + return ret.ToArray(); + } + + public bool UpdateLibrary(string libraryId) + { + return NetClient.MakePost($"{baseUrl}/api/v1/libraries/{libraryId}/scan"); + } + + public struct KomgaLibrary + { + public string id { get; } + public string name { get; } + + public KomgaLibrary(string id, string name) + { + this.id = id; + this.name = name; + } + } + + private static class NetClient + { + public static Stream MakeRequest(string url) + { + HttpClient client = new(); + HttpRequestMessage requestMessage = new(HttpMethod.Get, url); + HttpResponseMessage response = client.Send(requestMessage); + Stream resultString = response.IsSuccessStatusCode ? response.Content.ReadAsStream() : Stream.Null; + return resultString; + } + + public static bool MakePost(string url) + { + HttpClient client = new(); + HttpRequestMessage requestMessage = new(HttpMethod.Post, url); + HttpResponseMessage response = client.Send(requestMessage); + return response.IsSuccessStatusCode; + } + } +} \ No newline at end of file diff --git a/Tranga/TaskManager.cs b/Tranga/TaskManager.cs index 551fffc..3f8a618 100644 --- a/Tranga/TaskManager.cs +++ b/Tranga/TaskManager.cs @@ -13,16 +13,31 @@ public class TaskManager private readonly HashSet _allTasks; private bool _continueRunning = true; private readonly Connector[] _connectors; + private string downloadLocation { get; } + private string? komgaBaseUrl { get; } - /// - /// - /// /// Local path to save data (Manga) to - public TaskManager(string folderPath) + /// The Url of the Komga-instance that you want to update + public TaskManager(string folderPath, string? komgaBaseUrl = null) { this._connectors = new Connector[]{ new MangaDex(folderPath) }; _chapterCollection = new(); - _allTasks = ImportTasks(Directory.GetCurrentDirectory()); + _allTasks = new HashSet(); + + this.downloadLocation = folderPath; + this.komgaBaseUrl = komgaBaseUrl; + + Thread taskChecker = new(TaskCheckerThread); + taskChecker.Start(); + } + + public TaskManager(SettingsData settings) + { + this._connectors = new Connector[]{ new MangaDex(settings.downloadLocation) }; + _chapterCollection = new(); + this.downloadLocation = settings.downloadLocation; + this.komgaBaseUrl = settings.komgaUrl; + _allTasks = settings.allTasks; Thread taskChecker = new(TaskCheckerThread); taskChecker.Start(); } @@ -81,7 +96,7 @@ public class TaskManager if(task != TrangaTask.Task.UpdatePublications) _chapterCollection.Add((Publication)publication!, new List()); _allTasks.Add(newTask); - ExportTasks(Directory.GetCurrentDirectory()); + ExportData(Directory.GetCurrentDirectory()); } return newTask; } @@ -97,21 +112,15 @@ public class TaskManager _allTasks.RemoveWhere(trangaTask => trangaTask.task == task && trangaTask.connectorName == connectorName && trangaTask.publication?.downloadUrl == publication?.downloadUrl); - ExportTasks(Directory.GetCurrentDirectory()); + ExportData(Directory.GetCurrentDirectory()); } - - /// - /// - /// + /// All available Connectors public Dictionary GetAvailableConnectors() { return this._connectors.ToDictionary(connector => connector.name, connector => connector); } - - /// - /// - /// + /// All TrangaTasks in task-collection public TrangaTask[] GetAllTasks() { @@ -119,10 +128,7 @@ public class TaskManager _allTasks.CopyTo(ret); return ret; } - - /// - /// - /// + /// All added Publications public Publication[] GetAllPublications() { @@ -136,7 +142,7 @@ public class TaskManager public void Shutdown(bool force = false) { _continueRunning = false; - ExportTasks(Directory.GetCurrentDirectory()); + ExportData(Directory.GetCurrentDirectory()); if(force) Environment.Exit(_allTasks.Count(task => task.isBeingExecuted)); @@ -147,26 +153,38 @@ public class TaskManager Environment.Exit(0); } - private HashSet ImportTasks(string importFolderPath) + public static SettingsData ImportData(string importFolderPath) { - string filePath = Path.Join(importFolderPath, "tasks.json"); - if (!File.Exists(filePath)) - return new HashSet(); + string importPath = Path.Join(importFolderPath, "data.json"); + if (!File.Exists(importPath)) + return new SettingsData("", null, new HashSet()); - string toRead = File.ReadAllText(filePath); + string toRead = File.ReadAllText(importPath); + SettingsData data = JsonConvert.DeserializeObject(toRead)!; - TrangaTask[] importTasks = JsonConvert.DeserializeObject(toRead)!; - - foreach(TrangaTask task in importTasks.Where(task => task.publication is not null)) - this._chapterCollection.Add((Publication)task.publication!, new List()); - - return importTasks.ToHashSet(); + return data; } - private void ExportTasks(string exportFolderPath) + private void ExportData(string exportFolderPath) { - string filePath = Path.Join(exportFolderPath, "tasks.json"); - string toWrite = JsonConvert.SerializeObject(_allTasks.ToArray()); - File.WriteAllText(filePath,toWrite); + SettingsData data = new SettingsData(this.downloadLocation, this.komgaBaseUrl, this._allTasks); + + string exportPath = Path.Join(exportFolderPath, "data.json"); + string serializedData = JsonConvert.SerializeObject(data); + File.WriteAllText(exportPath, serializedData); + } + + public class SettingsData + { + public string downloadLocation { get; } + public string? komgaUrl { get; } + public HashSet allTasks { get; } + + public SettingsData(string downloadLocation, string? komgaUrl, HashSet allTasks) + { + this.downloadLocation = downloadLocation; + this.komgaUrl = komgaUrl; + this.allTasks = allTasks; + } } } \ No newline at end of file