From ebe3012c694ea1006b2fae995007f90287e87c8c Mon Sep 17 00:00:00 2001 From: Glax Date: Sat, 1 Jun 2024 22:08:59 +0200 Subject: [PATCH 1/3] NTFY check endpoint URI and add optional custom topic #183 --- Tranga/NotificationConnectors/Ntfy.cs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/Tranga/NotificationConnectors/Ntfy.cs b/Tranga/NotificationConnectors/Ntfy.cs index fef0f90..c725b8a 100644 --- a/Tranga/NotificationConnectors/Ntfy.cs +++ b/Tranga/NotificationConnectors/Ntfy.cs @@ -1,4 +1,5 @@ using System.Text; +using System.Text.RegularExpressions; using Newtonsoft.Json; namespace Tranga.NotificationConnectors; @@ -8,7 +9,7 @@ public class Ntfy : NotificationConnector // ReSharper disable twice MemberCanBePrivate.Global public string endpoint { get; init; } public string auth { get; init; } - private const string Topic = "tranga"; + private readonly string _topic = "tranga"; private readonly HttpClient _client = new(); [JsonConstructor] @@ -16,19 +17,25 @@ public class Ntfy : NotificationConnector { if (!baseUrlRex.IsMatch(endpoint)) throw new ArgumentException("endpoint does not match pattern"); - this.endpoint = endpoint; + Regex rootUriRex = new(@"(https?:\/\/[a-zA-Z0-9-\.]+\.[a-zA-Z0-9]+)(?:\/([a-zA-Z0-9-\.]+))?.*"); + Match match = rootUriRex.Match(endpoint); + if(!match.Success) + Log($"Error getting URI from provided endpoint-URI: {endpoint}"); + this.endpoint = match.Groups[1].Value; + if (match.Groups[2].Success) + _topic = match.Groups[2].Value; this.auth = auth; } public override string ToString() { - return $"Ntfy {endpoint} {Topic}"; + return $"Ntfy {endpoint} {_topic}"; } public override void SendNotification(string title, string notificationText) { Log($"Sending notification: {title} - {notificationText}"); - MessageData message = new(title, notificationText); + MessageData message = new(title, _topic, notificationText); HttpRequestMessage request = new(HttpMethod.Post, $"{this.endpoint}?auth={this.auth}"); request.Content = new StringContent(JsonConvert.SerializeObject(message, Formatting.None), Encoding.UTF8, "application/json"); HttpResponseMessage response = _client.Send(request); @@ -47,9 +54,9 @@ public class Ntfy : NotificationConnector public string message { get; } public int priority { get; } - public MessageData(string title, string message) + public MessageData(string title, string topic, string message) { - this.topic = Topic; + this.topic = topic; this.title = title; this.message = message; this.priority = 3; From 7b9e935db7565b2d93e65405da0b08008f717fdc Mon Sep 17 00:00:00 2001 From: Glax Date: Sat, 1 Jun 2024 22:10:09 +0200 Subject: [PATCH 2/3] Commented optional second level only domains for cover-image-names --- Tranga/MangaConnectors/MangaConnector.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Tranga/MangaConnectors/MangaConnector.cs b/Tranga/MangaConnectors/MangaConnector.cs index 21418b8..809cc3b 100644 --- a/Tranga/MangaConnectors/MangaConnector.cs +++ b/Tranga/MangaConnectors/MangaConnector.cs @@ -288,6 +288,7 @@ public abstract class MangaConnector : GlobalBase protected string SaveCoverImageToCache(string url, string mangaInternalId, RequestType requestType) { Regex urlRex = new (@"https?:\/\/((?:[a-zA-Z0-9-]+\.)+[a-zA-Z0-9]+)\/(?:.+\/)*(.+\.([a-zA-Z]+))"); + //https?:\/\/[a-zA-Z0-9-]+\.([a-zA-Z0-9-]+\.[a-zA-Z0-9]+)\/(?:.+\/)*(.+\.([a-zA-Z]+)) for only second level domains Match match = urlRex.Match(url); string filename = $"{match.Groups[1].Value}-{mangaInternalId}.{match.Groups[3].Value}"; string saveImagePath = Path.Join(settings.coverImageCache, filename); From aea4c0c61b0c5623fb3b801582536b5b82e90557 Mon Sep 17 00:00:00 2001 From: Glax Date: Sun, 2 Jun 2024 00:09:03 +0200 Subject: [PATCH 3/3] Add GlaxArguments to fetch Runtime-Args --- Tranga/Tranga.csproj | 1 + Tranga/TrangaArgs.cs | 132 ++++++++++--------------------------------- 2 files changed, 31 insertions(+), 102 deletions(-) diff --git a/Tranga/Tranga.csproj b/Tranga/Tranga.csproj index 47b765f..58cb6de 100644 --- a/Tranga/Tranga.csproj +++ b/Tranga/Tranga.csproj @@ -8,6 +8,7 @@ + diff --git a/Tranga/TrangaArgs.cs b/Tranga/TrangaArgs.cs index a4cf2ec..e95ad91 100644 --- a/Tranga/TrangaArgs.cs +++ b/Tranga/TrangaArgs.cs @@ -1,4 +1,5 @@ using Logging; +using GlaxArguments; namespace Tranga; @@ -7,46 +8,53 @@ public partial class Tranga : GlobalBase public static void Main(string[] args) { - Console.WriteLine(string.Join(' ', args)); - string[]? help = GetArg(args, ArgEnum.Help); - if (help is not null) - { - PrintHelp(); - return; - } + Argument downloadLocation = new (new[] { "-d", "--downloadLocation" }, 1, "Directory to which downloaded Manga are saved"); + Argument workingDirectory = new (new[] { "-w", "--workingDirectory" }, 1, "Directory in which application-data is saved"); + Argument consoleLogger = new (new []{"-c", "--consoleLogger"}, 0, "Enables the consoleLogger"); + Argument fileLogger = new (new []{"-f", "--fileLogger"}, 0, "Enables the fileLogger"); + Argument fPath = new (new []{"-l", "--fPath"}, 1, "Log Folder Path"); - string[]? consoleLogger = GetArg(args, ArgEnum.ConsoleLogger); - string[]? fileLogger = GetArg(args, ArgEnum.FileLogger); - string? directoryPath = GetArg(args, ArgEnum.FileLoggerPath)?[0]; + Argument[] arguments = new[] + { + downloadLocation, + workingDirectory, + consoleLogger, + fileLogger, + fPath + }; + ArgumentFetcher fetcher = new (arguments); + Dictionary fetched = fetcher.Fetch(args); + + string? directoryPath = fetched.TryGetValue(fPath, out string[]? path) ? path[0] : null; if (directoryPath is not null && !Directory.Exists(directoryPath)) Directory.CreateDirectory(directoryPath); List enabledLoggers = new(); - if(consoleLogger is not null) + if(fetched.ContainsKey(consoleLogger)) enabledLoggers.Add(Logger.LoggerType.ConsoleLogger); - if (fileLogger is not null) + if (fetched.ContainsKey(fileLogger)) enabledLoggers.Add(Logger.LoggerType.FileLogger); Logger logger = new(enabledLoggers.ToArray(), Console.Out, Console.OutputEncoding, directoryPath); TrangaSettings? settings = null; - string[]? downloadLocationPath = GetArg(args, ArgEnum.DownloadLocation); - string[]? workingDirectory = GetArg(args, ArgEnum.WorkingDirectory); + bool dlp = fetched.TryGetValue(downloadLocation, out string[]? downloadLocationPath); + bool wdp = fetched.TryGetValue(downloadLocation, out string[]? workingDirectoryPath); - if (downloadLocationPath is not null && workingDirectory is not null) + if (dlp && wdp) { - settings = new TrangaSettings(downloadLocationPath[0], workingDirectory[0]); - }else if (downloadLocationPath is not null) + settings = new TrangaSettings(downloadLocationPath![0], workingDirectoryPath![0]); + }else if (dlp) { if (settings is null) - settings = new TrangaSettings(downloadLocation: downloadLocationPath[0]); + settings = new TrangaSettings(downloadLocation: downloadLocationPath![0]); else - settings = new TrangaSettings(downloadLocation: downloadLocationPath[0], settings.workingDirectory); - }else if (workingDirectory is not null) + settings = new TrangaSettings(downloadLocation: downloadLocationPath![0], settings.workingDirectory); + }else if (wdp) { if (settings is null) - settings = new TrangaSettings(downloadLocation: workingDirectory[0]); + settings = new TrangaSettings(downloadLocation: workingDirectoryPath![0]); else - settings = new TrangaSettings(settings.downloadLocation, workingDirectory[0]); + settings = new TrangaSettings(settings.downloadLocation, workingDirectoryPath![0]); } else { @@ -58,84 +66,4 @@ public partial class Tranga : GlobalBase 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); - } - } - - /// - /// Returns an array containing the parameters for the argument. - /// - /// List of argument-strings - /// Requested parameter - /// - /// If there are no parameters for an argument, returns an empty array. - /// If the argument is not found returns null. - /// - private static string[]? GetArg(string[] args, ArgEnum arg) - { - List argsList = args.ToList(); - List 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 readonly Dictionary 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"}, 0, "Enables the fileLogger") }, - { ArgEnum.FileLoggerPath, new (new []{"-l", "--fPath"}, 1, "Log Folder Path" ) }, - { ArgEnum.Help, new(new []{"-h", "--help"}, 0, "Print this") } - //{ ArgEnum., new(new []{""}, 1, "") } - }; - - internal enum ArgEnum - { - TrangaSettings, - DownloadLocation, - WorkingDirectory, - ConsoleLogger, - FileLogger, - FileLoggerPath, - 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; - } - } } \ No newline at end of file