2
0

Moved logger to Trangasettings

This commit is contained in:
glax 2023-07-30 17:08:43 +02:00
parent b4bef25a22
commit 000539d6a6
13 changed files with 107 additions and 120 deletions

View File

@ -29,22 +29,22 @@ public class Program
if (File.Exists(settingsFilePath)) if (File.Exists(settingsFilePath))
settings = TrangaSettings.LoadSettings(settingsFilePath, logger); settings = TrangaSettings.LoadSettings(settingsFilePath, logger);
else else
settings = new TrangaSettings(downloadFolderPath, applicationFolderPath, new HashSet<LibraryManager>(), new HashSet<NotificationManager>()); settings = new TrangaSettings(downloadFolderPath, applicationFolderPath, new HashSet<LibraryManager>(), new HashSet<NotificationManager>(), logger);
Directory.CreateDirectory(settings.workingDirectory); Directory.CreateDirectory(settings.workingDirectory);
Directory.CreateDirectory(settings.downloadLocation); Directory.CreateDirectory(settings.downloadLocation);
Directory.CreateDirectory(settings.coverImageCache); Directory.CreateDirectory(settings.coverImageCache);
logger.WriteLine("Tranga",$"Application-Folder: {settings.workingDirectory}"); settings.logger?.WriteLine("Tranga",$"Application-Folder: {settings.workingDirectory}");
logger.WriteLine("Tranga",$"Settings-File-Path: {settings.settingsFilePath}"); settings.logger?.WriteLine("Tranga",$"Settings-File-Path: {settings.settingsFilePath}");
logger.WriteLine("Tranga",$"Download-Folder-Path: {settings.downloadLocation}"); settings.logger?.WriteLine("Tranga",$"Download-Folder-Path: {settings.downloadLocation}");
logger.WriteLine("Tranga",$"Logfile-Path: {logFilePath}"); settings.logger?.WriteLine("Tranga",$"Logfile-Path: {logFilePath}");
logger.WriteLine("Tranga",$"Image-Cache-Path: {settings.coverImageCache}"); settings.logger?.WriteLine("Tranga",$"Image-Cache-Path: {settings.coverImageCache}");
logger.WriteLine("Tranga", "Loading Taskmanager."); settings.logger?.WriteLine("Tranga", "Loading Taskmanager.");
TaskManager taskManager = new (settings, logger); TaskManager taskManager = new (settings);
Server server = new (6531, taskManager, logger); Server server = new (6531, taskManager);
foreach(NotificationManager nm in taskManager.settings.notificationManagers) foreach(NotificationManager nm in taskManager.settings.notificationManagers)
nm.SendNotification("Tranga-API", "Started Tranga-API"); nm.SendNotification("Tranga-API", "Started Tranga-API");
} }

View File

@ -247,17 +247,17 @@ public class RequestHandler
variables.TryGetValue("lunaseaWebhook", out string? lunaseaWebhook); variables.TryGetValue("lunaseaWebhook", out string? lunaseaWebhook);
if (downloadLocation is not null && downloadLocation.Length > 0) if (downloadLocation is not null && downloadLocation.Length > 0)
_taskManager.settings.UpdateSettings(TrangaSettings.UpdateField.DownloadLocation, _parent.logger, downloadLocation); _taskManager.settings.UpdateSettings(TrangaSettings.UpdateField.DownloadLocation, downloadLocation);
if (komgaUrl is not null && komgaAuth is not null && komgaUrl.Length > 5 && komgaAuth.Length > 0) if (komgaUrl is not null && komgaAuth is not null && komgaUrl.Length > 5 && komgaAuth.Length > 0)
_taskManager.settings.UpdateSettings(TrangaSettings.UpdateField.Komga, _parent.logger, komgaUrl, komgaAuth); _taskManager.settings.UpdateSettings(TrangaSettings.UpdateField.Komga, komgaUrl, komgaAuth);
if (kavitaUrl is not null && kavitaPassword is not null && kavitaUsername is not null && kavitaUrl.Length > 5 && if (kavitaUrl is not null && kavitaPassword is not null && kavitaUsername is not null && kavitaUrl.Length > 5 &&
kavitaUsername.Length > 0 && kavitaPassword.Length > 0) kavitaUsername.Length > 0 && kavitaPassword.Length > 0)
_taskManager.settings.UpdateSettings(TrangaSettings.UpdateField.Kavita, _parent.logger, kavitaUrl, kavitaUsername, _taskManager.settings.UpdateSettings(TrangaSettings.UpdateField.Kavita, kavitaUrl, kavitaUsername,
kavitaPassword); kavitaPassword);
if (gotifyUrl is not null && gotifyAppToken is not null && gotifyUrl.Length > 5 && gotifyAppToken.Length > 0) if (gotifyUrl is not null && gotifyAppToken is not null && gotifyUrl.Length > 5 && gotifyAppToken.Length > 0)
_taskManager.settings.UpdateSettings(TrangaSettings.UpdateField.Gotify, _parent.logger, gotifyUrl, gotifyAppToken); _taskManager.settings.UpdateSettings(TrangaSettings.UpdateField.Gotify, gotifyUrl, gotifyAppToken);
if(lunaseaWebhook is not null && lunaseaWebhook.Length > 5) if(lunaseaWebhook is not null && lunaseaWebhook.Length > 5)
_taskManager.settings.UpdateSettings(TrangaSettings.UpdateField.LunaSea, _parent.logger, lunaseaWebhook); _taskManager.settings.UpdateSettings(TrangaSettings.UpdateField.LunaSea, lunaseaWebhook);
break; break;
} }
} }

View File

