2023-05-25 10:25:24 +02:00
|
|
|
using System.Runtime.InteropServices;
|
2023-05-21 21:12:32 +02:00
|
|
|
using Logging;
|
|
|
|
using Tranga;
|
2023-06-08 19:24:46 +02:00
|
|
|
using Tranga.TrangaTasks;
|
2023-05-21 21:12:32 +02:00
|
|
|
|
2023-05-25 10:25:24 +02:00
|
|
|
string applicationFolderPath = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Tranga-API");
|
|
|
|
string downloadFolderPath = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "/Manga" : Path.Join(applicationFolderPath, "Manga");
|
|
|
|
string logsFolderPath = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "/var/logs/Tranga" : Path.Join(applicationFolderPath, "logs");
|
2023-05-21 21:12:32 +02:00
|
|
|
string logFilePath = Path.Join(logsFolderPath, $"log-{DateTime.Now:dd-M-yyyy-HH-mm-ss}.txt");
|
2023-05-22 00:13:24 +02:00
|
|
|
string settingsFilePath = Path.Join(applicationFolderPath, "settings.json");
|
2023-05-21 21:12:32 +02:00
|
|
|
|
|
|
|
Directory.CreateDirectory(logsFolderPath);
|
2023-05-25 14:23:33 +02:00
|
|
|
Logger logger = new(new[] { Logger.LoggerType.FileLogger, Logger.LoggerType.ConsoleLogger }, Console.Out, Console.Out.Encoding, logFilePath);
|
2023-05-25 10:25:24 +02:00
|
|
|
|
2023-05-25 14:23:33 +02:00
|
|
|
logger.WriteLine("Tranga", "Loading settings.");
|
2023-05-21 21:12:32 +02:00
|
|
|
|
2023-05-22 00:13:24 +02:00
|
|
|
TrangaSettings settings;
|
2023-05-21 21:12:32 +02:00
|
|
|
if (File.Exists(settingsFilePath))
|
2023-06-03 15:02:15 +02:00
|
|
|
settings = TrangaSettings.LoadSettings(settingsFilePath, logger);
|
2023-05-21 21:12:32 +02:00
|
|
|
else
|
2023-06-15 18:25:32 +02:00
|
|
|
settings = new TrangaSettings(downloadFolderPath, applicationFolderPath, new HashSet<LibraryManager>(), new HashSet<NotificationManager>());
|
2023-05-21 21:12:32 +02:00
|
|
|
|
2023-05-25 14:23:33 +02:00
|
|
|
Directory.CreateDirectory(settings.workingDirectory);
|
|
|
|
Directory.CreateDirectory(settings.downloadLocation);
|
|
|
|
Directory.CreateDirectory(settings.coverImageCache);
|
|
|
|
|
|
|
|
logger.WriteLine("Tranga",$"Application-Folder: {settings.workingDirectory}");
|
|
|
|
logger.WriteLine("Tranga",$"Settings-File-Path: {settings.settingsFilePath}");
|
|
|
|
logger.WriteLine("Tranga",$"Download-Folder-Path: {settings.downloadLocation}");
|
|
|
|
logger.WriteLine("Tranga",$"Logfile-Path: {logFilePath}");
|
|
|
|
logger.WriteLine("Tranga",$"Image-Cache-Path: {settings.coverImageCache}");
|
|
|
|
|
|
|
|
logger.WriteLine("Tranga", "Loading Taskmanager.");
|
2023-05-21 21:12:32 +02:00
|
|
|
TaskManager taskManager = new (settings, logger);
|
2023-06-19 16:46:12 +02:00
|
|
|
foreach(NotificationManager nm in taskManager.settings.notificationManagers)
|
|
|
|
nm.SendNotification("Tranga-API", "Started Tranga-API");
|
2023-05-21 21:12:32 +02:00
|
|
|
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
2023-05-21 22:01:28 +02:00
|
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
|
|
builder.Services.AddSwaggerGen();
|
2023-05-21 21:12:32 +02:00
|
|
|
builder.Services.AddControllers().AddNewtonsoftJson();
|
2023-05-23 13:16:37 +02:00
|
|
|
|
|
|
|
string corsHeader = "Tranga";
|
|
|
|
builder.Services.AddCors(options =>
|
|
|
|
{
|
|
|
|
options.AddPolicy(name: corsHeader,
|
|
|
|
policy =>
|
|
|
|
{
|
2023-05-24 22:56:10 +02:00
|
|
|
policy.AllowAnyOrigin();
|
2023-05-23 18:11:18 +02:00
|
|
|
policy.WithMethods("GET", "POST", "DELETE");
|
2023-05-23 13:16:37 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-05-21 21:12:32 +02:00
|
|
|
var app = builder.Build();
|
2023-05-21 22:01:28 +02:00
|
|
|
app.UseSwagger();
|
|
|
|
app.UseSwaggerUI();
|
2023-05-21 21:12:32 +02:00
|
|
|
|
2023-05-23 13:16:37 +02:00
|
|
|
app.UseCors(corsHeader);
|
2023-05-21 21:12:32 +02:00
|
|
|
|
2023-06-08 19:24:46 +02:00
|
|
|
app.MapGet("/Controllers/Get", () => taskManager.GetAvailableConnectors().Keys.ToArray());
|
2023-05-21 21:12:32 +02:00
|
|
|
|
2023-06-08 19:24:46 +02:00
|
|
|
app.MapGet("/Publications/GetKnown", (string? internalId) =>
|
|
|
|
{
|
|
|
|
if(internalId is null)
|
|
|
|
return taskManager.GetAllPublications();
|
|
|
|
|
|
|
|
return new [] { taskManager.GetAllPublications().FirstOrDefault(pub => pub.internalId == internalId) };
|
|
|
|
});
|
2023-05-23 13:17:05 +02:00
|
|
|
|
2023-06-08 19:24:46 +02:00
|
|
|
app.MapGet("/Publications/GetFromConnector", (string connectorName, string title) =>
|
2023-05-21 21:12:32 +02:00
|
|
|
{
|
|
|
|
Connector? connector = taskManager.GetAvailableConnectors().FirstOrDefault(con => con.Key == connectorName).Value;
|
|
|
|
if (connector is null)
|
|
|
|
return Array.Empty<Publication>();
|
|
|
|
if(title.Length < 4)
|
|
|
|
return Array.Empty<Publication>();
|
|
|
|
return taskManager.GetPublicationsFromConnector(connector, title);
|
|
|
|
});
|
|
|
|
|
2023-06-15 21:15:44 +02:00
|
|
|
app.MapGet("/Publications/GetChapters",
|
|
|
|
(string connectorName, string internalId, string? onlyNew, string? onlyExisting, string? language) =>
|
|
|
|
{
|
|
|
|
string[] yes = { "true", "yes", "1", "y" };
|
|
|
|
bool newOnly = onlyNew is not null && yes.Contains(onlyNew);
|
|
|
|
bool existingOnly = onlyExisting is not null && yes.Contains(onlyExisting);
|
|
|
|
|
|
|
|
Connector? connector = taskManager.GetAvailableConnectors().FirstOrDefault(con => con.Key == connectorName).Value;
|
|
|
|
if (connector is null)
|
|
|
|
return Array.Empty<Chapter>();
|
|
|
|
Publication? publication = taskManager.GetAllPublications().FirstOrDefault(pub => pub.internalId == internalId);
|
|
|
|
if (publication is null)
|
|
|
|
return Array.Empty<Chapter>();
|
|
|
|
|
|
|
|
if(newOnly)
|
|
|
|
return taskManager.GetNewChaptersList(connector, (Publication)publication, language??"en").ToArray();
|
|
|
|
else if (existingOnly)
|
|
|
|
return taskManager.GetExistingChaptersList(connector, (Publication)publication, language ?? "en").ToArray();
|
|
|
|
else
|
|
|
|
return connector.GetChapters((Publication)publication, language??"en");
|
2023-06-08 19:24:46 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
app.MapGet("/Tasks/GetTypes", () => Enum.GetNames(typeof(TrangaTask.Task)));
|
2023-05-21 21:12:32 +02:00
|
|
|
|
|
|
|
|
2023-06-08 19:24:46 +02:00
|
|
|
app.MapPost("/Tasks/CreateMonitorTask",
|
|
|
|
(string connectorName, string internalId, string reoccurrenceTime, string? language) =>
|
|
|
|
{
|
|
|
|
Connector? connector =
|
|
|
|
taskManager.GetAvailableConnectors().FirstOrDefault(con => con.Key == connectorName).Value;
|
|
|
|
if (connector is null)
|
|
|
|
return;
|
|
|
|
Publication? publication = taskManager.GetAllPublications().FirstOrDefault(pub => pub.internalId == internalId);
|
|
|
|
if (publication is null)
|
|
|
|
return;
|
2023-06-20 14:57:44 +02:00
|
|
|
taskManager.AddTask(new MonitorPublicationTask(connectorName, (Publication)publication, TimeSpan.Parse(reoccurrenceTime), language ?? "en"));
|
2023-06-08 19:24:46 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
app.MapPost("/Tasks/CreateUpdateLibraryTask", (string reoccurrenceTime) =>
|
2023-05-21 21:12:32 +02:00
|
|
|
{
|
2023-06-20 14:57:44 +02:00
|
|
|
taskManager.AddTask(new UpdateLibrariesTask(TimeSpan.Parse(reoccurrenceTime)));
|
2023-06-08 19:24:46 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
app.MapPost("/Tasks/CreateDownloadChaptersTask", (string connectorName, string internalId, string chapters, string? language) => {
|
|
|
|
|
|
|
|
Connector? connector =
|
|
|
|
taskManager.GetAvailableConnectors().FirstOrDefault(con => con.Key == connectorName).Value;
|
|
|
|
if (connector is null)
|
|
|
|
return;
|
|
|
|
Publication? publication = taskManager.GetAllPublications().FirstOrDefault(pub => pub.internalId == internalId);
|
|
|
|
if (publication is null)
|
|
|
|
return;
|
|
|
|
|
2023-06-09 11:06:18 +02:00
|
|
|
IEnumerable<Chapter> toDownload = connector.SearchChapters((Publication)publication, chapters, language ?? "en");
|
2023-06-08 19:53:05 +02:00
|
|
|
foreach(Chapter chapter in toDownload)
|
2023-06-20 14:57:44 +02:00
|
|
|
taskManager.AddTask(new DownloadChapterTask(connectorName, (Publication)publication, chapter, "en"));
|
2023-05-21 21:12:32 +02:00
|
|
|
});
|
|
|
|
|
2023-05-21 22:24:12 +02:00
|
|
|
app.MapDelete("/Tasks/Delete", (string taskType, string? connectorName, string? publicationId) =>
|
2023-05-21 21:12:32 +02:00
|
|
|
{
|
|
|
|
TrangaTask.Task task = Enum.Parse<TrangaTask.Task>(taskType);
|
2023-06-20 14:57:44 +02:00
|
|
|
foreach(TrangaTask tTask in taskManager.GetTasksMatching(task, connectorName, internalId: publicationId))
|
|
|
|
taskManager.DeleteTask(tTask);
|
2023-05-21 21:12:32 +02:00
|
|
|
});
|
|
|
|
|
2023-05-24 21:04:24 +02:00
|
|
|
app.MapGet("/Tasks/Get", (string taskType, string? connectorName, string? searchString) =>
|
|
|
|
{
|
2023-05-24 23:52:40 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
TrangaTask.Task task = Enum.Parse<TrangaTask.Task>(taskType);
|
2023-06-05 00:35:57 +02:00
|
|
|
return taskManager.GetTasksMatching(task, connectorName:connectorName, searchString:searchString);
|
2023-05-24 23:52:40 +02:00
|
|
|
}
|
|
|
|
catch (ArgumentException)
|
|
|
|
{
|
|
|
|
return Array.Empty<TrangaTask>();
|
|
|
|
}
|
2023-05-24 21:04:24 +02:00
|
|
|
});
|
2023-05-21 21:12:32 +02:00
|
|
|
|
2023-06-09 23:42:18 +02:00
|
|
|
app.MapGet("/Tasks/GetProgress", (string taskType, string connectorName, string publicationId, string? chapterSortNumber) =>
|
2023-06-01 23:08:43 +02:00
|
|
|
{
|
2023-06-09 23:42:18 +02:00
|
|
|
Connector? connector =
|
|
|
|
taskManager.GetAvailableConnectors().FirstOrDefault(con => con.Key == connectorName).Value;
|
|
|
|
if (connector is null)
|
|
|
|
return -1f;
|
2023-06-01 23:08:43 +02:00
|
|
|
try
|
|
|
|
{
|
2023-06-09 23:42:18 +02:00
|
|
|
TrangaTask? task = null;
|
2023-06-01 23:08:43 +02:00
|
|
|
TrangaTask.Task pTask = Enum.Parse<TrangaTask.Task>(taskType);
|
2023-06-20 14:57:44 +02:00
|
|
|
if (pTask is TrangaTask.Task.MonitorPublication)
|
2023-06-09 23:42:18 +02:00
|
|
|
{
|
|
|
|
task = taskManager.GetTasksMatching(pTask, connectorName: connectorName, internalId: publicationId).FirstOrDefault();
|
|
|
|
}else if (pTask is TrangaTask.Task.DownloadChapter && chapterSortNumber is not null)
|
|
|
|
{
|
|
|
|
task = taskManager.GetTasksMatching(pTask, connectorName: connectorName, internalId: publicationId,
|
|
|
|
chapterSortNumber: chapterSortNumber).FirstOrDefault();
|
|
|
|
}
|
2023-06-01 23:08:43 +02:00
|
|
|
if (task is null)
|
|
|
|
return -1f;
|
|
|
|
|
|
|
|
return task.progress;
|
|
|
|
}
|
|
|
|
catch (ArgumentException)
|
|
|
|
{
|
|
|
|
return -1f;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-06-05 00:50:51 +02:00
|
|
|
app.MapPost("/Tasks/Start", (string taskType, string? connectorName, string? internalId) =>
|
2023-05-21 21:12:32 +02:00
|
|
|
{
|
2023-05-24 23:52:40 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
TrangaTask.Task pTask = Enum.Parse<TrangaTask.Task>(taskType);
|
2023-06-05 00:35:57 +02:00
|
|
|
TrangaTask? task = taskManager
|
2023-06-20 14:57:44 +02:00
|
|
|
.GetTasksMatching(pTask, connectorName: connectorName, internalId: internalId).FirstOrDefault();
|
2023-05-24 23:52:40 +02:00
|
|
|
|
|
|
|
if (task is null)
|
|
|
|
return;
|
|
|
|
taskManager.ExecuteTaskNow(task);
|
|
|
|
}
|
|
|
|
catch (ArgumentException)
|
|
|
|
{
|
2023-05-21 21:12:32 +02:00
|
|
|
return;
|
2023-05-24 23:52:40 +02:00
|
|
|
}
|
|
|
|
|
2023-05-21 21:12:32 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
app.MapGet("/Tasks/GetRunningTasks",
|
|
|
|
() => taskManager.GetAllTasks().Where(task => task.state is TrangaTask.ExecutionState.Running));
|
|
|
|
|
|
|
|
app.MapGet("/Queue/GetList",
|
2023-06-10 00:45:46 +02:00
|
|
|
() => taskManager.GetAllTasks().Where(task => task.state is TrangaTask.ExecutionState.Enqueued).OrderBy(task => task.nextExecution));
|
2023-05-21 21:12:32 +02:00
|
|
|
|
|
|
|
app.MapPost("/Queue/Enqueue", (string taskType, string? connectorName, string? publicationId) =>
|
|
|
|
{
|
2023-05-24 23:52:40 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
TrangaTask.Task pTask = Enum.Parse<TrangaTask.Task>(taskType);
|
2023-06-05 00:35:57 +02:00
|
|
|
TrangaTask? task = taskManager
|
2023-06-20 14:57:44 +02:00
|
|
|
.GetTasksMatching(pTask, connectorName: connectorName, internalId: publicationId).FirstOrDefault();
|
2023-05-24 23:52:40 +02:00
|
|
|
|
|
|
|
if (task is null)
|
|
|
|
return;
|
|
|
|
taskManager.AddTaskToQueue(task);
|
|
|
|
}
|
|
|
|
catch (ArgumentException)
|
|
|
|
{
|
2023-05-21 21:12:32 +02:00
|
|
|
return;
|
2023-05-24 23:52:40 +02:00
|
|
|
}
|
2023-05-21 21:12:32 +02:00
|
|
|
});
|
|
|
|
|
2023-05-21 22:24:12 +02:00
|
|
|
app.MapDelete("/Queue/Dequeue", (string taskType, string? connectorName, string? publicationId) =>
|
2023-05-21 21:12:32 +02:00
|
|
|
{
|
2023-05-24 23:52:40 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
TrangaTask.Task pTask = Enum.Parse<TrangaTask.Task>(taskType);
|
2023-06-05 00:35:57 +02:00
|
|
|
TrangaTask? task = taskManager
|
2023-06-20 14:57:44 +02:00
|
|
|
.GetTasksMatching(pTask, connectorName: connectorName, internalId: publicationId).FirstOrDefault();
|
2023-05-24 23:52:40 +02:00
|
|
|
|
|
|
|
if (task is null)
|
|
|
|
return;
|
|
|
|
taskManager.RemoveTaskFromQueue(task);
|
|
|
|
}
|
|
|
|
catch (ArgumentException)
|
|
|
|
{
|
2023-05-21 21:12:32 +02:00
|
|
|
return;
|
2023-05-24 23:52:40 +02:00
|
|
|
}
|
2023-05-21 21:12:32 +02:00
|
|
|
});
|
|
|
|
|
2023-05-22 01:49:53 +02:00
|
|
|
app.MapGet("/Settings/Get", () => taskManager.settings);
|
2023-05-21 21:12:32 +02:00
|
|
|
|
2023-06-03 15:17:08 +02:00
|
|
|
app.MapPost("/Settings/Update",
|
2023-06-15 18:25:32 +02:00
|
|
|
(string? downloadLocation, string? komgaUrl, string? komgaAuth, string? kavitaUrl, string? kavitaUsername,
|
2023-06-15 18:57:21 +02:00
|
|
|
string? kavitaPassword, string? gotifyUrl, string? gotifyAppToken, string? lunaseaWebhook) =>
|
2023-06-15 18:25:32 +02:00
|
|
|
{
|
|
|
|
if (downloadLocation is not null && downloadLocation.Length > 0)
|
|
|
|
taskManager.settings.UpdateSettings(TrangaSettings.UpdateField.DownloadLocation, logger, downloadLocation);
|
|
|
|
if (komgaUrl is not null && komgaAuth is not null && komgaUrl.Length > 5 && komgaAuth.Length > 0)
|
|
|
|
taskManager.settings.UpdateSettings(TrangaSettings.UpdateField.Komga, logger, komgaUrl, komgaAuth);
|
|
|
|
if (kavitaUrl is not null && kavitaPassword is not null && kavitaUsername is not null && kavitaUrl.Length > 5 &&
|
|
|
|
kavitaUsername.Length > 0 && kavitaPassword.Length > 0)
|
|
|
|
taskManager.settings.UpdateSettings(TrangaSettings.UpdateField.Kavita, logger, kavitaUrl, kavitaUsername,
|
|
|
|
kavitaPassword);
|
|
|
|
if (gotifyUrl is not null && gotifyAppToken is not null && gotifyUrl.Length > 5 && gotifyAppToken.Length > 0)
|
|
|
|
taskManager.settings.UpdateSettings(TrangaSettings.UpdateField.Gotify, logger, gotifyUrl, gotifyAppToken);
|
2023-06-15 18:57:21 +02:00
|
|
|
if(lunaseaWebhook is not null && lunaseaWebhook.Length > 5)
|
|
|
|
taskManager.settings.UpdateSettings(TrangaSettings.UpdateField.LunaSea, logger, lunaseaWebhook);
|
2023-06-15 18:25:32 +02:00
|
|
|
});
|
2023-05-21 21:12:32 +02:00
|
|
|
|
2023-05-22 01:49:53 +02:00
|
|
|
app.Run();
|