2
0

Compare commits

...

4 Commits

Author SHA1 Message Date
755167c39a API: StartTask
API: Get Task Queue
API: Task Enqueue
API: Task Dequeue
2023-05-21 16:39:54 +02:00
1cff93fbac Use settings-file for API
Added API-call to list TaskTypes
Working? CreateTask API-call
Working? RemoveTask API-call
2023-05-21 16:23:35 +02:00
6c775d6e0c Moved check into if statement 2023-05-21 16:22:40 +02:00
876b1ab78b Added internalId to Publication 2023-05-21 16:22:14 +02:00
4 changed files with 92 additions and 20 deletions

View File

@ -1,7 +1,27 @@
using System.Text.Json;
using Tranga;
using Logging;
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");
Console.WriteLine($"Logfile-Path: {logFilePath}");
Console.WriteLine($"Settings-File-Path: {settingsFilePath}");
Logger logger = new(new[] { Logger.LoggerType.FileLogger }, null, null, logFilePath);
logger.WriteLine("Tranga_API", "Loading Taskmanager.");
TaskManager.SettingsData settings;
if (File.Exists(settingsFilePath))
settings = TaskManager.LoadData(settingsFilePath);
else
settings = new TaskManager.SettingsData(Directory.GetCurrentDirectory(), settingsFilePath, null, new HashSet<TrangaTask>());
TaskManager taskManager = new (settings, logger);
TaskManager taskManager = new (Directory.GetCurrentDirectory());
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
@ -22,8 +42,14 @@ app.MapGet("/GetPublications", (string connectorName, string? publicationName) =
app.MapGet("/ListTasks", () => JsonSerializer.Serialize(taskManager.GetAllTasks()));
app.MapGet("/TaskTypes", () =>
{
string[] availableTasks = Enum.GetNames(typeof(TrangaTask.Task));
return JsonSerializer.Serialize(availableTasks);
});
app.MapGet("/CreateTask",
(TrangaTask.Task task, string? connectorName, string? publicationName, TimeSpan reoccurrence, string? language) =>
(TrangaTask.Task task, string? connectorName, string? publicationInternalId, TimeSpan reoccurrence, string? language) =>
{
switch (task)
{
@ -33,20 +59,19 @@ app.MapGet("/CreateTask",
case TrangaTask.Task.DownloadNewChapters:
try
{
Connector connector = taskManager.GetConnector(connectorName);
Publication? publication = taskManager.GetAllPublications()
.FirstOrDefault(pub => pub.internalId == publicationInternalId);
Publication[] publications;
if (publicationName is not null)
publications = connector.GetPublications(publicationName);
else
publications = connector.GetPublications();
Publication? publication = publications.FirstOrDefault(pub => pub.downloadUrl == publicationName);
if (publication is null)
JsonSerializer.Serialize($"Publication {publicationName} is unknown.");
{
return JsonSerializer.Serialize($"Publication {publicationInternalId} is unknown.");
}
else
{
taskManager.AddTask(TrangaTask.Task.DownloadNewChapters, connectorName, publication, reoccurrence, language ?? "");
return JsonSerializer.Serialize("Success");
}
}
catch (Exception e)
{
return JsonSerializer.Serialize(e.Message);
@ -58,7 +83,7 @@ app.MapGet("/CreateTask",
return JsonSerializer.Serialize("Not Implemented");
});
app.MapGet("/RemoveTask", (TrangaTask.Task task, string? connectorName, string? publicationName) =>
app.MapGet("/RemoveTask", (TrangaTask.Task task, string? connectorName, string? publicationInternalId) =>
{
switch (task)
{
@ -66,9 +91,9 @@ app.MapGet("/RemoveTask", (TrangaTask.Task task, string? connectorName, string?
taskManager.DeleteTask(TrangaTask.Task.UpdateKomgaLibrary, null, null);
return JsonSerializer.Serialize("Success");
case TrangaTask.Task.DownloadNewChapters:
Publication? publication = taskManager.GetAllPublications().FirstOrDefault(pub => pub.downloadUrl == publicationName);
Publication? publication = taskManager.GetAllPublications().FirstOrDefault(pub => pub.internalId == publicationInternalId);
if (publication is null)
JsonSerializer.Serialize($"Publication {publicationName} is unknown.");
JsonSerializer.Serialize($"Publication with id {publicationInternalId} is unknown.");
taskManager.DeleteTask(TrangaTask.Task.DownloadNewChapters, connectorName, publication);
@ -78,4 +103,46 @@ app.MapGet("/RemoveTask", (TrangaTask.Task task, string? connectorName, string?
}
});
app.MapGet("/StartTask", (TrangaTask.Task task, string? connectorName, string? publicationInternalId) =>
{
TrangaTask[] allTasks = taskManager.GetAllTasks();
TrangaTask? taskToStart = allTasks.FirstOrDefault(tTask =>
tTask.task == task && tTask.connectorName == connectorName &&
tTask.publication?.internalId == publicationInternalId);
if(taskToStart is null)
JsonSerializer.Serialize($"Task with parameters {task} {connectorName} {publicationInternalId} is unknown.");
taskManager.ExecuteTaskNow(taskToStart!);
return JsonSerializer.Serialize("Success");
});
app.MapGet("/TaskQueue", () =>
{
return JsonSerializer.Serialize(taskManager.GetAllTasks()
.Where(task => task.state is TrangaTask.ExecutionState.Enqueued or TrangaTask.ExecutionState.Running)
.ToArray());
});
app.MapGet("/TaskEnqueue", (TrangaTask.Task task, string? connectorName, string? publicationInternalId) =>
{
TrangaTask[] allTasks = taskManager.GetAllTasks();
TrangaTask? taskToEnqueue = allTasks.FirstOrDefault(tTask =>
tTask.task == task && tTask.connectorName == connectorName &&
tTask.publication?.internalId == publicationInternalId);
if(taskToEnqueue is null)
JsonSerializer.Serialize($"Task with parameters {task} {connectorName} {publicationInternalId} is unknown.");
taskManager.AddTaskToQueue(taskToEnqueue!);
return JsonSerializer.Serialize("Success");
});
app.MapGet("/TaskDequeue", (TrangaTask.Task task, string? connectorName, string? publicationInternalId) =>
{TrangaTask[] allTasks = taskManager.GetAllTasks();
TrangaTask? taskToDequeue = allTasks.FirstOrDefault(tTask =>
tTask.task == task && tTask.connectorName == connectorName &&
tTask.publication?.internalId == publicationInternalId);
if(taskToDequeue is null)
JsonSerializer.Serialize($"Task with parameters {task} {connectorName} {publicationInternalId} is unknown.");
taskManager.RemoveTaskFromQueue(taskToDequeue);
return JsonSerializer.Serialize("Success");
});
app.Run();

View File

@ -117,6 +117,7 @@ public class MangaDex : Connector
year,
originalLanguage,
status,
manga["id"]!.GetValue<string>(),
manga["id"]!.GetValue<string>()
);
publications.Add(pub); //Add Publication (Manga) to result

View File

@ -21,7 +21,9 @@ public readonly struct Publication
public string folderName { get; }
public string downloadUrl { get; }
public Publication(string sortName, string? description, string[,] altTitles, string[] tags, string? posterUrl, string[,]? links, int? year, string? originalLanguage, string status, string downloadUrl)
public string internalId { get; }
public Publication(string sortName, string? description, string[,] altTitles, string[] tags, string? posterUrl, string[,]? links, int? year, string? originalLanguage, string status, string downloadUrl, string internalId)
{
this.sortName = sortName;
this.description = description;
@ -34,6 +36,7 @@ public readonly struct Publication
this.status = status;
this.downloadUrl = downloadUrl;
this.folderName = string.Concat(sortName.Split(Path.GetInvalidPathChars().Concat(Path.GetInvalidFileNameChars()).ToArray()));
this.internalId = internalId;
}
/// <returns>Serialized JSON String for series.json</returns>

View File

@ -125,8 +125,6 @@ public class TaskManager
string language = "")
{
logger?.WriteLine(this.GetType().ToString(), $"Adding new Task {task} {connectorName} {publication?.sortName}");
if (task != TrangaTask.Task.UpdateKomgaLibrary && connectorName is null)
throw new ArgumentException($"connectorName can not be null for task {task}");
TrangaTask newTask;
if (task == TrangaTask.Task.UpdateKomgaLibrary)
@ -142,6 +140,9 @@ public class TaskManager
}
else
{
if(connectorName is null)
throw new ArgumentException($"connectorName can not be null for task {task}");
//Get appropriate Connector from available Connectors for TrangaTask
Connector? connector = _connectors.FirstOrDefault(c => c.name == connectorName);
if (connector is null)