@ -32,16 +32,16 @@ public static class Tranga_Cli
Logger logger = new(new[] { Logger.LoggerType.FileLogger }, null, Console.Out.Encoding, logFilePath); Logger logger = new(new[] { Logger.LoggerType.FileLogger }, null, Console.Out.Encoding, logFilePath);
logger.WriteLine("Tranga_CLI", "Loading Taskmanager."); logger.WriteLine("Tranga_CLI", "Loading Taskmanager.");
TrangaSettings settings = File.Exists(settingsFilePath) ? TrangaSettings.LoadSettings(settingsFilePath, logger) : new TrangaSettings(Directory.GetCurrentDirectory(), applicationFolderPath, new HashSet<LibraryManager>(), new HashSet<NotificationManager>()); TrangaSettings settings = File.Exists(settingsFilePath) ? TrangaSettings.LoadSettings(settingsFilePath, logger) : new TrangaSettings(Directory.GetCurrentDirectory(), applicationFolderPath, new HashSet<LibraryManager>(), new HashSet<NotificationManager>(), logger);
logger.WriteLine("Tranga_CLI", "User Input"); settings.logger?.WriteLine("Tranga_CLI", "User Input");
Console.WriteLine($"Output folder path [{settings.downloadLocation}]:"); Console.WriteLine($"Output folder path [{settings.downloadLocation}]:");
string? tmpPath = Console.ReadLine(); string? tmpPath = Console.ReadLine();
while(tmpPath is null) while(tmpPath is null)
tmpPath = Console.ReadLine(); tmpPath = Console.ReadLine();
if (tmpPath.Length > 0) if (tmpPath.Length > 0)
settings.UpdateSettings(TrangaSettings.UpdateField.DownloadLocation, logger, tmpPath); settings.UpdateSettings(TrangaSettings.UpdateField.DownloadLocation, tmpPath);
Console.WriteLine($"Komga BaseURL [{settings.libraryManagers.FirstOrDefault(lm => lm.GetType() == typeof(Komga))?.baseUrl}]:"); Console.WriteLine($"Komga BaseURL [{settings.libraryManagers.FirstOrDefault(lm => lm.GetType() == typeof(Komga))?.baseUrl}]:");
string? tmpUrlKomga = Console.ReadLine(); string? tmpUrlKomga = Console.ReadLine();
@ -74,7 +74,7 @@ public static class Tranga_Cli
} }
} while (key != ConsoleKey.Enter); } while (key != ConsoleKey.Enter);
settings.UpdateSettings(TrangaSettings.UpdateField.Komga, logger, tmpUrlKomga, tmpKomgaUser, tmpKomgaPass); settings.UpdateSettings(TrangaSettings.UpdateField.Komga, tmpUrlKomga, tmpKomgaUser, tmpKomgaPass);
} }
Console.WriteLine($"Kavita BaseURL [{settings.libraryManagers.FirstOrDefault(lm => lm.GetType() == typeof(Kavita))?.baseUrl}]:"); Console.WriteLine($"Kavita BaseURL [{settings.libraryManagers.FirstOrDefault(lm => lm.GetType() == typeof(Kavita))?.baseUrl}]:");
@ -108,7 +108,7 @@ public static class Tranga_Cli
} }
} while (key != ConsoleKey.Enter); } while (key != ConsoleKey.Enter);
settings.UpdateSettings(TrangaSettings.UpdateField.Kavita, logger, tmpUrlKavita, tmpKavitaUser, tmpKavitaPass); settings.UpdateSettings(TrangaSettings.UpdateField.Kavita, tmpUrlKavita, tmpKavitaUser, tmpKavitaPass);
} }
Console.WriteLine($"Gotify BaseURL [{((Gotify?)settings.notificationManagers.FirstOrDefault(lm => lm.GetType() == typeof(Gotify)))?.endpoint}]:"); Console.WriteLine($"Gotify BaseURL [{((Gotify?)settings.notificationManagers.FirstOrDefault(lm => lm.GetType() == typeof(Gotify)))?.endpoint}]:");
@ -122,7 +122,7 @@ public static class Tranga_Cli
while (tmpGotifyAppToken is null || tmpGotifyAppToken.Length < 1) while (tmpGotifyAppToken is null || tmpGotifyAppToken.Length < 1)
tmpGotifyAppToken = Console.ReadLine(); tmpGotifyAppToken = Console.ReadLine();
settings.UpdateSettings(TrangaSettings.UpdateField.Gotify, logger, tmpGotifyUrl, tmpGotifyAppToken); settings.UpdateSettings(TrangaSettings.UpdateField.Gotify, tmpGotifyUrl, tmpGotifyAppToken);
} }
logger.WriteLine("Tranga_CLI", "Loaded."); logger.WriteLine("Tranga_CLI", "Loaded.");
@ -133,7 +133,7 @@ public static class Tranga_Cli
private static void TaskMode(TrangaSettings settings, Logger logger) private static void TaskMode(TrangaSettings settings, Logger logger)
{ {
TaskManager taskManager = new (settings, logger); TaskManager taskManager = new (settings);
ConsoleKey selection = ConsoleKey.EraseEndOfFile; ConsoleKey selection = ConsoleKey.EraseEndOfFile;
PrintMenu(taskManager, taskManager.settings.downloadLocation); PrintMenu(taskManager, taskManager.settings.downloadLocation);
while (selection != ConsoleKey.Q) while (selection != ConsoleKey.Q)

View File

@ -3,7 +3,6 @@ using System.IO.Compression;
using System.Net; using System.Net;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using Logging;
using Tranga.TrangaTasks; using Tranga.TrangaTasks;
using static System.IO.UnixFileMode; using static System.IO.UnixFileMode;
@ -18,13 +17,9 @@ public abstract class Connector
protected TrangaSettings settings { get; } protected TrangaSettings settings { get; }
internal DownloadClient downloadClient { get; init; } = null!; internal DownloadClient downloadClient { get; init; } = null!;
protected readonly Logger? logger; protected Connector(TrangaSettings settings)
protected Connector(TrangaSettings settings, Logger? logger = null)
{ {
this.settings = settings; this.settings = settings;
this.logger = logger;
if (!Directory.Exists(settings.coverImageCache)) if (!Directory.Exists(settings.coverImageCache))
Directory.CreateDirectory(settings.coverImageCache); Directory.CreateDirectory(settings.coverImageCache);
} }
@ -68,11 +63,11 @@ public abstract class Connector
Chapter[] newChapters = this.GetChapters(publication, language); Chapter[] newChapters = this.GetChapters(publication, language);
collection.Add(publication); collection.Add(publication);
NumberFormatInfo decimalPoint = new (){ NumberDecimalSeparator = "." }; NumberFormatInfo decimalPoint = new (){ NumberDecimalSeparator = "." };
logger?.WriteLine(this.GetType().ToString(), "Checking for duplicates"); settings.logger?.WriteLine(this.GetType().ToString(), "Checking for duplicates");
List<Chapter> newChaptersList = newChapters.Where(nChapter => List<Chapter> newChaptersList = newChapters.Where(nChapter =>
float.Parse(nChapter.chapterNumber, decimalPoint) > publication.ignoreChaptersBelow && float.Parse(nChapter.chapterNumber, decimalPoint) > publication.ignoreChaptersBelow &&
!nChapter.CheckChapterIsDownloaded(settings.downloadLocation)).ToList(); !nChapter.CheckChapterIsDownloaded(settings.downloadLocation)).ToList();
logger?.WriteLine(this.GetType().ToString(), $"{newChaptersList.Count} new chapters."); settings.logger?.WriteLine(this.GetType().ToString(), $"{newChaptersList.Count} new chapters.");
return newChaptersList; return newChaptersList;
} }
@ -167,19 +162,19 @@ public abstract class Connector
/// <param name="settings">TrangaSettings</param> /// <param name="settings">TrangaSettings</param>
public void CopyCoverFromCacheToDownloadLocation(Publication publication, TrangaSettings settings) public void CopyCoverFromCacheToDownloadLocation(Publication publication, TrangaSettings settings)
{ {
logger?.WriteLine(this.GetType().ToString(), $"Cloning cover {publication.sortName} -> {publication.internalId}"); settings.logger?.WriteLine(this.GetType().ToString(), $"Cloning cover {publication.sortName} -> {publication.internalId}");
//Check if Publication already has a Folder and cover //Check if Publication already has a Folder and cover
string publicationFolder = publication.CreatePublicationFolder(settings.downloadLocation); string publicationFolder = publication.CreatePublicationFolder(settings.downloadLocation);
DirectoryInfo dirInfo = new (publicationFolder); DirectoryInfo dirInfo = new (publicationFolder);
if (dirInfo.EnumerateFiles().Any(info => info.Name.Contains("cover", StringComparison.InvariantCultureIgnoreCase))) if (dirInfo.EnumerateFiles().Any(info => info.Name.Contains("cover", StringComparison.InvariantCultureIgnoreCase)))
{ {
logger?.WriteLine(this.GetType().ToString(), $"Cover exists {publication.sortName}"); settings.logger?.WriteLine(this.GetType().ToString(), $"Cover exists {publication.sortName}");
return; return;
} }
string fileInCache = Path.Join(settings.coverImageCache, publication.coverFileNameInCache); string fileInCache = Path.Join(settings.coverImageCache, publication.coverFileNameInCache);
string newFilePath = Path.Join(publicationFolder, $"cover.{Path.GetFileName(fileInCache).Split('.')[^1]}" ); string newFilePath = Path.Join(publicationFolder, $"cover.{Path.GetFileName(fileInCache).Split('.')[^1]}" );
logger?.WriteLine(this.GetType().ToString(), $"Cloning cover {fileInCache} -> {newFilePath}"); settings.logger?.WriteLine(this.GetType().ToString(), $"Cloning cover {fileInCache} -> {newFilePath}");
File.Copy(fileInCache, newFilePath, true); File.Copy(fileInCache, newFilePath, true);
if(RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) if(RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
File.SetUnixFileMode(newFilePath, GroupRead | GroupWrite | OtherRead | OtherWrite | UserRead | UserWrite); File.SetUnixFileMode(newFilePath, GroupRead | GroupWrite | OtherRead | OtherWrite | UserRead | UserWrite);
@ -217,7 +212,7 @@ public abstract class Connector
{ {
if (cancellationToken?.IsCancellationRequested ?? false) if (cancellationToken?.IsCancellationRequested ?? false)
return HttpStatusCode.RequestTimeout; return HttpStatusCode.RequestTimeout;
logger?.WriteLine("Connector", $"Downloading Images for {saveArchiveFilePath}"); settings.logger?.WriteLine("Connector", $"Downloading Images for {saveArchiveFilePath}");
//Check if Publication Directory already exists //Check if Publication Directory already exists
string directoryPath = Path.GetDirectoryName(saveArchiveFilePath)!; string directoryPath = Path.GetDirectoryName(saveArchiveFilePath)!;
if (!Directory.Exists(directoryPath)) if (!Directory.Exists(directoryPath))
@ -235,7 +230,7 @@ public abstract class Connector
{ {
string[] split = imageUrl.Split('.'); string[] split = imageUrl.Split('.');
string extension = split[^1]; string extension = split[^1];
logger?.WriteLine("Connector", $"Downloading Image {chapter + 1:000}/{imageUrls.Length:000} {parentTask.publication.sortName} {parentTask.publication.internalId} Vol.{parentTask.chapter.volumeNumber} Ch.{parentTask.chapter.chapterNumber} {parentTask.progress:P2}"); settings.logger?.WriteLine("Connector", $"Downloading Image {chapter + 1:000}/{imageUrls.Length:000} {parentTask.publication.sortName} {parentTask.publication.internalId} Vol.{parentTask.chapter.volumeNumber} Ch.{parentTask.chapter.chapterNumber} {parentTask.progress:P2}");
HttpStatusCode status = DownloadImage(imageUrl, Path.Join(tempFolder, $"{chapter++}.{extension}"), requestType, referrer); HttpStatusCode status = DownloadImage(imageUrl, Path.Join(tempFolder, $"{chapter++}.{extension}"), requestType, referrer);
if ((int)status < 200 || (int)status >= 300) if ((int)status < 200 || (int)status >= 300)
return status; return status;
@ -247,7 +242,7 @@ public abstract class Connector
if(comicInfoPath is not null) if(comicInfoPath is not null)
File.Copy(comicInfoPath, Path.Join(tempFolder, "ComicInfo.xml")); File.Copy(comicInfoPath, Path.Join(tempFolder, "ComicInfo.xml"));
logger?.WriteLine("Connector", $"Creating archive {saveArchiveFilePath}"); settings.logger?.WriteLine("Connector", $"Creating archive {saveArchiveFilePath}");
//ZIP-it and ship-it //ZIP-it and ship-it
ZipFile.CreateFromDirectory(tempFolder, saveArchiveFilePath); ZipFile.CreateFromDirectory(tempFolder, saveArchiveFilePath);
if(RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) if(RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
@ -269,7 +264,7 @@ public abstract class Connector
using MemoryStream ms = new(); using MemoryStream ms = new();
coverResult.result.CopyTo(ms); coverResult.result.CopyTo(ms);
File.WriteAllBytes(saveImagePath, ms.ToArray()); File.WriteAllBytes(saveImagePath, ms.ToArray());
logger?.WriteLine(this.GetType().ToString(), $"Saving image to {saveImagePath}"); settings.logger?.WriteLine(this.GetType().ToString(), $"Saving image to {saveImagePath}");
return filename; return filename;
} }
} }

View File

@ -2,7 +2,6 @@
using System.Net; using System.Net;
using System.Text.Json; using System.Text.Json;
using System.Text.Json.Nodes; using System.Text.Json.Nodes;
using Logging;
using Tranga.TrangaTasks; using Tranga.TrangaTasks;
namespace Tranga.Connectors; namespace Tranga.Connectors;
@ -19,7 +18,7 @@ public class MangaDex : Connector
Author, Author,
} }
public MangaDex(TrangaSettings settings, Logger? logger = null) : base(settings, logger) public MangaDex(TrangaSettings settings) : base(settings)
{ {
name = "MangaDex"; name = "MangaDex";
this.downloadClient = new DownloadClient(new Dictionary<byte, int>() this.downloadClient = new DownloadClient(new Dictionary<byte, int>()
@ -29,12 +28,12 @@ public class MangaDex : Connector
{(byte)RequestType.AtHomeServer, 40}, {(byte)RequestType.AtHomeServer, 40},
{(byte)RequestType.CoverUrl, 250}, {(byte)RequestType.CoverUrl, 250},
{(byte)RequestType.Author, 250} {(byte)RequestType.Author, 250}
}, logger); }, settings.logger);
} }
protected override Publication[] GetPublicationsInternal(string publicationTitle = "") protected override Publication[] GetPublicationsInternal(string publicationTitle = "")
{ {
logger?.WriteLine(this.GetType().ToString(), $"Getting Publications (title={publicationTitle})"); settings.logger?.WriteLine(this.GetType().ToString(), $"Getting Publications (title={publicationTitle})");
const int limit = 100; //How many values we want returned at once const int limit = 100; //How many values we want returned at once
int offset = 0; //"Page" int offset = 0; //"Page"
int total = int.MaxValue; //How many total results are there, is updated on first request int total = int.MaxValue; //How many total results are there, is updated on first request
@ -60,7 +59,7 @@ public class MangaDex : Connector
//Loop each Manga and extract information from JSON //Loop each Manga and extract information from JSON
foreach (JsonNode? mangeNode in mangaInResult) foreach (JsonNode? mangeNode in mangaInResult)
{ {
logger?.WriteLine(this.GetType().ToString(), $"Getting publication data. {++loadedPublicationData}/{total}"); settings.logger?.WriteLine(this.GetType().ToString(), $"Getting publication data. {++loadedPublicationData}/{total}");
JsonObject manga = (JsonObject)mangeNode!; JsonObject manga = (JsonObject)mangeNode!;
JsonObject attributes = manga["attributes"]!.AsObject(); JsonObject attributes = manga["attributes"]!.AsObject();
@ -147,13 +146,13 @@ public class MangaDex : Connector
} }
} }
logger?.WriteLine(this.GetType().ToString(), $"Done getting publications (title={publicationTitle})"); settings.logger?.WriteLine(this.GetType().ToString(), $"Done getting publications (title={publicationTitle})");
return publications.ToArray(); return publications.ToArray();
} }
public override Chapter[] GetChapters(Publication publication, string language = "") public override Chapter[] GetChapters(Publication publication, string language = "")
{ {
logger?.WriteLine(this.GetType().ToString(), $"Getting Chapters for {publication.sortName} {publication.internalId} (language={language})"); settings.logger?.WriteLine(this.GetType().ToString(), $"Getting Chapters for {publication.sortName} {publication.internalId} (language={language})");
const int limit = 100; //How many values we want returned at once const int limit = 100; //How many values we want returned at once
int offset = 0; //"Page" int offset = 0; //"Page"
int total = int.MaxValue; //How many total results are there, is updated on first request int total = int.MaxValue; //How many total results are there, is updated on first request
@ -204,7 +203,7 @@ public class MangaDex : Connector
{ {
NumberDecimalSeparator = "." NumberDecimalSeparator = "."
}; };
logger?.WriteLine(this.GetType().ToString(), $"Done getting {chapters.Count} Chapters for {publication.internalId}"); settings.logger?.WriteLine(this.GetType().ToString(), $"Done getting {chapters.Count} Chapters for {publication.internalId}");
return chapters.OrderBy(chapter => Convert.ToSingle(chapter.chapterNumber, chapterNumberFormatInfo)).ToArray(); return chapters.OrderBy(chapter => Convert.ToSingle(chapter.chapterNumber, chapterNumberFormatInfo)).ToArray();
} }
@ -212,7 +211,7 @@ public class MangaDex : Connector
{ {
if (cancellationToken?.IsCancellationRequested ?? false) if (cancellationToken?.IsCancellationRequested ?? false)
return HttpStatusCode.RequestTimeout; return HttpStatusCode.RequestTimeout;
logger?.WriteLine(this.GetType().ToString(), $"Downloading Chapter-Info {publication.sortName} {publication.internalId} {chapter.volumeNumber}-{chapter.chapterNumber}"); settings.logger?.WriteLine(this.GetType().ToString(), $"Downloading Chapter-Info {publication.sortName} {publication.internalId} {chapter.volumeNumber}-{chapter.chapterNumber}");
//Request URLs for Chapter-Images //Request URLs for Chapter-Images
DownloadClient.RequestResult requestResult = DownloadClient.RequestResult requestResult =
downloadClient.MakeRequest($"https://api.mangadex.org/at-home/server/{chapter.url}?forcePort443=false'", (byte)RequestType.AtHomeServer); downloadClient.MakeRequest($"https://api.mangadex.org/at-home/server/{chapter.url}?forcePort443=false'", (byte)RequestType.AtHomeServer);
@ -239,10 +238,10 @@ public class MangaDex : Connector
private string? GetCoverUrl(string publicationId, string? posterId) private string? GetCoverUrl(string publicationId, string? posterId)
{ {
logger?.WriteLine(this.GetType().ToString(), $"Getting CoverUrl for {publicationId}"); settings.logger?.WriteLine(this.GetType().ToString(), $"Getting CoverUrl for {publicationId}");
if (posterId is null) if (posterId is null)
{ {
logger?.WriteLine(this.GetType().ToString(), $"No posterId, aborting"); settings.logger?.WriteLine(this.GetType().ToString(), $"No posterId, aborting");
return null; return null;
} }
@ -258,7 +257,7 @@ public class MangaDex : Connector
string fileName = result["data"]!["attributes"]!["fileName"]!.GetValue<string>(); string fileName = result["data"]!["attributes"]!["fileName"]!.GetValue<string>();
string coverUrl = $"https://uploads.mangadex.org/covers/{publicationId}/{fileName}"; string coverUrl = $"https://uploads.mangadex.org/covers/{publicationId}/{fileName}";
logger?.WriteLine(this.GetType().ToString(), $"Got Cover-Url for {publicationId} -> {coverUrl}"); settings.logger?.WriteLine(this.GetType().ToString(), $"Got Cover-Url for {publicationId} -> {coverUrl}");
return coverUrl; return coverUrl;
} }
@ -277,7 +276,7 @@ public class MangaDex : Connector
string authorName = result["data"]!["attributes"]!["name"]!.GetValue<string>(); string authorName = result["data"]!["attributes"]!["name"]!.GetValue<string>();
ret.Add(authorName); ret.Add(authorName);
logger?.WriteLine(this.GetType().ToString(), $"Got author {authorId} -> {authorName}"); settings.logger?.WriteLine(this.GetType().ToString(), $"Got author {authorId} -> {authorName}");
} }
return ret; return ret;
} }

