Tranga-Website/Tranga-API/Program.cs

148 lines
6.1 KiB
C#
Raw Normal View History

2023-05-19 17:36:27 +02:00
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);
2023-05-19 17:36:27 +02:00
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/GetConnectors", () => JsonSerializer.Serialize(taskManager.GetAvailableConnectors().Values.ToArray()));
2023-05-20 17:18:22 +02:00
app.MapGet("/GetPublications", (string connectorName, string? publicationName) =>
2023-05-19 17:36:27 +02:00
{
2023-05-20 14:21:47 +02:00
Connector connector = taskManager.GetConnector(connectorName);
2023-05-19 17:36:27 +02:00
Publication[] publications;
2023-05-20 17:18:22 +02:00
if (publicationName is not null)
publications = connector.GetPublications(publicationName);
2023-05-19 17:36:27 +02:00
else
publications = connector.GetPublications();
return JsonSerializer.Serialize(publications);
});
app.MapGet("/ListTasks", () => JsonSerializer.Serialize(taskManager.GetAllTasks()));
app.MapGet("/TaskTypes", () =>
{
string[] availableTasks = Enum.GetNames(typeof(TrangaTask.Task));
return JsonSerializer.Serialize(availableTasks);
});
2023-05-19 17:36:27 +02:00
app.MapGet("/CreateTask",
(TrangaTask.Task task, string? connectorName, string? publicationInternalId, TimeSpan reoccurrence, string? language) =>
2023-05-19 17:36:27 +02:00
{
2023-05-20 17:18:22 +02:00
switch (task)
{
case TrangaTask.Task.UpdateKomgaLibrary:
taskManager.AddTask(TrangaTask.Task.UpdateKomgaLibrary, null, null, reoccurrence);
break;
case TrangaTask.Task.DownloadNewChapters:
try
{
Publication? publication = taskManager.GetAllPublications()
.FirstOrDefault(pub => pub.internalId == publicationInternalId);
2023-05-20 17:18:22 +02:00
if (publication is null)
{
return JsonSerializer.Serialize($"Publication {publicationInternalId} is unknown.");
}
else
{
taskManager.AddTask(TrangaTask.Task.DownloadNewChapters, connectorName, publication, reoccurrence, language ?? "");
return JsonSerializer.Serialize("Success");
}
2023-05-20 17:18:22 +02:00
}
catch (Exception e)
{
return JsonSerializer.Serialize(e.Message);
}
default: return JsonSerializer.Serialize("Not Implemented");
}
return JsonSerializer.Serialize("Not Implemented");
2023-05-19 17:36:27 +02:00
});
app.MapGet("/RemoveTask", (TrangaTask.Task task, string? connectorName, string? publicationInternalId) =>
2023-05-19 17:36:27 +02:00
{
2023-05-20 17:18:22 +02:00
switch (task)
{
case TrangaTask.Task.UpdateKomgaLibrary:
taskManager.DeleteTask(TrangaTask.Task.UpdateKomgaLibrary, null, null);
2023-05-20 17:18:22 +02:00
return JsonSerializer.Serialize("Success");
case TrangaTask.Task.DownloadNewChapters:
Publication? publication = taskManager.GetAllPublications().FirstOrDefault(pub => pub.internalId == publicationInternalId);
2023-05-20 17:18:22 +02:00
if (publication is null)
JsonSerializer.Serialize($"Publication with id {publicationInternalId} is unknown.");
2023-05-20 17:18:22 +02:00
taskManager.DeleteTask(TrangaTask.Task.DownloadNewChapters, connectorName, publication);
2023-05-20 17:18:22 +02:00
return JsonSerializer.Serialize("Success");
2023-05-19 17:36:27 +02:00
2023-05-20 17:18:22 +02:00
default: return JsonSerializer.Serialize("Not Implemented");
}
2023-05-19 17:36:27 +02:00
});
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");
});
2023-05-19 17:36:27 +02:00
app.Run();