Compare commits

...

6 Commits

10 changed files with 331 additions and 29 deletions

View File

@ -20,7 +20,7 @@ public class Logger : TextWriter
this.Encoding = encoding ?? Encoding.ASCII;
if (enabledLoggers.Contains(LoggerType.FileLogger) && logFilePath is not null)
_fileLogger = new FileLogger(logFilePath, encoding);
else
else if(enabledLoggers.Contains(LoggerType.FileLogger) && logFilePath is null)
{
_fileLogger = null;
throw new ArgumentException($"logFilePath can not be null for LoggerType {LoggerType.FileLogger}");

View File

@ -1,4 +1,7 @@
using Logging;
using Newtonsoft.Json;
using Tranga.LibraryConnectors;
using Tranga.NotificationConnectors;
namespace Tranga;
@ -6,17 +9,23 @@ public abstract class GlobalBase
{
protected Logger? logger { get; init; }
protected TrangaSettings settings { get; init; }
private HashSet<NotificationConnector> notificationConnectors { get; init; }
private HashSet<LibraryConnector> libraryConnectors { get; init; }
public GlobalBase(GlobalBase clone)
protected GlobalBase(GlobalBase clone)
{
this.logger = clone.logger;
this.settings = clone.settings;
this.notificationConnectors = clone.notificationConnectors;
this.libraryConnectors = clone.libraryConnectors;
}
public GlobalBase(Logger? logger, TrangaSettings settings)
protected GlobalBase(Logger? logger, TrangaSettings settings)
{
this.logger = logger;
this.settings = settings;
this.notificationConnectors = settings.LoadNotificationConnectors();
this.libraryConnectors = settings.LoadLibraryConnectors();
}
protected void Log(string message)
@ -29,6 +38,38 @@ public abstract class GlobalBase
Log(string.Format(fStr, replace));
}
protected void SendNotifications(string title, string text)
{
foreach (NotificationConnector nc in notificationConnectors)
nc.SendNotification(title, text);
}
protected void AddNotificationConnector(NotificationConnector notificationConnector)
{
notificationConnectors.RemoveWhere(nc => nc.GetType() == notificationConnector.GetType());
notificationConnectors.Add(notificationConnector);
while(IsFileInUse(settings.notificationConnectorsFilePath))
Thread.Sleep(100);
File.WriteAllText(settings.notificationConnectorsFilePath, JsonConvert.SerializeObject(notificationConnectors));
}
protected void UpdateLibraries()
{
foreach(LibraryConnector lc in libraryConnectors)
lc.UpdateLibrary();
}
protected void AddLibraryConnector(LibraryConnector libraryConnector)
{
libraryConnectors.RemoveWhere(lc => lc.GetType() == libraryConnector.GetType());
libraryConnectors.Add(libraryConnector);
while(IsFileInUse(settings.libraryConnectorsFilePath))
Thread.Sleep(100);
File.WriteAllText(settings.libraryConnectorsFilePath, JsonConvert.SerializeObject(libraryConnectors));
}
protected bool IsFileInUse(string filePath)
{
if (!File.Exists(filePath))

View File

@ -6,7 +6,7 @@ public class DownloadChapter : Job
{
public Chapter chapter { get; init; }
public DownloadChapter(MangaConnector connector, Chapter chapter) : base(connector)
public DownloadChapter(GlobalBase clone, MangaConnector connector, Chapter chapter) : base(clone, connector)
{
this.chapter = chapter;
}
@ -16,6 +16,8 @@ public class DownloadChapter : Job
Task downloadTask = new(delegate
{
mangaConnector.DownloadChapter(chapter, this.progressToken);
UpdateLibraries();
SendNotifications("Chapter downloaded", $"{chapter.parentPublication.sortName} - {chapter.chapterNumber}");
});
downloadTask.Start();
return Array.Empty<Job>();

View File

@ -6,7 +6,7 @@ public class DownloadNewChapters : Job
{
public Publication publication { get; init; }
public DownloadNewChapters(MangaConnector connector, Publication publication, bool recurring = false) : base (connector, recurring)
public DownloadNewChapters(GlobalBase clone, MangaConnector connector, Publication publication, bool recurring = false) : base (clone, connector, recurring)
{
this.publication = publication;
}
@ -18,7 +18,7 @@ public class DownloadNewChapters : Job
List<Job> subJobs = new();
foreach (Chapter chapter in chapters)
{
DownloadChapter downloadChapterJob = new(this.mangaConnector, chapter);
DownloadChapter downloadChapterJob = new(this, this.mangaConnector, chapter);
subJobs.Add(downloadChapterJob);
}
progressToken.Complete();

View File

@ -2,7 +2,7 @@
namespace Tranga.Jobs;
public abstract class Job
public abstract class Job : GlobalBase
{
public MangaConnector mangaConnector { get; init; }
public ProgressToken progressToken { get; private set; }
@ -11,7 +11,7 @@ public abstract class Job
public DateTime? lastExecution { get; private set; }
public DateTime nextExecution => NextExecution();
public Job(MangaConnector connector, bool recurring = false, TimeSpan? recurrenceTime = null)
public Job(GlobalBase clone, MangaConnector connector, bool recurring = false, TimeSpan? recurrenceTime = null) : base(clone)
{
this.mangaConnector = connector;
this.progressToken = new ProgressToken(0);
@ -21,7 +21,7 @@ public abstract class Job
this.recurrenceTime = recurrenceTime;
}
public Job(MangaConnector connector, ProgressToken progressToken, bool recurring = false, TimeSpan? recurrenceTime = null)
public Job(GlobalBase clone, MangaConnector connector, ProgressToken progressToken, bool recurring = false, TimeSpan? recurrenceTime = null) : base(clone)
{
this.mangaConnector = connector;
this.progressToken = progressToken;
@ -31,7 +31,7 @@ public abstract class Job
this.recurrenceTime = recurrenceTime;
}
public Job(MangaConnector connector, int taskIncrements, bool recurring = false, TimeSpan? recurrenceTime = null)
public Job(GlobalBase clone, MangaConnector connector, int taskIncrements, bool recurring = false, TimeSpan? recurrenceTime = null) : base(clone)
{
this.mangaConnector = connector;
this.progressToken = new ProgressToken(taskIncrements);

View File

@ -14,13 +14,6 @@ public class JobBoss : GlobalBase
this.mangaConnectorJobQueue = new();
}
public void MonitorJobs()
{
foreach (Job job in jobs.Where(job => job.nextExecution < DateTime.Now && !QueueContainsJob(job)).OrderBy(job => job.nextExecution))
AddJobToQueue(job);
CheckJobQueue();
}
public void AddJob(Job job)
{
this.jobs.Add(job);
@ -53,8 +46,10 @@ public class JobBoss : GlobalBase
AddJobToQueue(job);
}
private void CheckJobQueue()
public void CheckJobs()
{
foreach (Job job in jobs.Where(job => job.nextExecution < DateTime.Now && !QueueContainsJob(job)).OrderBy(job => job.nextExecution))
AddJobToQueue(job);
foreach (Queue<Job> jobQueue in mangaConnectorJobQueue.Values)
{
Job queueHead = jobQueue.Peek();

75
Tranga/Server.cs Normal file
View File

@ -0,0 +1,75 @@
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using Newtonsoft.Json;
namespace Tranga;
public class Server : GlobalBase
{
private readonly HttpListener _listener = new ();
private readonly Tranga _parent;
public Server(Tranga parent) : base(parent)
{
this._parent = parent;
if(RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
this._listener.Prefixes.Add($"http://*:{settings.apiPortNumber}/");
else
this._listener.Prefixes.Add($"http://localhost:{settings.apiPortNumber}/");
Thread t = new (Listen);
t.Start();
}
private void Listen()
{
this._listener.Start();
foreach(string prefix in this._listener.Prefixes)
Log($"Listening on {prefix}");
while (this._listener.IsListening && _parent.keepRunning)
{
HttpListenerContext context = this._listener.GetContextAsync().Result;
Log($"{context.Request.HttpMethod} {context.Request.Url} {context.Request.UserAgent}");
Task t = new(() =>
{
if(context.Request.HttpMethod == "OPTIONS")
SendResponse(HttpStatusCode.OK, context.Response);
else
HandleRequest(context);
});
t.Start();
}
}
private void HandleRequest(HttpListenerContext context)
{
HttpListenerRequest request = context.Request;
HttpListenerResponse response = context.Response;
if(request.Url!.LocalPath.Contains("favicon"))
SendResponse(HttpStatusCode.NoContent, response);
SendResponse(HttpStatusCode.NotFound, response);
}
private void SendResponse(HttpStatusCode statusCode, HttpListenerResponse response, object? content = null)
{
//logger?.WriteLine(this.GetType().ToString(), $"Sending response: {statusCode}");
response.StatusCode = (int)statusCode;
response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With");
response.AddHeader("Access-Control-Allow-Methods", "GET, POST, DELETE");
response.AddHeader("Access-Control-Max-Age", "1728000");
response.AppendHeader("Access-Control-Allow-Origin", "*");
response.ContentType = "application/json";
try
{
response.OutputStream.Write(content is not null
? Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(content))
: Array.Empty<byte>());
response.OutputStream.Close();
}
catch (HttpListenerException)
{
}
}
}

32
Tranga/Tranga.cs Normal file
View File

@ -0,0 +1,32 @@
using Logging;
using Tranga.Jobs;
namespace Tranga;
public partial class Tranga : GlobalBase
{
public bool keepRunning;
private JobBoss _jobBoss;
private Server server;
public Tranga(Logger? logger, TrangaSettings settings) : base(logger, settings)
{
keepRunning = true;
_jobBoss = new(this);
StartJobBoss();
this.server = new Server(this);
}
private void StartJobBoss()
{
Thread t = new (() =>
{
while (keepRunning)
{
_jobBoss.CheckJobs();
Thread.Sleep(1000);
}
});
t.Start();
}
}

136
Tranga/TrangaArgs.cs Normal file
View File

@ -0,0 +1,136 @@
using Logging;
namespace Tranga;
public partial class Tranga : GlobalBase
{
public static void Main(string[] args)
{
string[]? help = GetArg(args, ArgEnum.Help);
if (help is not null)
{
PrintHelp();
return;
}
string[]? consoleLogger = GetArg(args, ArgEnum.ConsoleLogger);
string[]? fileLogger = GetArg(args, ArgEnum.FileLogger);
string? filePath = fileLogger?[0];//TODO validate path
List<Logger.LoggerType> enabledLoggers = new();
if(consoleLogger is not null)
enabledLoggers.Add(Logger.LoggerType.ConsoleLogger);
if (fileLogger is not null)
enabledLoggers.Add(Logger.LoggerType.FileLogger);
Logger logger = new(enabledLoggers.ToArray(), Console.Out, Console.OutputEncoding, filePath);
TrangaSettings? settings = null;
string[]? downloadLocationPath = GetArg(args, ArgEnum.DownloadLocation);
string[]? workingDirectory = GetArg(args, ArgEnum.WorkingDirectory);
if (downloadLocationPath is not null && workingDirectory is not null)
{
settings = new TrangaSettings(downloadLocationPath[0], workingDirectory[0]);
}else if (downloadLocationPath is not null)
{
if (settings is null)
settings = new TrangaSettings(downloadLocation: downloadLocationPath[0]);
else
settings = new TrangaSettings(downloadLocation: downloadLocationPath[0], settings.workingDirectory);
}else if (workingDirectory is not null)
{
if (settings is null)
settings = new TrangaSettings(downloadLocation: workingDirectory[0]);
else
settings = new TrangaSettings(settings.downloadLocation, workingDirectory[0]);
}
else
{
settings = new TrangaSettings();
}
Directory.CreateDirectory(settings.downloadLocation);//TODO validate path
Directory.CreateDirectory(settings.workingDirectory);//TODO validate path
Tranga _ = new (logger, settings);
}
private static void PrintHelp()
{
Console.WriteLine("Tranga-Help:");
foreach (Argument argument in arguments.Values)
{
foreach(string name in argument.names)
Console.Write("{0} ", name);
if(argument.parameterCount > 0)
Console.Write($"<{argument.parameterCount}>");
Console.Write("\r\n {0}\r\n", argument.helpText);
}
}
/// <summary>
/// Returns an array containing the parameters for the argument.
/// </summary>
/// <param name="args">List of argument-strings</param>
/// <param name="arg">Requested parameter</param>
/// <returns>
/// If there are no parameters for an argument, returns an empty array.
/// If the argument is not found returns null.
/// </returns>
private static string[]? GetArg(string[] args, ArgEnum arg)
{
List<string> argsList = args.ToList();
List<string> ret = new();
foreach (string name in arguments[arg].names)
{
int argIndex = argsList.IndexOf(name);
if (argIndex != -1)
{
if (arguments[arg].parameterCount == 0)
return ret.ToArray();
for (int parameterIndex = 1; parameterIndex <= arguments[arg].parameterCount; parameterIndex++)
{
if(argIndex + parameterIndex >= argsList.Count || args[argIndex + parameterIndex].Contains('-'))//End of arguments, or no parameter provided, when one is required
Console.WriteLine($"No parameter provided for argument {name}. -h for help.");
ret.Add(args[argIndex + parameterIndex]);
}
}
}
return ret.Any() ? ret.ToArray() : null;
}
private static Dictionary<ArgEnum, Argument> arguments = new()
{
{ ArgEnum.DownloadLocation, new(new []{"-d", "--downloadLocation"}, 1, "Directory to which downloaded Manga are saved") },
{ ArgEnum.WorkingDirectory, new(new []{"-w", "--workingDirectory"}, 1, "Directory in which application-data is saved") },
{ ArgEnum.ConsoleLogger, new(new []{"-c", "--consoleLogger"}, 0, "Enables the consoleLogger") },
{ ArgEnum.FileLogger, new(new []{"-f", "--fileLogger"}, 1, "Enables the fileLogger, Directory where logfiles are saved") },
{ ArgEnum.Help, new(new []{"-h", "--help"}, 0, "Print this") }
//{ ArgEnum., new(new []{""}, 1, "") }
};
internal enum ArgEnum
{
TrangaSettings,
DownloadLocation,
WorkingDirectory,
ConsoleLogger,
FileLogger,
Help
}
private struct Argument
{
public string[] names { get; }
public byte parameterCount { get; }
public string helpText { get; }
public Argument(string[] names, byte parameterCount, string helpText)
{
this.names = names;
this.parameterCount = parameterCount;
this.helpText = helpText;
}
}
}

View File

@ -9,38 +9,59 @@ public class TrangaSettings
{
public string downloadLocation { get; private set; }
public string workingDirectory { get; init; }
public int apiPortNumber { get; init; }
[JsonIgnore] public string settingsFilePath => Path.Join(workingDirectory, "settings.json");
[JsonIgnore] public string libraryConnectorsFilePath => Path.Join(workingDirectory, "libraryConnectors.json");
[JsonIgnore] public string notificationConnectorsFilePath => Path.Join(workingDirectory, "notificationConnectors.json");
[JsonIgnore] public string tasksFilePath => Path.Join(workingDirectory, "tasks.json");
[JsonIgnore] public string coverImageCache => Path.Join(workingDirectory, "imageCache");
public ushort? version { get; set; }
public TrangaSettings(string? downloadLocation = null, string? workingDirectory = null)
public TrangaSettings(string? downloadLocation = null, string? workingDirectory = null, int apiPortNumber = 6531)
{
this.apiPortNumber = apiPortNumber;
downloadLocation ??= Path.Join(Directory.GetCurrentDirectory(), "Downloads");
workingDirectory ??= Directory.GetCurrentDirectory();
if (downloadLocation.Length < 1 || workingDirectory.Length < 1)
throw new ArgumentException("Download-location and working-directory paths can not be empty!");
this.workingDirectory = workingDirectory;
this.downloadLocation = downloadLocation;
if (File.Exists(settingsFilePath))
{
TrangaSettings settings = JsonConvert.DeserializeObject<TrangaSettings>(File.ReadAllText(settingsFilePath))!;
this.downloadLocation = settings.downloadLocation;
this.workingDirectory = settings.workingDirectory;
}
}
public static TrangaSettings LoadSettings(string importFilePath, Logger? logger)
public HashSet<LibraryConnector> LoadLibraryConnectors()
{
if (!File.Exists(importFilePath))
return new TrangaSettings();
string toRead = File.ReadAllText(importFilePath);
TrangaSettings? settings = JsonConvert.DeserializeObject<TrangaSettings>(File.ReadAllText(importFilePath),
new JsonSerializerSettings
if (!File.Exists(libraryConnectorsFilePath))
return new HashSet<LibraryConnector>();
return JsonConvert.DeserializeObject<HashSet<LibraryConnector>>(File.ReadAllText(libraryConnectorsFilePath),
new JsonSerializerSettings()
{
Converters =
{
new NotificationManagerJsonConverter(),
new LibraryManagerJsonConverter()
}
});
return settings ?? new TrangaSettings();
})!;
}
public HashSet<NotificationConnector> LoadNotificationConnectors()
{
if (!File.Exists(notificationConnectorsFilePath))
return new HashSet<NotificationConnector>();
return JsonConvert.DeserializeObject<HashSet<NotificationConnector>>(File.ReadAllText(libraryConnectorsFilePath),
new JsonSerializerSettings()
{
Converters =
{
new NotificationManagerJsonConverter()
}
})!;
}
public void ExportSettings()