View File

@ -2,7 +2,6 @@
using System.Net; using System.Net;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using HtmlAgilityPack; using HtmlAgilityPack;
using Logging;
using Tranga.TrangaTasks; using Tranga.TrangaTasks;
namespace Tranga.Connectors; namespace Tranga.Connectors;
@ -11,18 +10,18 @@ public class MangaKatana : Connector
{ {
public override string name { get; } public override string name { get; }
public MangaKatana(TrangaSettings settings, Logger? logger = null) : base(settings, logger) public MangaKatana(TrangaSettings settings) : base(settings)
{ {
this.name = "MangaKatana"; this.name = "MangaKatana";
this.downloadClient = new DownloadClient(new Dictionary<byte, int>() this.downloadClient = new DownloadClient(new Dictionary<byte, int>()
{ {
{(byte)1, 60} {(byte)1, 60}
}, logger); }, settings.logger);
} }
protected override Publication[] GetPublicationsInternal(string publicationTitle = "") protected override Publication[] GetPublicationsInternal(string publicationTitle = "")
{ {
logger?.WriteLine(this.GetType().ToString(), $"Getting Publications (title={publicationTitle})"); settings.logger?.WriteLine(this.GetType().ToString(), $"Getting Publications (title={publicationTitle})");
string sanitizedTitle = string.Concat(Regex.Matches(publicationTitle, "[A-z]* *")).ToLower().Replace(' ', '_'); string sanitizedTitle = string.Concat(Regex.Matches(publicationTitle, "[A-z]* *")).ToLower().Replace(' ', '_');
string requestUrl = $"https://mangakatana.com/?search={sanitizedTitle}&search_by=book_name"; string requestUrl = $"https://mangakatana.com/?search={sanitizedTitle}&search_by=book_name";
DownloadClient.RequestResult requestResult = DownloadClient.RequestResult requestResult =
@ -136,7 +135,7 @@ public class MangaKatana : Connector
public override Chapter[] GetChapters(Publication publication, string language = "") public override Chapter[] GetChapters(Publication publication, string language = "")
{ {
logger?.WriteLine(this.GetType().ToString(), $"Getting Chapters for {publication.sortName} {publication.internalId} (language={language})"); settings.logger?.WriteLine(this.GetType().ToString(), $"Getting Chapters for {publication.sortName} {publication.internalId} (language={language})");
string requestUrl = $"https://mangakatana.com/manga/{publication.publicationId}"; string requestUrl = $"https://mangakatana.com/manga/{publication.publicationId}";
// Leaving this in for verification if the page exists // Leaving this in for verification if the page exists
DownloadClient.RequestResult requestResult = DownloadClient.RequestResult requestResult =
@ -150,7 +149,7 @@ public class MangaKatana : Connector
NumberDecimalSeparator = "." NumberDecimalSeparator = "."
}; };
List<Chapter> chapters = ParseChaptersFromHtml(publication, requestUrl); List<Chapter> chapters = ParseChaptersFromHtml(publication, requestUrl);
logger?.WriteLine(this.GetType().ToString(), $"Done getting Chapters for {publication.internalId}"); settings.logger?.WriteLine(this.GetType().ToString(), $"Done getting Chapters for {publication.internalId}");
return chapters.OrderBy(chapter => Convert.ToSingle(chapter.chapterNumber, chapterNumberFormatInfo)).ToArray(); return chapters.OrderBy(chapter => Convert.ToSingle(chapter.chapterNumber, chapterNumberFormatInfo)).ToArray();
} }
@ -183,7 +182,7 @@ public class MangaKatana : Connector
{ {
if (cancellationToken?.IsCancellationRequested ?? false) if (cancellationToken?.IsCancellationRequested ?? false)
return HttpStatusCode.RequestTimeout; return HttpStatusCode.RequestTimeout;
logger?.WriteLine(this.GetType().ToString(), $"Downloading Chapter-Info {publication.sortName} {publication.internalId} {chapter.volumeNumber}-{chapter.chapterNumber}"); settings.logger?.WriteLine(this.GetType().ToString(), $"Downloading Chapter-Info {publication.sortName} {publication.internalId} {chapter.volumeNumber}-{chapter.chapterNumber}");
string requestUrl = chapter.url; string requestUrl = chapter.url;
// Leaving this in to check if the page exists // Leaving this in to check if the page exists
DownloadClient.RequestResult requestResult = DownloadClient.RequestResult requestResult =

View File

@ -2,7 +2,6 @@
using System.Net; using System.Net;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using HtmlAgilityPack; using HtmlAgilityPack;
using Logging;
using Tranga.TrangaTasks; using Tranga.TrangaTasks;
namespace Tranga.Connectors; namespace Tranga.Connectors;
@ -11,18 +10,18 @@ public class Manganato : Connector
{ {
public override string name { get; } public override string name { get; }
public Manganato(TrangaSettings settings, Logger? logger = null) : base(settings, logger) public Manganato(TrangaSettings settings) : base(settings)
{ {
this.name = "Manganato"; this.name = "Manganato";
this.downloadClient = new DownloadClient(new Dictionary<byte, int>() this.downloadClient = new DownloadClient(new Dictionary<byte, int>()
{ {
{(byte)1, 60} {(byte)1, 60}
}, logger); }, settings.logger);
} }
protected override Publication[] GetPublicationsInternal(string publicationTitle = "") protected override Publication[] GetPublicationsInternal(string publicationTitle = "")
{ {
logger?.WriteLine(this.GetType().ToString(), $"Getting Publications (title={publicationTitle})"); settings.logger?.WriteLine(this.GetType().ToString(), $"Getting Publications (title={publicationTitle})");
string sanitizedTitle = string.Join('_', Regex.Matches(publicationTitle, "[A-z]*")).ToLower(); string sanitizedTitle = string.Join('_', Regex.Matches(publicationTitle, "[A-z]*")).ToLower();
string requestUrl = $"https://manganato.com/search/story/{sanitizedTitle}"; string requestUrl = $"https://manganato.com/search/story/{sanitizedTitle}";
DownloadClient.RequestResult requestResult = DownloadClient.RequestResult requestResult =
@ -127,7 +126,7 @@ public class Manganato : Connector
public override Chapter[] GetChapters(Publication publication, string language = "") public override Chapter[] GetChapters(Publication publication, string language = "")
{ {
logger?.WriteLine(this.GetType().ToString(), $"Getting Chapters for {publication.sortName} {publication.internalId} (language={language})"); settings.logger?.WriteLine(this.GetType().ToString(), $"Getting Chapters for {publication.sortName} {publication.internalId} (language={language})");
string requestUrl = $"https://chapmanganato.com/{publication.publicationId}"; string requestUrl = $"https://chapmanganato.com/{publication.publicationId}";
DownloadClient.RequestResult requestResult = DownloadClient.RequestResult requestResult =
downloadClient.MakeRequest(requestUrl, (byte)1); downloadClient.MakeRequest(requestUrl, (byte)1);
@ -140,7 +139,7 @@ public class Manganato : Connector
NumberDecimalSeparator = "." NumberDecimalSeparator = "."
}; };
List<Chapter> chapters = ParseChaptersFromHtml(publication, requestResult.result); List<Chapter> chapters = ParseChaptersFromHtml(publication, requestResult.result);
logger?.WriteLine(this.GetType().ToString(), $"Done getting Chapters for {publication.internalId}"); settings.logger?.WriteLine(this.GetType().ToString(), $"Done getting Chapters for {publication.internalId}");
return chapters.OrderBy(chapter => Convert.ToSingle(chapter.chapterNumber, chapterNumberFormatInfo)).ToArray(); return chapters.OrderBy(chapter => Convert.ToSingle(chapter.chapterNumber, chapterNumberFormatInfo)).ToArray();
} }
@ -173,7 +172,7 @@ public class Manganato : Connector
{ {
if (cancellationToken?.IsCancellationRequested ?? false) if (cancellationToken?.IsCancellationRequested ?? false)
return HttpStatusCode.RequestTimeout; return HttpStatusCode.RequestTimeout;
logger?.WriteLine(this.GetType().ToString(), $"Downloading Chapter-Info {publication.sortName} {publication.internalId} {chapter.volumeNumber}-{chapter.chapterNumber}"); settings.logger?.WriteLine(this.GetType().ToString(), $"Downloading Chapter-Info {publication.sortName} {publication.internalId} {chapter.volumeNumber}-{chapter.chapterNumber}");
string requestUrl = chapter.url; string requestUrl = chapter.url;
DownloadClient.RequestResult requestResult = DownloadClient.RequestResult requestResult =
downloadClient.MakeRequest(requestUrl, (byte)1); downloadClient.MakeRequest(requestUrl, (byte)1);

View File

@ -3,7 +3,6 @@ using System.Net;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Xml.Linq; using System.Xml.Linq;
using HtmlAgilityPack; using HtmlAgilityPack;
using Logging;
using Newtonsoft.Json; using Newtonsoft.Json;
using PuppeteerSharp; using PuppeteerSharp;
using Tranga.TrangaTasks; using Tranga.TrangaTasks;
@ -16,13 +15,13 @@ public class Mangasee : Connector
private IBrowser? _browser = null; private IBrowser? _browser = null;
private const string ChromiumVersion = "1154303"; private const string ChromiumVersion = "1154303";
public Mangasee(TrangaSettings settings, Logger? logger = null) : base(settings, logger) public Mangasee(TrangaSettings settings) : base(settings)
{ {
this.name = "Mangasee"; this.name = "Mangasee";
this.downloadClient = new DownloadClient(new Dictionary<byte, int>() this.downloadClient = new DownloadClient(new Dictionary<byte, int>()
{ {
{ (byte)1, 60 } { (byte)1, 60 }
}, logger); }, settings.logger);
Task d = new Task(DownloadBrowser); Task d = new Task(DownloadBrowser);
d.Start(); d.Start();
@ -35,31 +34,31 @@ public class Mangasee : Connector
browserFetcher.Remove(rev); browserFetcher.Remove(rev);
if (!browserFetcher.LocalRevisions().Contains(ChromiumVersion)) if (!browserFetcher.LocalRevisions().Contains(ChromiumVersion))
{ {
logger?.WriteLine(this.GetType().ToString(), "Downloading headless browser"); settings.logger?.WriteLine(this.GetType().ToString(), "Downloading headless browser");
DateTime last = DateTime.Now.Subtract(TimeSpan.FromSeconds(5)); DateTime last = DateTime.Now.Subtract(TimeSpan.FromSeconds(5));
browserFetcher.DownloadProgressChanged += (sender, args) => browserFetcher.DownloadProgressChanged += (sender, args) =>
{ {
double currentBytes = Convert.ToDouble(args.BytesReceived) / Convert.ToDouble(args.TotalBytesToReceive); double currentBytes = Convert.ToDouble(args.BytesReceived) / Convert.ToDouble(args.TotalBytesToReceive);
if (args.TotalBytesToReceive == args.BytesReceived) if (args.TotalBytesToReceive == args.BytesReceived)
{ {
logger?.WriteLine(this.GetType().ToString(), "Browser downloaded."); settings.logger?.WriteLine(this.GetType().ToString(), "Browser downloaded.");
} }
else if (DateTime.Now > last.AddSeconds(5)) else if (DateTime.Now > last.AddSeconds(5))
{ {
logger?.WriteLine(this.GetType().ToString(), $"Browser download progress: {currentBytes:P2}"); settings.logger?.WriteLine(this.GetType().ToString(), $"Browser download progress: {currentBytes:P2}");
last = DateTime.Now; last = DateTime.Now;
} }
}; };
if (!browserFetcher.CanDownloadAsync(ChromiumVersion).Result) if (!browserFetcher.CanDownloadAsync(ChromiumVersion).Result)
{ {
logger?.WriteLine(this.GetType().ToString(), $"Can't download browser version {ChromiumVersion}"); settings.logger?.WriteLine(this.GetType().ToString(), $"Can't download browser version {ChromiumVersion}");
return; return;
} }
await browserFetcher.DownloadAsync(ChromiumVersion); await browserFetcher.DownloadAsync(ChromiumVersion);
} }
logger?.WriteLine(this.GetType().ToString(), "Starting browser."); settings.logger?.WriteLine(this.GetType().ToString(), "Starting browser.");
this._browser = await Puppeteer.LaunchAsync(new LaunchOptions this._browser = await Puppeteer.LaunchAsync(new LaunchOptions
{ {
Headless = true, Headless = true,
@ -74,7 +73,7 @@ public class Mangasee : Connector
protected override Publication[] GetPublicationsInternal(string publicationTitle = "") protected override Publication[] GetPublicationsInternal(string publicationTitle = "")
{ {
logger?.WriteLine(this.GetType().ToString(), $"Getting Publications (title={publicationTitle})"); settings.logger?.WriteLine(this.GetType().ToString(), $"Getting Publications (title={publicationTitle})");
string requestUrl = $"https://mangasee123.com/_search.php"; string requestUrl = $"https://mangasee123.com/_search.php";
DownloadClient.RequestResult requestResult = DownloadClient.RequestResult requestResult =
downloadClient.MakeRequest(requestUrl, (byte)1); downloadClient.MakeRequest(requestUrl, (byte)1);
@ -99,7 +98,7 @@ public class Mangasee : Connector
queryFiltered = queryFiltered.Where(item => item.Value >= publicationTitle.Split(' ').Length - 1) queryFiltered = queryFiltered.Where(item => item.Value >= publicationTitle.Split(' ').Length - 1)
.ToDictionary(item => item.Key, item => item.Value); .ToDictionary(item => item.Key, item => item.Value);
logger?.WriteLine(this.GetType().ToString(), $"Got {queryFiltered.Count} Publications (title={publicationTitle})"); settings.logger?.WriteLine(this.GetType().ToString(), $"Got {queryFiltered.Count} Publications (title={publicationTitle})");
HashSet<Publication> ret = new(); HashSet<Publication> ret = new();
List<SearchResultItem> orderedFiltered = List<SearchResultItem> orderedFiltered =
@ -112,7 +111,7 @@ public class Mangasee : Connector
downloadClient.MakeRequest($"https://mangasee123.com/manga/{orderedItem.i}", (byte)1); downloadClient.MakeRequest($"https://mangasee123.com/manga/{orderedItem.i}", (byte)1);
if ((int)requestResult.statusCode >= 200 || (int)requestResult.statusCode < 300) if ((int)requestResult.statusCode >= 200 || (int)requestResult.statusCode < 300)
{ {
logger?.WriteLine(this.GetType().ToString(), $"Retrieving Publication info: {orderedItem.s} {index++}/{orderedFiltered.Count}"); settings.logger?.WriteLine(this.GetType().ToString(), $"Retrieving Publication info: {orderedItem.s} {index++}/{orderedFiltered.Count}");
ret.Add(ParseSinglePublicationFromHtml(requestResult.result, orderedItem.s, orderedItem.i, orderedItem.a)); ret.Add(ParseSinglePublicationFromHtml(requestResult.result, orderedItem.s, orderedItem.i, orderedItem.a));
} }
} }
@ -230,7 +229,7 @@ public class Mangasee : Connector
{ {
NumberDecimalSeparator = "." NumberDecimalSeparator = "."
}; };
logger?.WriteLine(this.GetType().ToString(), $"Done getting Chapters for {publication.internalId}"); settings.logger?.WriteLine(this.GetType().ToString(), $"Done getting Chapters for {publication.internalId}");
return ret.OrderBy(chapter => Convert.ToSingle(chapter.chapterNumber, chapterNumberFormatInfo)).ToArray(); return ret.OrderBy(chapter => Convert.ToSingle(chapter.chapterNumber, chapterNumberFormatInfo)).ToArray();
} }
@ -240,13 +239,13 @@ public class Mangasee : Connector
return HttpStatusCode.RequestTimeout; return HttpStatusCode.RequestTimeout;
while (this._browser is null && !(cancellationToken?.IsCancellationRequested??false)) while (this._browser is null && !(cancellationToken?.IsCancellationRequested??false))
{ {
logger?.WriteLine(this.GetType().ToString(), "Waiting for headless browser to download..."); settings.logger?.WriteLine(this.GetType().ToString(), "Waiting for headless browser to download...");
Thread.Sleep(1000); Thread.Sleep(1000);
} }
if (cancellationToken?.IsCancellationRequested??false) if (cancellationToken?.IsCancellationRequested??false)
return HttpStatusCode.RequestTimeout; return HttpStatusCode.RequestTimeout;
logger?.WriteLine(this.GetType().ToString(), $"Downloading Chapter-Info {publication.sortName} {publication.internalId} {chapter.volumeNumber}-{chapter.chapterNumber}"); settings.logger?.WriteLine(this.GetType().ToString(), $"Downloading Chapter-Info {publication.sortName} {publication.internalId} {chapter.volumeNumber}-{chapter.chapterNumber}");
IPage page = _browser!.NewPageAsync().Result; IPage page = _browser!.NewPageAsync().Result;
IResponse response = page.GoToAsync(chapter.url).Result; IResponse response = page.GoToAsync(chapter.url).Result;
if (response.Ok) if (response.Ok)

View File

@ -17,12 +17,10 @@ public class TaskManager
private bool _continueRunning = true; private bool _continueRunning = true;
private readonly Connector[] _connectors; private readonly Connector[] _connectors;
public TrangaSettings settings { get; } public TrangaSettings settings { get; }
private Logger? logger { get; }
public TaskManager(TrangaSettings settings, Logger? logger = null) public TaskManager(TrangaSettings settings)
{ {
this.logger = logger; settings.logger?.WriteLine("Tranga", value: "\n"+
logger?.WriteLine("Tranga", value: "\n"+
@"-----------------------------------------------------------------"+"\n"+ @"-----------------------------------------------------------------"+"\n"+
@" |¯¯¯¯¯¯|°|¯¯¯¯¯¯\ /¯¯¯¯¯¯| |¯¯¯\|¯¯¯| /¯¯¯¯¯¯\' /¯¯¯¯¯¯| "+"\n"+ @" |¯¯¯¯¯¯|°|¯¯¯¯¯¯\ /¯¯¯¯¯¯| |¯¯¯\|¯¯¯| /¯¯¯¯¯¯\' /¯¯¯¯¯¯| "+"\n"+
@" | | | x <|' / ! | | '| | (/¯¯¯\° / ! | "+ "\n"+ @" | | | x <|' / ! | | '| | (/¯¯¯\° / ! | "+ "\n"+
@ -30,10 +28,10 @@ public class TaskManager
@"-----------------------------------------------------------------"); @"-----------------------------------------------------------------");
this._connectors = new Connector[] this._connectors = new Connector[]
{ {
new MangaDex(settings, logger), new MangaDex(settings),
new Manganato(settings, logger), new Manganato(settings),
new Mangasee(settings, logger), new Mangasee(settings),
new MangaKatana(settings, logger) new MangaKatana(settings)
}; };
this.settings = settings; this.settings = settings;
@ -50,7 +48,7 @@ public class TaskManager
/// </summary> /// </summary>
private void TaskCheckerThread() private void TaskCheckerThread()
{ {
logger?.WriteLine(this.GetType().ToString(), "Starting TaskCheckerThread."); settings.logger?.WriteLine(this.GetType().ToString(), "Starting TaskCheckerThread.");
int waitingTasksCount = _allTasks.Count(task => task.state is TrangaTask.ExecutionState.Waiting); int waitingTasksCount = _allTasks.Count(task => task.state is TrangaTask.ExecutionState.Waiting);
while (_continueRunning) while (_continueRunning)
{ {
@ -137,7 +135,7 @@ public class TaskManager
CancellationTokenSource cToken = new (); CancellationTokenSource cToken = new ();
Task t = new(() => Task t = new(() =>
{ {
task.Execute(this, this.logger, cToken.Token); task.Execute(this, cToken.Token);
}, cToken.Token); }, cToken.Token);
_runningTasks.Add(task, cToken); _runningTasks.Add(task, cToken);
t.Start(); t.Start();
@ -149,7 +147,7 @@ public class TaskManager
{ {
case TrangaTask.Task.UpdateLibraries: case TrangaTask.Task.UpdateLibraries:
//Only one UpdateKomgaLibrary Task //Only one UpdateKomgaLibrary Task
logger?.WriteLine(this.GetType().ToString(), $"Replacing old {newTask.task}-Task."); settings.logger?.WriteLine(this.GetType().ToString(), $"Replacing old {newTask.task}-Task.");
if (GetTasksMatching(newTask).FirstOrDefault() is { } exists) if (GetTasksMatching(newTask).FirstOrDefault() is { } exists)
_allTasks.Remove(exists); _allTasks.Remove(exists);
_allTasks.Add(newTask); _allTasks.Add(newTask);
@ -158,19 +156,19 @@ public class TaskManager
default: default:
if (!GetTasksMatching(newTask).Any()) if (!GetTasksMatching(newTask).Any())
{ {
logger?.WriteLine(this.GetType().ToString(), $"Adding new Task {newTask}"); settings.logger?.WriteLine(this.GetType().ToString(), $"Adding new Task {newTask}");
_allTasks.Add(newTask); _allTasks.Add(newTask);
ExportDataAndSettings(); ExportDataAndSettings();
} }
else else
logger?.WriteLine(this.GetType().ToString(), $"Task already exists {newTask}"); settings.logger?.WriteLine(this.GetType().ToString(), $"Task already exists {newTask}");
break; break;
} }
} }
public void DeleteTask(TrangaTask removeTask) public void DeleteTask(TrangaTask removeTask)
{ {
logger?.WriteLine(this.GetType().ToString(), $"Removing Task {removeTask}"); settings.logger?.WriteLine(this.GetType().ToString(), $"Removing Task {removeTask}");
if(_allTasks.Contains(removeTask)) if(_allTasks.Contains(removeTask))
_allTasks.Remove(removeTask); _allTasks.Remove(removeTask);
removeTask.parentTask?.RemoveChildTask(removeTask); removeTask.parentTask?.RemoveChildTask(removeTask);
@ -321,7 +319,7 @@ public class TaskManager
/// <param name="force">If force is true, tasks are aborted.</param> /// <param name="force">If force is true, tasks are aborted.</param>
public void Shutdown(bool force = false) public void Shutdown(bool force = false)
{ {
logger?.WriteLine(this.GetType().ToString(), $"Shutting down (forced={force})"); settings.logger?.WriteLine(this.GetType().ToString(), $"Shutting down (forced={force})");
_continueRunning = false; _continueRunning = false;
ExportDataAndSettings(); ExportDataAndSettings();
@ -331,17 +329,17 @@ public class TaskManager
//Wait for tasks to finish //Wait for tasks to finish
while(_allTasks.Any(task => task.state is TrangaTask.ExecutionState.Running or TrangaTask.ExecutionState.Enqueued)) while(_allTasks.Any(task => task.state is TrangaTask.ExecutionState.Running or TrangaTask.ExecutionState.Enqueued))
Thread.Sleep(10); Thread.Sleep(10);
logger?.WriteLine(this.GetType().ToString(), "Tasks finished. Bye!"); settings.logger?.WriteLine(this.GetType().ToString(), "Tasks finished. Bye!");
Environment.Exit(0); Environment.Exit(0);
} }
private void ImportData() private void ImportData()
{ {
logger?.WriteLine(this.GetType().ToString(), "Importing Data"); settings.logger?.WriteLine(this.GetType().ToString(), "Importing Data");
string buffer; string buffer;
if (File.Exists(settings.tasksFilePath)) if (File.Exists(settings.tasksFilePath))
{ {
logger?.WriteLine(this.GetType().ToString(), $"Importing tasks from {settings.tasksFilePath}"); settings.logger?.WriteLine(this.GetType().ToString(), $"Importing tasks from {settings.tasksFilePath}");
buffer = File.ReadAllText(settings.tasksFilePath); buffer = File.ReadAllText(settings.tasksFilePath);
this._allTasks = JsonConvert.DeserializeObject<HashSet<TrangaTask>>(buffer, new JsonSerializerSettings() { Converters = { new TrangaTask.TrangaTaskJsonConverter() } })!; this._allTasks = JsonConvert.DeserializeObject<HashSet<TrangaTask>>(buffer, new JsonSerializerSettings() { Converters = { new TrangaTask.TrangaTaskJsonConverter() } })!;
} }
@ -362,10 +360,10 @@ public class TaskManager
/// </summary> /// </summary>
private void ExportDataAndSettings() private void ExportDataAndSettings()
{ {
logger?.WriteLine(this.GetType().ToString(), $"Exporting settings to {settings.settingsFilePath}"); settings.logger?.WriteLine(this.GetType().ToString(), $"Exporting settings to {settings.settingsFilePath}");
settings.ExportSettings(); settings.ExportSettings();
logger?.WriteLine(this.GetType().ToString(), $"Exporting tasks to {settings.tasksFilePath}"); settings.logger?.WriteLine(this.GetType().ToString(), $"Exporting tasks to {settings.tasksFilePath}");
while(IsFileInUse(settings.tasksFilePath)) while(IsFileInUse(settings.tasksFilePath))
Thread.Sleep(50); Thread.Sleep(50);
File.WriteAllText(settings.tasksFilePath, JsonConvert.SerializeObject(this._allTasks)); File.WriteAllText(settings.tasksFilePath, JsonConvert.SerializeObject(this._allTasks));

View File

@ -16,9 +16,10 @@ public class TrangaSettings
public HashSet<LibraryManager> libraryManagers { get; } public HashSet<LibraryManager> libraryManagers { get; }
public HashSet<NotificationManager> notificationManagers { get; } public HashSet<NotificationManager> notificationManagers { get; }
public ushort? version { get; set; } public ushort? version { get; set; }
[JsonIgnore] public Logger? logger { get; init; }
public TrangaSettings(string downloadLocation, string workingDirectory, HashSet<LibraryManager>? libraryManagers, public TrangaSettings(string downloadLocation, string workingDirectory, HashSet<LibraryManager>? libraryManagers,
HashSet<NotificationManager>? notificationManagers) HashSet<NotificationManager>? notificationManagers, Logger? logger)
{ {
if (downloadLocation.Length < 1 || workingDirectory.Length < 1) if (downloadLocation.Length < 1 || workingDirectory.Length < 1)
throw new ArgumentException("Download-location and working-directory paths can not be empty!"); throw new ArgumentException("Download-location and working-directory paths can not be empty!");
@ -26,13 +27,14 @@ public class TrangaSettings
this.downloadLocation = downloadLocation; this.downloadLocation = downloadLocation;
this.libraryManagers = libraryManagers??new(); this.libraryManagers = libraryManagers??new();
this.notificationManagers = notificationManagers??new(); this.notificationManagers = notificationManagers??new();
this.logger = logger;
} }
public static TrangaSettings LoadSettings(string importFilePath, Logger? logger) public static TrangaSettings LoadSettings(string importFilePath, Logger? logger)
{ {
if (!File.Exists(importFilePath)) if (!File.Exists(importFilePath))
return new TrangaSettings(Path.Join(Directory.GetCurrentDirectory(), "Downloads"), return new TrangaSettings(Path.Join(Directory.GetCurrentDirectory(), "Downloads"),
Directory.GetCurrentDirectory(), new HashSet<LibraryManager>(), new HashSet<NotificationManager>()); Directory.GetCurrentDirectory(), new HashSet<LibraryManager>(), new HashSet<NotificationManager>(), logger);
string toRead = File.ReadAllText(importFilePath); string toRead = File.ReadAllText(importFilePath);
TrangaSettings settings = JsonConvert.DeserializeObject<TrangaSettings>(toRead, TrangaSettings settings = JsonConvert.DeserializeObject<TrangaSettings>(toRead,
@ -71,7 +73,7 @@ public class TrangaSettings
File.WriteAllText(settingsFilePath, JsonConvert.SerializeObject(this)); File.WriteAllText(settingsFilePath, JsonConvert.SerializeObject(this));
} }
public void UpdateSettings(UpdateField field, Logger? logger = null, params string[] values) public void UpdateSettings(UpdateField field, params string[] values)
{ {
switch (field) switch (field)
{ {
@ -84,19 +86,19 @@ public class TrangaSettings
if (values.Length != 2) if (values.Length != 2)
return; return;
libraryManagers.RemoveWhere(lm => lm.GetType() == typeof(Komga)); libraryManagers.RemoveWhere(lm => lm.GetType() == typeof(Komga));
libraryManagers.Add(new Komga(values[0], values[1], logger)); libraryManagers.Add(new Komga(values[0], values[1], this.logger));
break; break;
case UpdateField.Kavita: case UpdateField.Kavita:
if (values.Length != 3) if (values.Length != 3)
return; return;
libraryManagers.RemoveWhere(lm => lm.GetType() == typeof(Kavita)); libraryManagers.RemoveWhere(lm => lm.GetType() == typeof(Kavita));
libraryManagers.Add(new Kavita(values[0], values[1], values[2], logger)); libraryManagers.Add(new Kavita(values[0], values[1], values[2], this.logger));
break; break;
case UpdateField.Gotify: case UpdateField.Gotify:
if (values.Length != 2) if (values.Length != 2)
return; return;
notificationManagers.RemoveWhere(nm => nm.GetType() == typeof(Gotify)); notificationManagers.RemoveWhere(nm => nm.GetType() == typeof(Gotify));
Gotify newGotify = new(values[0], values[1], logger); Gotify newGotify = new(values[0], values[1], this.logger);
notificationManagers.Add(newGotify); notificationManagers.Add(newGotify);
newGotify.SendNotification("Success!", "Gotify was added to Tranga!"); newGotify.SendNotification("Success!", "Gotify was added to Tranga!");
break; break;
@ -104,7 +106,7 @@ public class TrangaSettings
if(values.Length != 1) if(values.Length != 1)
return; return;
notificationManagers.RemoveWhere(nm => nm.GetType() == typeof(LunaSea)); notificationManagers.RemoveWhere(nm => nm.GetType() == typeof(LunaSea));
LunaSea newLunaSea = new(values[0], logger); LunaSea newLunaSea = new(values[0], this.logger);
notificationManagers.Add(newLunaSea); notificationManagers.Add(newLunaSea);
newLunaSea.SendNotification("Success!", "LunaSea was added to Tranga!"); newLunaSea.SendNotification("Success!", "LunaSea was added to Tranga!");
break; break;

View File

@ -1,5 +1,4 @@
using System.Net; using System.Net;
using Logging;
using Tranga.Connectors; using Tranga.Connectors;
using Tranga.NotificationManagers; using Tranga.NotificationManagers;
using Tranga.LibraryManagers; using Tranga.LibraryManagers;
@ -23,7 +22,7 @@ public class DownloadChapterTask : TrangaTask
this.language = language; this.language = language;
} }
protected override HttpStatusCode ExecuteTask(TaskManager taskManager, Logger? logger, CancellationToken? cancellationToken = null) protected override HttpStatusCode ExecuteTask(TaskManager taskManager, CancellationToken? cancellationToken = null)
{ {
if (cancellationToken?.IsCancellationRequested ?? false) if (cancellationToken?.IsCancellationRequested ?? false)
return HttpStatusCode.RequestTimeout; return HttpStatusCode.RequestTimeout;

View File

@ -1,5 +1,4 @@
using System.Net; using System.Net;
using Logging;
using Tranga.Connectors; using Tranga.Connectors;
namespace Tranga.TrangaTasks; namespace Tranga.TrangaTasks;
@ -16,7 +15,7 @@ public class MonitorPublicationTask : TrangaTask
this.language = language; this.language = language;
} }
protected override HttpStatusCode ExecuteTask(TaskManager taskManager, Logger? logger, CancellationToken? cancellationToken = null) protected override HttpStatusCode ExecuteTask(TaskManager taskManager, CancellationToken? cancellationToken = null)
{ {
if (cancellationToken?.IsCancellationRequested ?? false) if (cancellationToken?.IsCancellationRequested ?? false)
return HttpStatusCode.RequestTimeout; return HttpStatusCode.RequestTimeout;

View File

@ -51,9 +51,8 @@ public abstract class TrangaTask
/// BL for concrete Tasks /// BL for concrete Tasks
/// </summary> /// </summary>
/// <param name="taskManager"></param> /// <param name="taskManager"></param>
/// <param name="logger"></param>
/// <param name="cancellationToken"></param> /// <param name="cancellationToken"></param>
protected abstract HttpStatusCode ExecuteTask(TaskManager taskManager, Logger? logger, CancellationToken? cancellationToken = null); protected abstract HttpStatusCode ExecuteTask(TaskManager taskManager, CancellationToken? cancellationToken = null);
public abstract TrangaTask Clone(); public abstract TrangaTask Clone();
@ -63,18 +62,17 @@ public abstract class TrangaTask
/// Execute the task /// Execute the task
/// </summary> /// </summary>
/// <param name="taskManager">Should be the parent taskManager</param> /// <param name="taskManager">Should be the parent taskManager</param>
/// <param name="logger"></param>
/// <param name="cancellationToken"></param> /// <param name="cancellationToken"></param>
public void Execute(TaskManager taskManager, Logger? logger, CancellationToken? cancellationToken = null) public void Execute(TaskManager taskManager, CancellationToken? cancellationToken = null)
{ {
logger?.WriteLine(this.GetType().ToString(), $"Executing Task {this}"); taskManager.settings.logger?.WriteLine(this.GetType().ToString(), $"Executing Task {this}");
this.state = ExecutionState.Running; this.state = ExecutionState.Running;
this.executionStarted = DateTime.Now; this.executionStarted = DateTime.Now;
this.lastChange = DateTime.Now; this.lastChange = DateTime.Now;
if(parentTask is not null && parentTask.childTasks.All(ct => ct.state is ExecutionState.Waiting or ExecutionState.Failed)) if(parentTask is not null && parentTask.childTasks.All(ct => ct.state is ExecutionState.Waiting or ExecutionState.Failed))
parentTask.executionStarted = DateTime.Now; parentTask.executionStarted = DateTime.Now;
HttpStatusCode statusCode = ExecuteTask(taskManager, logger, cancellationToken); HttpStatusCode statusCode = ExecuteTask(taskManager, cancellationToken);
if ((int)statusCode >= 200 && (int)statusCode < 300) if ((int)statusCode >= 200 && (int)statusCode < 300)
{ {
@ -90,7 +88,7 @@ public abstract class TrangaTask
if (this is DownloadChapterTask) if (this is DownloadChapterTask)
taskManager.DeleteTask(this); taskManager.DeleteTask(this);
logger?.WriteLine(this.GetType().ToString(), $"Finished Executing Task {this}"); taskManager.settings.logger?.WriteLine(this.GetType().ToString(), $"Finished Executing Task {this}");
} }
public void AddChildTask(TrangaTask childTask) public void AddChildTask(TrangaTask childTask)