From b64ab5c6d4b9eb5924c0d236bdea44680e8dbd3a Mon Sep 17 00:00:00 2001 From: glax Date: Mon, 22 May 2023 00:13:24 +0200 Subject: [PATCH] Created TrangaSettings Different files for settings, tasks, and known publications Komga connector is stored in TrangaSettings --- Tranga-API/Program.cs | 10 ++--- Tranga-CLI/Tranga_Cli.cs | 35 ++++++++------- Tranga/TaskManager.cs | 96 +++++++++++++++++----------------------- Tranga/TrangaSettings.cs | 32 ++++++++++++++ 4 files changed, 98 insertions(+), 75 deletions(-) create mode 100644 Tranga/TrangaSettings.cs diff --git a/Tranga-API/Program.cs b/Tranga-API/Program.cs index 0b5ba2e..6e68f97 100644 --- a/Tranga-API/Program.cs +++ b/Tranga-API/Program.cs @@ -5,7 +5,7 @@ using Tranga; string applicationFolderPath = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Tranga-API"); string logsFolderPath = Path.Join(applicationFolderPath, "logs"); string logFilePath = Path.Join(logsFolderPath, $"log-{DateTime.Now:dd-M-yyyy-HH-mm-ss}.txt"); -string settingsFilePath = Path.Join(applicationFolderPath, "data.json"); +string settingsFilePath = Path.Join(applicationFolderPath, "settings.json"); Directory.CreateDirectory(applicationFolderPath); Directory.CreateDirectory(logsFolderPath); @@ -16,11 +16,11 @@ Console.WriteLine($"Settings-File-Path: {settingsFilePath}"); Logger logger = new(new[] { Logger.LoggerType.FileLogger }, null, null, logFilePath); logger.WriteLine("Tranga_CLI", "Loading Taskmanager."); -TaskManager.SettingsData settings; +TrangaSettings settings; if (File.Exists(settingsFilePath)) - settings = TaskManager.LoadData(settingsFilePath); + settings = TrangaSettings.LoadSettings(settingsFilePath); else - settings = new TaskManager.SettingsData(Directory.GetCurrentDirectory(), settingsFilePath, null, new HashSet()); + settings = new TrangaSettings(Directory.GetCurrentDirectory(), settingsFilePath, null); TaskManager taskManager = new (settings, logger); @@ -114,7 +114,7 @@ class Settings public string downloadLocation { get; } public Komga? komga { get; } - public Settings(TaskManager.SettingsData settings) + public Settings(TrangaSettings settings) { this.downloadLocation = settings.downloadLocation; this.komga = settings.komga; diff --git a/Tranga-CLI/Tranga_Cli.cs b/Tranga-CLI/Tranga_Cli.cs index 72169e5..34de4f5 100644 --- a/Tranga-CLI/Tranga_Cli.cs +++ b/Tranga-CLI/Tranga_Cli.cs @@ -17,7 +17,7 @@ public static class Tranga_Cli string applicationFolderPath = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Tranga"); string logsFolderPath = Path.Join(applicationFolderPath, "logs"); string logFilePath = Path.Join(logsFolderPath, $"log-{DateTime.Now:dd-M-yyyy-HH-mm-ss}.txt"); - string settingsFilePath = Path.Join(applicationFolderPath, "data.json"); + string settingsFilePath = Path.Join(applicationFolderPath, "settings.json"); Directory.CreateDirectory(applicationFolderPath); Directory.CreateDirectory(logsFolderPath); @@ -28,11 +28,11 @@ public static class Tranga_Cli Logger logger = new(new[] { Logger.LoggerType.FileLogger }, null, null, logFilePath); logger.WriteLine("Tranga_CLI", "Loading Taskmanager."); - TaskManager.SettingsData settings; + TrangaSettings settings; if (File.Exists(settingsFilePath)) - settings = TaskManager.LoadData(settingsFilePath); + settings = TrangaSettings.LoadSettings(settingsFilePath); else - settings = new TaskManager.SettingsData(Directory.GetCurrentDirectory(), settingsFilePath, null, new HashSet()); + settings = new TrangaSettings(Directory.GetCurrentDirectory(), applicationFolderPath, null); logger.WriteLine("Tranga_CLI", "User Input"); @@ -40,8 +40,8 @@ public static class Tranga_Cli string? tmpPath = Console.ReadLine(); while(tmpPath is null) tmpPath = Console.ReadLine(); - if(tmpPath.Length > 0) - settings.UpdateSettings(pDownloadLocation: tmpPath, null); + if (tmpPath.Length > 0) + settings.downloadLocation = tmpPath; Console.WriteLine($"Komga BaseURL [{settings.komga?.baseUrl}]:"); string? tmpUrl = Console.ReadLine(); @@ -74,14 +74,14 @@ public static class Tranga_Cli } } while (key != ConsoleKey.Enter); - settings.UpdateSettings(null, new Komga(tmpUrl, tmpUser, tmpPass, logger)); + settings.komga = new Komga(tmpUrl, tmpUser, tmpPass, logger); } logger.WriteLine("Tranga_CLI", "Loaded."); TaskMode(settings, logger); } - private static void TaskMode(TaskManager.SettingsData settings, Logger logger) + private static void TaskMode(TrangaSettings settings, Logger logger) { TaskManager taskManager = new (settings, logger); ConsoleKey selection = ConsoleKey.EraseEndOfFile; @@ -143,7 +143,7 @@ public static class Tranga_Cli TailLog(logger); Console.ReadKey(); break; - case ConsoleKey.M: + case ConsoleKey.G: RemoveTaskFromQueue(taskManager, logger); Console.WriteLine("Press any key."); Console.ReadKey(); @@ -153,6 +153,11 @@ public static class Tranga_Cli Console.WriteLine("Press any key."); Console.ReadKey(); break; + case ConsoleKey.M: + AddMangaTaskToQueue(taskManager, logger); + Console.WriteLine("Press any key."); + Console.ReadKey(); + break; } PrintMenu(taskManager, taskManager.settings.downloadLocation, logger); } @@ -186,8 +191,8 @@ public static class Tranga_Cli Console.WriteLine(); Console.WriteLine($"{"C: Create Task",-30}{"L: List tasks",-30}{"B: Enqueue Task", -30}"); Console.WriteLine($"{"D: Delete Task",-30}{"S: Search Tasks", -30}{"K: List Task Queue", -30}"); - Console.WriteLine($"{"E: Execute Task now",-30}{"R: List Running Tasks", -30}{"M: Remove Task from Queue", -30}"); - Console.WriteLine(); + Console.WriteLine($"{"E: Execute Task now",-30}{"R: List Running Tasks", -30}{"G: Remove Task from Queue", -30}"); + Console.WriteLine($"{"M: New Download Manga Task",-30}{"", -30}{"", -30}"); Console.WriteLine($"{"",-30}{"F: Show Log",-30}{"Q: Exit",-30}"); } @@ -300,7 +305,7 @@ public static class Tranga_Cli } } - private static void CreateTask(TaskManager taskManager, TaskManager.SettingsData settings, Logger logger) + private static void CreateTask(TaskManager taskManager, TrangaSettings settings, Logger logger) { logger.WriteLine("Tranga_CLI", "Menu: Creating Task"); TrangaTask.Task? tmpTask = SelectTaskType(logger); @@ -319,7 +324,7 @@ public static class Tranga_Cli Publication? publication = null; if (task != TrangaTask.Task.UpdatePublications && task != TrangaTask.Task.UpdateKomgaLibrary) { - publication = SelectPublication(connector!, logger); + publication = SelectPublication(taskManager, connector!, logger); if (publication is null) return; } @@ -443,7 +448,7 @@ public static class Tranga_Cli return null; } - private static Publication? SelectPublication(Connector connector, Logger logger) + private static Publication? SelectPublication(TaskManager taskManager, Connector connector, Logger logger) { logger.WriteLine("Tranga_CLI", "Menu: Select Publication"); @@ -452,7 +457,7 @@ public static class Tranga_Cli Console.WriteLine("Publication search query (leave empty for all):"); string? query = Console.ReadLine(); - Publication[] publications = connector.GetPublications(query ?? ""); + Publication[] publications = taskManager.GetPublicationsFromConnector(connector, query ?? ""); int pIndex = 0; Console.WriteLine("Publications:"); diff --git a/Tranga/TaskManager.cs b/Tranga/TaskManager.cs index 6419f8f..eb616fa 100644 --- a/Tranga/TaskManager.cs +++ b/Tranga/TaskManager.cs @@ -11,28 +11,30 @@ namespace Tranga; public class TaskManager { public Dictionary> _chapterCollection = new(); - private readonly HashSet _allTasks; + private HashSet _allTasks; private bool _continueRunning = true; private readonly Connector[] _connectors; private readonly Dictionary> _taskQueue = new(); - public SettingsData settings { get; } + public TrangaSettings settings { get; } private Logger? logger { get; } - public Komga? komga { get; } + public Komga? komga => settings.komga; /// Local path to save data (Manga) to - /// Path to the settings file (data.json) + /// Path to the working directory /// The Url of the Komga-instance that you want to update /// The Komga username /// The Komga password /// - public TaskManager(string downloadFolderPath, string? settingsFilePath = null, string? komgaBaseUrl = null, string? komgaUsername = null, string? komgaPassword = null, Logger? logger = null) + public TaskManager(string downloadFolderPath, string? workingDirectory = null, string? komgaBaseUrl = null, string? komgaUsername = null, string? komgaPassword = null, Logger? logger = null) { this.logger = logger; _allTasks = new HashSet(); + + Komga? newKomga = null; if (komgaBaseUrl != null && komgaUsername != null && komgaPassword != null) - this.komga = new Komga(komgaBaseUrl, komgaUsername, komgaPassword, logger); + newKomga = new Komga(komgaBaseUrl, komgaUsername, komgaPassword, logger); - this.settings = new SettingsData(downloadFolderPath, settingsFilePath, this.komga, this._allTasks); + this.settings = new TrangaSettings(downloadFolderPath, workingDirectory, newKomga); ExportData(); this._connectors = new Connector[]{ new MangaDex(downloadFolderPath, logger) }; @@ -48,21 +50,22 @@ public class TaskManager Komga? komga = null; if (komgaUrl is not null && komgaAuth is not null) komga = new Komga(komgaUrl, komgaAuth, null); - settings.UpdateSettings(downloadLocation, komga); + settings.downloadLocation = downloadLocation ?? settings.downloadLocation; + settings.komga = komga ?? komga; ExportData(); } - public TaskManager(SettingsData settings, Logger? logger = null) + public TaskManager(TrangaSettings settings, Logger? logger = null) { this.logger = logger; this._connectors = new Connector[]{ new MangaDex(settings.downloadLocation, logger) }; - this.settings = settings; - ExportData(); - foreach(Connector cConnector in this._connectors) _taskQueue.Add(cConnector, new List()); - this.komga = settings.komga; - _allTasks = settings.allTasks; + _allTasks = new HashSet(); + + this.settings = settings; + ImportData(); + ExportData(); Thread taskChecker = new(TaskCheckerThread); taskChecker.Start(); } @@ -289,19 +292,25 @@ public class TaskManager Environment.Exit(0); } - /// - /// Loads stored data (settings, tasks) from file - /// - /// working directory, filename has to be data.json - public static SettingsData LoadData(string importFilePath) + private void ImportData() { - if (!File.Exists(importFilePath)) - return new SettingsData(Directory.GetCurrentDirectory(), null, null, new HashSet()); + logger?.WriteLine(this.GetType().ToString(), "Importing Data"); + string buffer; + if (File.Exists(settings.tasksFilePath)) + { + logger?.WriteLine(this.GetType().ToString(), $"Importing tasks from {settings.tasksFilePath}"); + buffer = File.ReadAllText(settings.tasksFilePath); + this._allTasks = JsonConvert.DeserializeObject>(buffer)!; + } - string toRead = File.ReadAllText(importFilePath); - SettingsData data = JsonConvert.DeserializeObject(toRead)!; - - return data; + if (File.Exists(settings.knownPublicationsPath)) + { + logger?.WriteLine(this.GetType().ToString(), $"Importing known publications from {settings.knownPublicationsPath}"); + buffer = File.ReadAllText(settings.knownPublicationsPath); + Publication[] publications = JsonConvert.DeserializeObject(buffer)!; + foreach (Publication publication in publications) + this._chapterCollection.TryAdd(publication, new List()); + } } /// @@ -309,38 +318,15 @@ public class TaskManager /// private void ExportData() { - logger?.WriteLine(this.GetType().ToString(), $"Exporting data to {settings.settingsFilePath}"); + logger?.WriteLine(this.GetType().ToString(), $"Exporting settings to {settings.settingsFilePath}"); + File.WriteAllText(settings.settingsFilePath, JsonConvert.SerializeObject(settings)); + logger?.WriteLine(this.GetType().ToString(), $"Exporting tasks to {settings.tasksFilePath}"); + File.WriteAllText(settings.tasksFilePath, JsonConvert.SerializeObject(this._allTasks)); - - string serializedData = JsonConvert.SerializeObject(settings); - - File.WriteAllText(settings.settingsFilePath, serializedData); + logger?.WriteLine(this.GetType().ToString(), $"Exporting known publications to {settings.knownPublicationsPath}"); + File.WriteAllText(settings.knownPublicationsPath, JsonConvert.SerializeObject(this._chapterCollection.Keys.ToArray())); } - public class SettingsData - { - public string downloadLocation { get; private set; } - public string settingsFilePath { get; } - public Komga? komga { get; private set; } - public HashSet allTasks { get; } - - public SettingsData(string downloadLocation, string? settingsFilePath, Komga? komga, HashSet allTasks) - { - this.settingsFilePath = settingsFilePath ?? - Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), - "Tranga", "data.json"); - this.downloadLocation = downloadLocation; - this.komga = komga; - this.allTasks = allTasks; - } - - public void UpdateSettings(string? pDownloadLocation, Komga? pKomga) - { - if(pDownloadLocation is not null) - this.downloadLocation = pDownloadLocation; - if(pKomga is not null) - this.komga = pKomga; - } - } + } \ No newline at end of file diff --git a/Tranga/TrangaSettings.cs b/Tranga/TrangaSettings.cs new file mode 100644 index 0000000..982528b --- /dev/null +++ b/Tranga/TrangaSettings.cs @@ -0,0 +1,32 @@ +using Newtonsoft.Json; + +namespace Tranga; + +public class TrangaSettings +{ + public string downloadLocation { get; set; } + public string workingDirectory { get; set; } + public string settingsFilePath => Path.Join(workingDirectory, "settings.json"); + public string tasksFilePath => Path.Join(workingDirectory, "tasks.json"); + public string knownPublicationsPath => Path.Join(workingDirectory, "knownPublications.json"); + public Komga? komga { get; set; } + + public TrangaSettings(string downloadLocation, string? workingDirectory, Komga? komga) + { + this.workingDirectory = workingDirectory ?? + Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Tranga"); + this.downloadLocation = downloadLocation; + this.komga = komga; + } + + public static TrangaSettings LoadSettings(string importFilePath) + { + if (!File.Exists(importFilePath)) + return new TrangaSettings(Directory.GetCurrentDirectory(), null, null); + + string toRead = File.ReadAllText(importFilePath); + TrangaSettings settings = JsonConvert.DeserializeObject(toRead)!; + + return settings; + } +} \ No newline at end of file