From 8261d02cc7db9fd0c5655a43a4c8676dfa81fdb0 Mon Sep 17 00:00:00 2001 From: glax Date: Wed, 31 May 2023 20:29:30 +0200 Subject: [PATCH] Renamed methods, made some methods non-static in Connector, some more logging --- Tranga/Connector.cs | 49 +++++++++++++++++------------------ Tranga/Connectors/MangaDex.cs | 28 +++----------------- Tranga/Publication.cs | 10 +++++++ Tranga/TaskExecutor.cs | 6 ++--- 4 files changed, 40 insertions(+), 53 deletions(-) diff --git a/Tranga/Connector.cs b/Tranga/Connector.cs index c2339ea..150ba11 100644 --- a/Tranga/Connector.cs +++ b/Tranga/Connector.cs @@ -14,9 +14,9 @@ public abstract class Connector internal string downloadLocation { get; } //Location of local files protected DownloadClient downloadClient { get; init; } - protected Logger? logger; + protected readonly Logger? logger; - protected string imageCachePath; + protected readonly string imageCachePath; protected Connector(string downloadLocation, string imageCachePath, Logger? logger) { @@ -61,23 +61,25 @@ public abstract class Connector /// /// Publication to retrieve Cover for /// TrangaSettings - public abstract void CloneCoverFromCache(Publication publication, TrangaSettings settings); - - /// - /// Saves the series-info to series.json in the Publication Folder - /// - /// Publication to save series.json for - public void SaveSeriesInfo(Publication publication) + public void CloneCoverFromCache(Publication publication, TrangaSettings settings) { - logger?.WriteLine(this.GetType().ToString(), $"Saving series.json for {publication.sortName}"); - //Check if Publication already has a Folder and a series.json + logger?.WriteLine(this.GetType().ToString(), $"Cloning cover {publication.sortName}"); + //Check if Publication already has a Folder and cover string publicationFolder = Path.Join(downloadLocation, publication.folderName); + if(!Directory.Exists(publicationFolder)) Directory.CreateDirectory(publicationFolder); - - string seriesInfoPath = Path.Join(publicationFolder, "series.json"); - if(!File.Exists(seriesInfoPath)) - File.WriteAllText(seriesInfoPath,publication.GetSeriesInfoJson()); + DirectoryInfo dirInfo = new (publicationFolder); + if (dirInfo.EnumerateFiles().Any(info => info.Name.Contains("cover."))) + { + logger?.WriteLine(this.GetType().ToString(), $"Cover exists {publication.sortName}"); + return; + } + + string fileInCache = Path.Join(settings.coverImageCache, publication.coverFileNameInCache); + string newFilePath = Path.Join(publicationFolder, $"cover.{Path.GetFileName(fileInCache).Split('.')[^1]}" ); + logger?.WriteLine(this.GetType().ToString(), $"Cloning cover {fileInCache} -> {newFilePath}"); + File.Copy(fileInCache, newFilePath, true); } /// @@ -85,7 +87,7 @@ public abstract class Connector /// See ComicInfo.xml /// /// XML-string - protected static string CreateComicInfo(Publication publication, Chapter chapter, Logger? logger) + protected static string GetComicInfoXmlString(Publication publication, Chapter chapter, Logger? logger) { logger?.WriteLine("Connector", $"Creating ComicInfo.Xml for {publication.sortName} {publication.internalId} {chapter.volumeNumber}-{chapter.chapterNumber}"); XElement comicInfo = new XElement("ComicInfo", @@ -103,16 +105,16 @@ public abstract class Connector /// Checks if a chapter-archive is already present /// /// true if chapter is present - public bool ChapterIsDownloaded(Publication publication, Chapter chapter) + public bool CheckChapterIsDownloaded(Publication publication, Chapter chapter) { - return File.Exists(CreateFullFilepath(publication, chapter)); + return File.Exists(GetArchiveFilePath(publication, chapter)); } /// /// Creates full file path of chapter-archive /// /// Filepath - protected string CreateFullFilepath(Publication publication, Chapter chapter) + protected string GetArchiveFilePath(Publication publication, Chapter chapter) { return Path.Join(downloadLocation, publication.folderName, $"{chapter.fileName}.cbz"); } @@ -122,9 +124,8 @@ public abstract class Connector /// /// /// - /// DownloadClient of the connector /// Requesttype for ratelimit - protected static void DownloadImage(string imageUrl, string fullPath, DownloadClient downloadClient, byte requestType) + private void DownloadImage(string imageUrl, string fullPath, byte requestType) { DownloadClient.RequestResult requestResult = downloadClient.MakeRequest(imageUrl, requestType); byte[] buffer = new byte[requestResult.result.Length]; @@ -137,11 +138,9 @@ public abstract class Connector /// /// List of URLs to download Images from /// Full path to save archive to (without file ending .cbz) - /// DownloadClient of the connector - /// /// Path of the generate Chapter ComicInfo.xml, if it was generated /// RequestType for RateLimits - protected static void DownloadChapterImages(string[] imageUrls, string saveArchiveFilePath, DownloadClient downloadClient, byte requestType, Logger? logger, string? comicInfoPath = null) + protected void DownloadChapterImages(string[] imageUrls, string saveArchiveFilePath, byte requestType, string? comicInfoPath = null) { logger?.WriteLine("Connector", $"Downloading Images for {saveArchiveFilePath}"); //Check if Publication Directory already exists @@ -162,7 +161,7 @@ public abstract class Connector string[] split = imageUrl.Split('.'); string extension = split[^1]; logger?.WriteLine("Connector", $"Downloading Image {chapter + 1}/{imageUrls.Length}"); - DownloadImage(imageUrl, Path.Join(tempFolder, $"{chapter++}.{extension}"), downloadClient, requestType); + DownloadImage(imageUrl, Path.Join(tempFolder, $"{chapter++}.{extension}"), requestType); } if(comicInfoPath is not null) diff --git a/Tranga/Connectors/MangaDex.cs b/Tranga/Connectors/MangaDex.cs index 37beaac..b3bad9b 100644 --- a/Tranga/Connectors/MangaDex.cs +++ b/Tranga/Connectors/MangaDex.cs @@ -38,6 +38,7 @@ public class MangaDex : Connector int offset = 0; //"Page" int total = int.MaxValue; //How many total results are there, is updated on first request HashSet publications = new(); + int loadedPublicationData = 0; while (offset < total) //As long as we haven't requested all "Pages" { //Request next Page @@ -55,10 +56,10 @@ public class MangaDex : Connector total = result["total"]!.GetValue(); //Update the total number of Publications JsonArray mangaInResult = result["data"]!.AsArray(); //Manga-data-Array - logger?.WriteLine(this.GetType().ToString(), $"Getting publication data."); //Loop each Manga and extract information from JSON foreach (JsonNode? mangeNode in mangaInResult) { + logger?.WriteLine(this.GetType().ToString(), $"Getting publication data. {++loadedPublicationData}/{total}"); JsonObject manga = (JsonObject)mangeNode!; JsonObject attributes = manga["attributes"]!.AsObject(); @@ -224,10 +225,10 @@ public class MangaDex : Connector imageUrls.Add($"{baseUrl}/data/{hash}/{image!.GetValue()}"); string comicInfoPath = Path.GetTempFileName(); - File.WriteAllText(comicInfoPath, CreateComicInfo(publication, chapter, logger)); + File.WriteAllText(comicInfoPath, GetComicInfoXmlString(publication, chapter, logger)); //Download Chapter-Images - DownloadChapterImages(imageUrls.ToArray(), CreateFullFilepath(publication, chapter), downloadClient, (byte)RequestType.AtHomeServer, logger, comicInfoPath); + DownloadChapterImages(imageUrls.ToArray(), GetArchiveFilePath(publication, chapter), (byte)RequestType.AtHomeServer, comicInfoPath); } private string? GetCoverUrl(string publicationId, string? posterId) @@ -273,27 +274,6 @@ public class MangaDex : Connector return author; } - public override void CloneCoverFromCache(Publication publication, TrangaSettings settings) - { - logger?.WriteLine(this.GetType().ToString(), $"Cloning cover {publication.sortName}"); - //Check if Publication already has a Folder and cover - string publicationFolder = Path.Join(downloadLocation, publication.folderName); - - if(!Directory.Exists(publicationFolder)) - Directory.CreateDirectory(publicationFolder); - DirectoryInfo dirInfo = new (publicationFolder); - if (dirInfo.EnumerateFiles().Any(info => info.Name.Contains("cover."))) - { - logger?.WriteLine(this.GetType().ToString(), $"Cover exists {publication.sortName}"); - return; - } - - string fileInCache = Path.Join(settings.coverImageCache, publication.coverFileNameInCache); - string newFilePath = Path.Join(publicationFolder, $"cover.{Path.GetFileName(fileInCache).Split('.')[^1]}" ); - logger?.WriteLine(this.GetType().ToString(), $"Cloning cover {fileInCache} -> {newFilePath}"); - File.Copy(fileInCache, newFilePath, true); - } - private string SaveImage(string url) { string[] split = url.Split('/'); diff --git a/Tranga/Publication.cs b/Tranga/Publication.cs index bc8004a..07e7715 100644 --- a/Tranga/Publication.cs +++ b/Tranga/Publication.cs @@ -47,6 +47,16 @@ public readonly struct Publication string onlyLowerLetters = string.Concat(this.sortName.ToLower().Where(Char.IsLetter)); this.internalId = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{onlyLowerLetters}{this.year}")); } + + public void SaveSeriesInfoJson(string downloadDirectory) + { + string publicationFolder = Path.Join(downloadDirectory, this.folderName); + if(!Directory.Exists(publicationFolder)) + Directory.CreateDirectory(publicationFolder); + string seriesInfoPath = Path.Join(publicationFolder, "series.json"); + if(!File.Exists(seriesInfoPath)) + File.WriteAllText(seriesInfoPath,this.GetSeriesInfoJson()); + } /// Serialized JSON String for series.json public string GetSeriesInfoJson() diff --git a/Tranga/TaskExecutor.cs b/Tranga/TaskExecutor.cs index b9cd75b..284cb1e 100644 --- a/Tranga/TaskExecutor.cs +++ b/Tranga/TaskExecutor.cs @@ -100,9 +100,7 @@ public static class TaskExecutor connector.CloneCoverFromCache(publication, settings); - string seriesInfoPath = Path.Join(publicationFolder, "series.json"); - if(!File.Exists(seriesInfoPath)) - File.WriteAllText(seriesInfoPath,publication.GetSeriesInfoJson()); + publication.SaveSeriesInfoJson(connector.downloadLocation); foreach(Chapter newChapter in newChapters) connector.DownloadChapter(publication, newChapter); @@ -122,7 +120,7 @@ public static class TaskExecutor chapterCollection.TryAdd(publication, newChaptersList); //To ensure publication is actually in collection Chapter[] newChapters = connector.GetChapters(publication, language); - newChaptersList = newChapters.Where(nChapter => !connector.ChapterIsDownloaded(publication, nChapter)).ToList(); + newChaptersList = newChapters.Where(nChapter => !connector.CheckChapterIsDownloaded(publication, nChapter)).ToList(); return newChaptersList; }