From 4ee47ed65cd87b3bdf8b14040e8e80befc625d6f Mon Sep 17 00:00:00 2001 From: glax Date: Sat, 20 May 2023 15:05:41 +0200 Subject: [PATCH] Snarky comments. Documentation --- Tranga-CLI/Tranga_Cli.cs | 8 +++++++- Tranga/Connector.cs | 13 +++++++++++++ Tranga/Komga.cs | 26 ++++++++++++++++++++++---- Tranga/Publication.cs | 11 ++++------- Tranga/TaskExecutor.cs | 2 ++ Tranga/TaskManager.cs | 24 +++++++++++++++++++++++- 6 files changed, 71 insertions(+), 13 deletions(-) diff --git a/Tranga-CLI/Tranga_Cli.cs b/Tranga-CLI/Tranga_Cli.cs index 98b0846..15ee027 100644 --- a/Tranga-CLI/Tranga_Cli.cs +++ b/Tranga-CLI/Tranga_Cli.cs @@ -4,6 +4,12 @@ using Tranga.Connectors; namespace Tranga_CLI; +/* + * This is written with pure hatred for readability. + * At some point do this properly. + * Read at own risk. + */ + public static class Tranga_Cli { public static void Main(string[] args) @@ -11,7 +17,7 @@ public static class Tranga_Cli TaskManager.SettingsData settings; string settingsPath = Path.Join(Directory.GetCurrentDirectory(), "data.json"); if (File.Exists(settingsPath)) - settings = TaskManager.ImportData(Directory.GetCurrentDirectory()); + settings = TaskManager.LoadData(Directory.GetCurrentDirectory()); else settings = new TaskManager.SettingsData(Directory.GetCurrentDirectory(), null, new HashSet()); diff --git a/Tranga/Connector.cs b/Tranga/Connector.cs index b620224..293c8c1 100644 --- a/Tranga/Connector.cs +++ b/Tranga/Connector.cs @@ -68,6 +68,11 @@ public abstract class Connector File.WriteAllText(seriesInfoPath,publication.GetSeriesInfoJson()); } + /// + /// Creates a string containing XML of publication and chapter. + /// See ComicInfo.xml + /// + /// XML-string protected static string CreateComicInfo(Publication publication, Chapter chapter) { XElement comicInfo = new XElement("ComicInfo", @@ -80,11 +85,19 @@ public abstract class Connector return comicInfo.ToString(); } + /// + /// Checks if a chapter-archive is already present + /// + /// true if chapter is present public bool ChapterIsDownloaded(Publication publication, Chapter chapter) { return File.Exists(CreateFullFilepath(publication, chapter)); } + /// + /// Creates full file path of chapter-archive + /// + /// Filepath protected string CreateFullFilepath(Publication publication, Chapter chapter) { return Path.Join(downloadLocation, publication.folderName, chapter.fileName); diff --git a/Tranga/Komga.cs b/Tranga/Komga.cs index 6cfa3f5..c6b9fba 100644 --- a/Tranga/Komga.cs +++ b/Tranga/Komga.cs @@ -5,17 +5,26 @@ using JsonSerializer = System.Text.Json.JsonSerializer; namespace Tranga; +/// +/// Provides connectivity to Komga-API +/// Can fetch and update libraries +/// public class Komga { - [System.Text.Json.Serialization.JsonRequired]public string baseUrl { get; } - [System.Text.Json.Serialization.JsonRequired]public string auth { get; } - + public string baseUrl { get; } + public string auth { get; } //Base64 encoded, if you use your password everywhere, you have problems + + /// Base-URL of Komga instance, no trailing slashes(/) + /// Komga Username + /// Komga password, will be base64 encoded. yea public Komga(string baseUrl, string username, string password) { this.baseUrl = baseUrl; this.auth = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes($"{username}:{password}")); } - + + /// Base-URL of Komga instance, no trailing slashes(/) + /// Base64 string of username and password (username):(password) [JsonConstructor] public Komga(string baseUrl, string auth) { @@ -23,6 +32,10 @@ public class Komga this.auth = auth; } + /// + /// Fetches all libraries available to the user + /// + /// Array of KomgaLibraries public KomgaLibrary[] GetLibraries() { Stream data = NetClient.MakeRequest($"{baseUrl}/api/v1/libraries", auth); @@ -43,6 +56,11 @@ public class Komga return ret.ToArray(); } + /// + /// Updates library with given id + /// + /// Id of the Komga-Library + /// true if successful public bool UpdateLibrary(string libraryId) { return NetClient.MakePost($"{baseUrl}/api/v1/libraries/{libraryId}/scan", auth); diff --git a/Tranga/Publication.cs b/Tranga/Publication.cs index 86808bb..506abd2 100644 --- a/Tranga/Publication.cs +++ b/Tranga/Publication.cs @@ -35,10 +35,7 @@ public readonly struct Publication this.downloadUrl = downloadUrl; this.folderName = string.Concat(sortName.Split(Path.GetInvalidPathChars().Concat(Path.GetInvalidFileNameChars()).ToArray())); } - - /// - /// - /// + /// Serialized JSON String for series.json public string GetSeriesInfoJson() { @@ -57,13 +54,13 @@ public readonly struct Publication //Only for series.json what an abomination, why are all the fields not-null???? private struct Metadata { - // ReSharper disable UnusedAutoPropertyAccessor.Local we need it, trust + // ReSharper disable UnusedAutoPropertyAccessor.Local we need them all, trust me [JsonRequired] public string type { get; } [JsonRequired] public string publisher { get; } // ReSharper disable twice IdentifierTypo [JsonRequired] public int comicid { get; } [JsonRequired] public string booktype { get; } - // ReSharper disable InconsistentNaming + // ReSharper disable InconsistentNaming This one property is capitalized. Why? [JsonRequired] public string ComicImage { get; } [JsonRequired] public int total_issues { get; } [JsonRequired] public string publication_run { get; } @@ -84,7 +81,7 @@ public readonly struct Publication this.status = status; this.description_text = description_text; - //kill it with fire + //kill it with fire, but otherwise Komga will not parse type = "Manga"; publisher = ""; comicid = 0; diff --git a/Tranga/TaskExecutor.cs b/Tranga/TaskExecutor.cs index f827e75..2d42a72 100644 --- a/Tranga/TaskExecutor.cs +++ b/Tranga/TaskExecutor.cs @@ -16,10 +16,12 @@ public static class TaskExecutor /// Is thrown when there is no Connector available with the name of the TrangaTask.connectorName public static void Execute(TaskManager taskManager, TrangaTask trangaTask, Dictionary> chapterCollection) { + //Only execute task if it is not already being executed. if (trangaTask.state == TrangaTask.ExecutionState.Running) return; trangaTask.state = TrangaTask.ExecutionState.Running; + //Connector is not needed for all tasks Connector? connector = null; if (trangaTask.task != TrangaTask.Task.UpdateKomgaLibrary) connector = taskManager.GetConnector(trangaTask.connectorName!); diff --git a/Tranga/TaskManager.cs b/Tranga/TaskManager.cs index ec2e6d0..f25fa54 100644 --- a/Tranga/TaskManager.cs +++ b/Tranga/TaskManager.cs @@ -20,6 +20,8 @@ public class TaskManager /// Local path to save data (Manga) to /// The Url of the Komga-instance that you want to update + /// The Komga username + /// The Komga password public TaskManager(string folderPath, string? komgaBaseUrl = null, string? komgaUsername = null, string? komgaPassword = null) { this.downloadLocation = folderPath; @@ -47,10 +49,15 @@ public class TaskManager taskChecker.Start(); } + /// + /// Runs continuously until shutdown. + /// Checks if tasks have to be executed (time elapsed) + /// private void TaskCheckerThread() { while (_continueRunning) { + //Check if previous tasks have finished and execute new tasks foreach (KeyValuePair> connectorTaskQueue in tasksToExecute) { connectorTaskQueue.Value.RemoveAll(task => task.state == TrangaTask.ExecutionState.Waiting); @@ -58,6 +65,8 @@ public class TaskManager ExecuteTaskNow(connectorTaskQueue.Value.First()); } + //Check if task should be executed + //Depending on type execute immediately or enqueue foreach (TrangaTask task in _allTasks.Where(aTask => aTask.ShouldExecute())) { task.state = TrangaTask.ExecutionState.Enqueued; @@ -172,6 +181,11 @@ public class TaskManager return this._chapterCollection.Keys.ToArray(); } + /// + /// Return Connector with given Name + /// + /// Connector-name (exact) + /// If Connector is not available public Connector GetConnector(string connectorName) { Connector? ret = this._connectors.FirstOrDefault(connector => connector.name == connectorName); @@ -198,7 +212,11 @@ public class TaskManager Environment.Exit(0); } - public static SettingsData ImportData(string importFolderPath) + /// + /// Loads stored data (settings, tasks) from file + /// + /// working directory, filename has to be data.json + public static SettingsData LoadData(string importFolderPath) { string importPath = Path.Join(importFolderPath, "data.json"); if (!File.Exists(importPath)) @@ -210,6 +228,10 @@ public class TaskManager return data; } + /// + /// Exports data (settings, tasks) to file + /// + /// Folder path, filename will be data.json private void ExportData(string exportFolderPath) { SettingsData data = new SettingsData(this.downloadLocation, this.komga, this._allTasks);