2
0

Added UpdateKomgaTask

Fixed Komga-auth
Added Komga to data.json
This commit is contained in:
glax 2023-05-20 14:07:38 +02:00
parent ff01bac9d4
commit 538cfec619
5 changed files with 196 additions and 70 deletions

View File

@ -8,19 +8,57 @@ public static class Tranga_Cli
{ {
public static void Main(string[] args) public static void Main(string[] args)
{ {
string folderPath = Directory.GetCurrentDirectory(); TaskManager.SettingsData settings;
string settingsPath = Path.Join(Directory.GetCurrentDirectory(), "lastPath.setting"); string settingsPath = Path.Join(Directory.GetCurrentDirectory(), "data.json");
if (File.Exists(settingsPath)) if (File.Exists(settingsPath))
folderPath = File.ReadAllText(settingsPath); settings = TaskManager.ImportData(Directory.GetCurrentDirectory());
else
settings = new TaskManager.SettingsData(Directory.GetCurrentDirectory(), null, new HashSet<TrangaTask>());
Console.WriteLine($"Output folder path [{folderPath}]:"); Console.WriteLine($"Output folder path [{settings.downloadLocation}]:");
string? tmpPath = Console.ReadLine(); string? tmpPath = Console.ReadLine();
while(tmpPath is null) while(tmpPath is null)
tmpPath = Console.ReadLine(); tmpPath = Console.ReadLine();
if(tmpPath.Length > 0) if(tmpPath.Length > 0)
folderPath = tmpPath; settings.downloadLocation = tmpPath;
File.WriteAllText(settingsPath, folderPath);
Console.WriteLine($"Komga BaseURL [{settings.komga?.baseUrl}]:");
string? tmpUrl = Console.ReadLine();
while (tmpUrl is null)
tmpUrl = Console.ReadLine();
if (tmpUrl.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.komga = new Komga(tmpUrl, tmpUser, tmpPass);
}
//For now only TaskManager mode
/*
Console.Write("Mode (D: Interactive only, T: TaskManager):"); Console.Write("Mode (D: Interactive only, T: TaskManager):");
ConsoleKeyInfo mode = Console.ReadKey(); ConsoleKeyInfo mode = Console.ReadKey();
while (mode.Key != ConsoleKey.D && mode.Key != ConsoleKey.T) while (mode.Key != ConsoleKey.D && mode.Key != ConsoleKey.T)
@ -28,14 +66,14 @@ public static class Tranga_Cli
Console.WriteLine(); Console.WriteLine();
if(mode.Key == ConsoleKey.D) if(mode.Key == ConsoleKey.D)
DownloadNow(folderPath); DownloadNow(settings);
else if (mode.Key == ConsoleKey.T) else if (mode.Key == ConsoleKey.T)
TaskMode(folderPath); TaskMode(settings);*/
TaskMode(settings);
} }
private static void TaskMode(string folderPath) private static void TaskMode(TaskManager.SettingsData settings)
{ {
TaskManager.SettingsData settings = TaskManager.ImportData(Directory.GetCurrentDirectory());
TaskManager taskManager = new TaskManager(settings); TaskManager taskManager = new TaskManager(settings);
ConsoleKey selection = ConsoleKey.NoName; ConsoleKey selection = ConsoleKey.NoName;
int menu = 0; int menu = 0;
@ -50,13 +88,18 @@ public static class Tranga_Cli
menu = 0; menu = 0;
break; break;
case 2: case 2:
Connector connector = SelectConnector(folderPath, taskManager.GetAvailableConnectors().Values.ToArray());
TrangaTask.Task task = SelectTask(); TrangaTask.Task task = SelectTask();
Connector? connector = null;
if(task != TrangaTask.Task.UpdateKomgaLibrary)
connector = SelectConnector(settings.downloadLocation, taskManager.GetAvailableConnectors().Values.ToArray());
Publication? publication = null; Publication? publication = null;
if(task != TrangaTask.Task.UpdatePublications) if(task != TrangaTask.Task.UpdatePublications && task != TrangaTask.Task.UpdateKomgaLibrary)
publication = SelectPublication(connector); publication = SelectPublication(connector!);
TimeSpan reoccurrence = SelectReoccurrence(); TimeSpan reoccurrence = SelectReoccurrence();
TrangaTask newTask = taskManager.AddTask(task, connector.name, publication, reoccurrence, "en"); TrangaTask newTask = taskManager.AddTask(task, connector?.name, publication, reoccurrence, "en");
Console.WriteLine(newTask); Console.WriteLine(newTask);
Console.WriteLine("Press any key."); Console.WriteLine("Press any key.");
Console.ReadKey(); Console.ReadKey();
@ -80,8 +123,7 @@ public static class Tranga_Cli
while (query is null || query.Length < 1) while (query is null || query.Length < 1)
query = Console.ReadLine(); query = Console.ReadLine();
PrintTasks(taskManager.GetAllTasks().Where(qTask => PrintTasks(taskManager.GetAllTasks().Where(qTask =>
((Publication)qTask.publication!).sortName.ToLower() qTask.ToString().ToLower().Contains(query, StringComparison.OrdinalIgnoreCase)).ToArray());
.Contains(query, StringComparison.OrdinalIgnoreCase)).ToArray());
Console.WriteLine("Press any key."); Console.WriteLine("Press any key.");
Console.ReadKey(); Console.ReadKey();
menu = 0; menu = 0;
@ -93,7 +135,7 @@ public static class Tranga_Cli
menu = 0; menu = 0;
break; break;
default: default:
selection = Menu(taskManager, folderPath); selection = Menu(taskManager, settings.downloadLocation);
switch (selection) switch (selection)
{ {
case ConsoleKey.L: case ConsoleKey.L:
@ -129,6 +171,8 @@ public static class Tranga_Cli
{ {
Console.WriteLine("Force quit (Even with running tasks?) y/N"); Console.WriteLine("Force quit (Even with running tasks?) y/N");
selection = Console.ReadKey().Key; selection = Console.ReadKey().Key;
while(selection != ConsoleKey.Y && selection != ConsoleKey.N)
selection = Console.ReadKey().Key;
taskManager.Shutdown(selection == ConsoleKey.Y); taskManager.Shutdown(selection == ConsoleKey.Y);
}else }else
// ReSharper disable once RedundantArgumentDefaultValue Better readability // ReSharper disable once RedundantArgumentDefaultValue Better readability
@ -233,9 +277,9 @@ public static class Tranga_Cli
return TimeSpan.Parse(Console.ReadLine()!, new CultureInfo("en-US")); return TimeSpan.Parse(Console.ReadLine()!, new CultureInfo("en-US"));
} }
private static void DownloadNow(string folderPath) private static void DownloadNow(TaskManager.SettingsData settings)
{ {
Connector connector = SelectConnector(folderPath); Connector connector = SelectConnector(settings.downloadLocation);
Publication publication = SelectPublication(connector); Publication publication = SelectPublication(connector);

View File

@ -1,20 +1,31 @@
using System.Text.Json; using System.Net.Http.Headers;
using System.Text.Json.Nodes; using System.Text.Json.Nodes;
using Newtonsoft.Json;
using JsonSerializer = System.Text.Json.JsonSerializer;
namespace Tranga; namespace Tranga;
public class Komga public class Komga
{ {
private string baseUrl { get; } [System.Text.Json.Serialization.JsonRequired]public string baseUrl { get; }
[System.Text.Json.Serialization.JsonRequired]public string auth { get; }
public Komga(string baseUrl) public Komga(string baseUrl, string username, string password)
{ {
this.baseUrl = baseUrl; this.baseUrl = baseUrl;
this.auth = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes($"{username}:{password}"));
}
[JsonConstructor]
public Komga(string baseUrl, string auth)
{
this.baseUrl = baseUrl;
this.auth = auth;
} }
public KomgaLibrary[] GetLibraries() public KomgaLibrary[] GetLibraries()
{ {
Stream data = NetClient.MakeRequest($"{baseUrl}/api/v1/libraries"); Stream data = NetClient.MakeRequest($"{baseUrl}/api/v1/libraries", auth);
JsonArray? result = JsonSerializer.Deserialize<JsonArray>(data); JsonArray? result = JsonSerializer.Deserialize<JsonArray>(data);
if (result is null) if (result is null)
return Array.Empty<KomgaLibrary>(); return Array.Empty<KomgaLibrary>();
@ -34,7 +45,7 @@ public class Komga
public bool UpdateLibrary(string libraryId) public bool UpdateLibrary(string libraryId)
{ {
return NetClient.MakePost($"{baseUrl}/api/v1/libraries/{libraryId}/scan"); return NetClient.MakePost($"{baseUrl}/api/v1/libraries/{libraryId}/scan", auth);
} }
public struct KomgaLibrary public struct KomgaLibrary
@ -51,19 +62,37 @@ public class Komga
private static class NetClient private static class NetClient
{ {
public static Stream MakeRequest(string url) public static Stream MakeRequest(string url, string auth)
{ {
HttpClient client = new(); HttpClient client = new();
HttpRequestMessage requestMessage = new(HttpMethod.Get, url); HttpRequestMessage requestMessage = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = new Uri(url),
Headers =
{
{ "Accept", "application/json" },
{ "Authorization", new AuthenticationHeaderValue("Basic", auth).ToString() }
}
};
HttpResponseMessage response = client.Send(requestMessage); HttpResponseMessage response = client.Send(requestMessage);
Stream resultString = response.IsSuccessStatusCode ? response.Content.ReadAsStream() : Stream.Null; Stream resultString = response.IsSuccessStatusCode ? response.Content.ReadAsStream() : Stream.Null;
return resultString; return resultString;
} }
public static bool MakePost(string url) public static bool MakePost(string url, string auth)
{ {
HttpClient client = new(); HttpClient client = new();
HttpRequestMessage requestMessage = new(HttpMethod.Post, url); HttpRequestMessage requestMessage = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri(url),
Headers =
{
{ "Accept", "application/json" },
{ "Authorization", new AuthenticationHeaderValue("Basic", auth).ToString() }
}
};
HttpResponseMessage response = client.Send(requestMessage); HttpResponseMessage response = client.Send(requestMessage);
return response.IsSuccessStatusCode; return response.IsSuccessStatusCode;
} }

View File

@ -7,20 +7,22 @@
/// </summary> /// </summary>
public static class TaskExecutor public static class TaskExecutor
{ {
/// <summary> /// <summary>
/// Executes TrangaTask. /// Executes TrangaTask.
/// </summary> /// </summary>
/// <param name="connectors">List of all available Connectors</param> /// <param name="taskManager">Parent</param>
/// <param name="trangaTask">Task to execute</param> /// <param name="trangaTask">Task to execute</param>
/// <param name="chapterCollection">Current chapterCollection to update</param> /// <param name="chapterCollection">Current chapterCollection to update</param>
/// <exception cref="ArgumentException">Is thrown when there is no Connector available with the name of the TrangaTask.connectorName</exception> /// <exception cref="ArgumentException">Is thrown when there is no Connector available with the name of the TrangaTask.connectorName</exception>
public static void Execute(Connector[] connectors, TrangaTask trangaTask, Dictionary<Publication, List<Chapter>> chapterCollection) public static void Execute(TaskManager taskManager, TrangaTask trangaTask, Dictionary<Publication, List<Chapter>> chapterCollection)
{ {
//Get Connector from list of available Connectors and the required Connector of the TrangaTask Connector? connector = null;
Connector? connector = connectors.FirstOrDefault(c => c.name == trangaTask.connectorName); if (trangaTask.task != TrangaTask.Task.UpdateKomgaLibrary)
if (connector is null) {
throw new ArgumentException($"Connector {trangaTask.connectorName} is not a known connector."); //Get Connector from list of available Connectors and the required Connector of the TrangaTask
if (!taskManager.GetAvailableConnectors().TryGetValue(trangaTask.connectorName, out connector))
throw new ArgumentException($"Connector {trangaTask.connectorName} is not a known connector.");
}
if (trangaTask.isBeingExecuted) if (trangaTask.isBeingExecuted)
return; return;
@ -31,19 +33,37 @@ public static class TaskExecutor
switch (trangaTask.task) switch (trangaTask.task)
{ {
case TrangaTask.Task.DownloadNewChapters: case TrangaTask.Task.DownloadNewChapters:
DownloadNewChapters(connector, (Publication)trangaTask.publication!, trangaTask.language, chapterCollection); DownloadNewChapters(connector!, (Publication)trangaTask.publication!, trangaTask.language, chapterCollection);
break; break;
case TrangaTask.Task.UpdateChapters: case TrangaTask.Task.UpdateChapters:
UpdateChapters(connector, (Publication)trangaTask.publication!, trangaTask.language, chapterCollection); UpdateChapters(connector!, (Publication)trangaTask.publication!, trangaTask.language, chapterCollection);
break; break;
case TrangaTask.Task.UpdatePublications: case TrangaTask.Task.UpdatePublications:
UpdatePublications(connector, chapterCollection); UpdatePublications(connector!, chapterCollection);
break;
case TrangaTask.Task.UpdateKomgaLibrary:
UpdateKomgaLibrary(taskManager);
break; break;
} }
trangaTask.isBeingExecuted = false; trangaTask.isBeingExecuted = false;
} }
/// <summary>
/// Updates all Komga-Libraries
/// </summary>
/// <param name="taskManager">Parent</param>
private static void UpdateKomgaLibrary(TaskManager taskManager)
{
if (taskManager.komga is null)
return;
Komga komga = taskManager.komga;
Komga.KomgaLibrary[] allLibraries = komga.GetLibraries();
foreach (Komga.KomgaLibrary lib in allLibraries)
komga.UpdateLibrary(lib.id);
}
/// <summary> /// <summary>
/// Updates the available Publications from a Connector (all of them) /// Updates the available Publications from a Connector (all of them)
/// </summary> /// </summary>

View File

@ -14,19 +14,21 @@ public class TaskManager
private bool _continueRunning = true; private bool _continueRunning = true;
private readonly Connector[] _connectors; private readonly Connector[] _connectors;
private string downloadLocation { get; } private string downloadLocation { get; }
private string? komgaBaseUrl { get; }
public Komga? komga { get; private set; }
/// <param name="folderPath">Local path to save data (Manga) to</param> /// <param name="folderPath">Local path to save data (Manga) to</param>
/// <param name="komgaBaseUrl">The Url of the Komga-instance that you want to update</param> /// <param name="komgaBaseUrl">The Url of the Komga-instance that you want to update</param>
public TaskManager(string folderPath, string? komgaBaseUrl = null) public TaskManager(string folderPath, string? komgaBaseUrl = null, string? komgaUsername = null, string? komgaPassword = null)
{ {
this.downloadLocation = folderPath;
if (komgaBaseUrl != null && komgaUsername != null && komgaPassword != null)
this.komga = new Komga(komgaBaseUrl, komgaUsername, komgaPassword);
this._connectors = new Connector[]{ new MangaDex(folderPath) }; this._connectors = new Connector[]{ new MangaDex(folderPath) };
_chapterCollection = new(); _chapterCollection = new();
_allTasks = new HashSet<TrangaTask>(); _allTasks = new HashSet<TrangaTask>();
this.downloadLocation = folderPath;
this.komgaBaseUrl = komgaBaseUrl;
Thread taskChecker = new(TaskCheckerThread); Thread taskChecker = new(TaskCheckerThread);
taskChecker.Start(); taskChecker.Start();
} }
@ -36,7 +38,7 @@ public class TaskManager
this._connectors = new Connector[]{ new MangaDex(settings.downloadLocation) }; this._connectors = new Connector[]{ new MangaDex(settings.downloadLocation) };
_chapterCollection = new(); _chapterCollection = new();
this.downloadLocation = settings.downloadLocation; this.downloadLocation = settings.downloadLocation;
this.komgaBaseUrl = settings.komgaUrl; this.komga = settings.komga;
_allTasks = settings.allTasks; _allTasks = settings.allTasks;
Thread taskChecker = new(TaskCheckerThread); Thread taskChecker = new(TaskCheckerThread);
taskChecker.Start(); taskChecker.Start();
@ -49,7 +51,7 @@ public class TaskManager
foreach (TrangaTask task in _allTasks) foreach (TrangaTask task in _allTasks)
{ {
if(task.ShouldExecute()) if(task.ShouldExecute())
TaskExecutor.Execute(this._connectors, task, this._chapterCollection); //TODO Might crash here, when adding new Task while another Task is running. Check later TaskExecutor.Execute(this, task, this._chapterCollection); //TODO Might crash here, when adding new Task while another Task is running. Check later
} }
Thread.Sleep(1000); Thread.Sleep(1000);
} }
@ -66,7 +68,7 @@ public class TaskManager
Task t = new Task(() => Task t = new Task(() =>
{ {
TaskExecutor.Execute(this._connectors, task, this._chapterCollection); TaskExecutor.Execute(this, task, this._chapterCollection);
}); });
t.Start(); t.Start();
} }
@ -80,24 +82,44 @@ public class TaskManager
/// <param name="reoccurrence">Time-Interval between Executions</param> /// <param name="reoccurrence">Time-Interval between Executions</param>
/// <param name="language">language, should Task require parameter. Can be empty</param> /// <param name="language">language, should Task require parameter. Can be empty</param>
/// <exception cref="ArgumentException">Is thrown when connectorName is not a available Connector</exception> /// <exception cref="ArgumentException">Is thrown when connectorName is not a available Connector</exception>
public TrangaTask AddTask(TrangaTask.Task task, string connectorName, Publication? publication, TimeSpan reoccurrence, public TrangaTask AddTask(TrangaTask.Task task, string? connectorName, Publication? publication, TimeSpan reoccurrence,
string language = "") string language = "")
{ {
//Get appropriate Connector from available Connectors for TrangaTask if (task != TrangaTask.Task.UpdateKomgaLibrary && connectorName is null)
Connector? connector = _connectors.FirstOrDefault(c => c.name == connectorName); throw new ArgumentException($"connectorName can not be null for task {task}");
if (connector is null)
throw new ArgumentException($"Connector {connectorName} is not a known connector."); TrangaTask newTask;
if (task == TrangaTask.Task.UpdateKomgaLibrary)
TrangaTask newTask = new TrangaTask(connector.name, task, publication, reoccurrence, language);
//Check if same task already exists
if (!_allTasks.Any(trangaTask => trangaTask.task != task && trangaTask.connectorName != connector.name &&
trangaTask.publication?.downloadUrl != publication?.downloadUrl))
{ {
if(task != TrangaTask.Task.UpdatePublications) newTask = new TrangaTask(task, null, null, reoccurrence, language);
_chapterCollection.Add((Publication)publication!, new List<Chapter>());
_allTasks.Add(newTask); //Check if same task already exists
ExportData(Directory.GetCurrentDirectory()); // ReSharper disable once SimplifyLinqExpressionUseAll readabilty
if (!_allTasks.Any(trangaTask => trangaTask.task == task))
{
_allTasks.Add(newTask);
}
} }
else
{
//Get appropriate Connector from available Connectors for TrangaTask
Connector? connector = _connectors.FirstOrDefault(c => c.name == connectorName);
if (connector is null)
throw new ArgumentException($"Connector {connectorName} is not a known connector.");
newTask = new TrangaTask(task, connector.name, publication, reoccurrence, language);
//Check if same task already exists
if (!_allTasks.Any(trangaTask => trangaTask.task == task && trangaTask.connectorName == connector.name &&
trangaTask.publication?.downloadUrl == publication?.downloadUrl))
{
if(task != TrangaTask.Task.UpdatePublications)
_chapterCollection.Add((Publication)publication!, new List<Chapter>());
_allTasks.Add(newTask);
}
}
ExportData(Directory.GetCurrentDirectory());
return newTask; return newTask;
} }
@ -134,6 +156,12 @@ public class TaskManager
{ {
return this._chapterCollection.Keys.ToArray(); return this._chapterCollection.Keys.ToArray();
} }
public void NewKomga(Komga? pKomga)
{
this.komga = pKomga;
this.ExportData(Directory.GetCurrentDirectory());
}
/// <summary> /// <summary>
/// Shuts down the taskManager. /// Shuts down the taskManager.
@ -167,7 +195,7 @@ public class TaskManager
private void ExportData(string exportFolderPath) private void ExportData(string exportFolderPath)
{ {
SettingsData data = new SettingsData(this.downloadLocation, this.komgaBaseUrl, this._allTasks); SettingsData data = new SettingsData(this.downloadLocation, this.komga, this._allTasks);
string exportPath = Path.Join(exportFolderPath, "data.json"); string exportPath = Path.Join(exportFolderPath, "data.json");
string serializedData = JsonConvert.SerializeObject(data); string serializedData = JsonConvert.SerializeObject(data);
@ -176,14 +204,14 @@ public class TaskManager
public class SettingsData public class SettingsData
{ {
public string downloadLocation { get; } public string downloadLocation { get; set; }
public string? komgaUrl { get; } public Komga? komga { get; set; }
public HashSet<TrangaTask> allTasks { get; } public HashSet<TrangaTask> allTasks { get; }
public SettingsData(string downloadLocation, string? komgaUrl, HashSet<TrangaTask> allTasks) public SettingsData(string downloadLocation, Komga? komga, HashSet<TrangaTask> allTasks)
{ {
this.downloadLocation = downloadLocation; this.downloadLocation = downloadLocation;
this.komgaUrl = komgaUrl; this.komga = komga;
this.allTasks = allTasks; this.allTasks = allTasks;
} }
} }

View File

@ -11,16 +11,20 @@ public class TrangaTask
// ReSharper disable once MemberCanBePrivate.Global I want it thaaat way // ReSharper disable once MemberCanBePrivate.Global I want it thaaat way
public TimeSpan reoccurrence { get; } public TimeSpan reoccurrence { get; }
public DateTime lastExecuted { get; set; } public DateTime lastExecuted { get; set; }
public string connectorName { get; } public string? connectorName { get; }
public Task task { get; } public Task task { get; }
public Publication? publication { get; } public Publication? publication { get; }
public string language { get; } public string language { get; }
[JsonIgnore]public bool isBeingExecuted { get; set; } [JsonIgnore]public bool isBeingExecuted { get; set; }
public TrangaTask(string connectorName, Task task, Publication? publication, TimeSpan reoccurrence, string language = "") public TrangaTask(Task task, string? connectorName, Publication? publication, TimeSpan reoccurrence, string language = "")
{ {
if (task != Task.UpdatePublications && publication is null) if(task != Task.UpdateKomgaLibrary && connectorName is null)
throw new ArgumentException($"Publication has to be not null for task {task}"); throw new ArgumentException($"connectorName can not be null for task {task}");
if (publication is null && task != Task.UpdatePublications && task != Task.UpdateKomgaLibrary)
throw new ArgumentException($"Publication can not be null for task {task}");
this.publication = publication; this.publication = publication;
this.reoccurrence = reoccurrence; this.reoccurrence = reoccurrence;
this.lastExecuted = DateTime.Now.Subtract(reoccurrence); this.lastExecuted = DateTime.Now.Subtract(reoccurrence);
@ -39,7 +43,8 @@ public class TrangaTask
{ {
UpdatePublications, UpdatePublications,
UpdateChapters, UpdateChapters,
DownloadNewChapters DownloadNewChapters,
UpdateKomgaLibrary
} }
public override string ToString() public override string ToString()