2
0

Added KomgaAPI base,

Rewrote settings/task storage to only produce single file
This commit is contained in:
glax 2023-05-20 12:53:19 +02:00
parent d9a7eeb5c3
commit 52f357021d
4 changed files with 127 additions and 36 deletions

View File

@ -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)

View File

@ -1,2 +1,3 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=Komga/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Tranga/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

71
Tranga/Komga.cs Normal file
View File

@ -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<JsonArray>(data);
if (result is null)
return Array.Empty<KomgaLibrary>();
HashSet<KomgaLibrary> ret = new();
foreach (JsonNode jsonNode in result)
{
var jObject = (JsonObject?)jsonNode;
string libraryId = jObject!["id"]!.GetValue<string>();
string libraryName = jObject!["name"]!.GetValue<string>();
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;
}
}
}

View File

@ -13,16 +13,31 @@ public class TaskManager
private readonly HashSet<TrangaTask> _allTasks;
private bool _continueRunning = true;
private readonly Connector[] _connectors;
private string downloadLocation { get; }
private string? komgaBaseUrl { get; }
/// <summary>
///
/// </summary>
/// <param name="folderPath">Local path to save data (Manga) to</param>
public TaskManager(string folderPath)
/// <param name="komgaBaseUrl">The Url of the Komga-instance that you want to update</param>
public TaskManager(string folderPath, string? komgaBaseUrl = null)
{
this._connectors = new Connector[]{ new MangaDex(folderPath) };
_chapterCollection = new();
_allTasks = ImportTasks(Directory.GetCurrentDirectory());
_allTasks = new HashSet<TrangaTask>();
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<Chapter>());
_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());
}
/// <summary>
///
/// </summary>
/// <returns>All available Connectors</returns>
public Dictionary<string, Connector> GetAvailableConnectors()
{
return this._connectors.ToDictionary(connector => connector.name, connector => connector);
}
/// <summary>
///
/// </summary>
/// <returns>All TrangaTasks in task-collection</returns>
public TrangaTask[] GetAllTasks()
{
@ -119,10 +128,7 @@ public class TaskManager
_allTasks.CopyTo(ret);
return ret;
}
/// <summary>
///
/// </summary>
/// <returns>All added Publications</returns>
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<TrangaTask> ImportTasks(string importFolderPath)
public static SettingsData ImportData(string importFolderPath)
{
string filePath = Path.Join(importFolderPath, "tasks.json");
if (!File.Exists(filePath))
return new HashSet<TrangaTask>();
string importPath = Path.Join(importFolderPath, "data.json");
if (!File.Exists(importPath))
return new SettingsData("", null, new HashSet<TrangaTask>());
string toRead = File.ReadAllText(filePath);
string toRead = File.ReadAllText(importPath);
SettingsData data = JsonConvert.DeserializeObject<SettingsData>(toRead)!;
TrangaTask[] importTasks = JsonConvert.DeserializeObject<TrangaTask[]>(toRead)!;
foreach(TrangaTask task in importTasks.Where(task => task.publication is not null))
this._chapterCollection.Add((Publication)task.publication!, new List<Chapter>());
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<TrangaTask> allTasks { get; }
public SettingsData(string downloadLocation, string? komgaUrl, HashSet<TrangaTask> allTasks)
{
this.downloadLocation = downloadLocation;
this.komgaUrl = komgaUrl;
this.allTasks = allTasks;
}
}
}