From e663163de82cf1867e7687c8de9a31f28fd04118 Mon Sep 17 00:00:00 2001 From: glax Date: Thu, 31 Aug 2023 12:14:03 +0200 Subject: [PATCH] Download Covers only when Downloading Chapters --- Tranga/MangaConnectors/MangaDex.cs | 9 ++++----- Tranga/MangaConnectors/MangaKatana.cs | 10 ++++++---- Tranga/MangaConnectors/Manganato.cs | 10 ++++++---- Tranga/MangaConnectors/Mangasee.cs | 9 ++++++--- Tranga/Publication.cs | 10 +++++----- 5 files changed, 27 insertions(+), 21 deletions(-) diff --git a/Tranga/MangaConnectors/MangaDex.cs b/Tranga/MangaConnectors/MangaDex.cs index 21900c1..40d3000 100644 --- a/Tranga/MangaConnectors/MangaDex.cs +++ b/Tranga/MangaConnectors/MangaDex.cs @@ -102,9 +102,6 @@ public class MangaDex : MangaConnector authorIds.Add(node!["id"]!.GetValue()); } string? coverUrl = GetCoverUrl(publicationId, posterId); - string? coverCacheName = null; - if (coverUrl is not null) - coverCacheName = SaveCoverImageToCache(coverUrl, (byte)RequestType.AtHomeServer); List authors = GetAuthors(authorIds); @@ -135,7 +132,6 @@ public class MangaDex : MangaConnector altTitlesDict, tags.ToArray(), coverUrl, - coverCacheName, linksDict, year, originalLanguage, @@ -209,7 +205,8 @@ public class MangaDex : MangaConnector { if (progressToken?.cancellationRequested ?? false) return HttpStatusCode.RequestTimeout; - Log($"Retrieving chapter-info {chapter} {chapter.parentPublication}"); + Publication chapterParentPublication = chapter.parentPublication; + Log($"Retrieving chapter-info {chapter} {chapterParentPublication}"); //Request URLs for Chapter-Images DownloadClient.RequestResult requestResult = downloadClient.MakeRequest($"https://api.mangadex.org/at-home/server/{chapter.url}?forcePort443=false'", (byte)RequestType.AtHomeServer); @@ -230,6 +227,8 @@ public class MangaDex : MangaConnector string comicInfoPath = Path.GetTempFileName(); File.WriteAllText(comicInfoPath, chapter.GetComicInfoXmlString()); + if (chapterParentPublication.coverUrl is not null) + chapterParentPublication.coverFileNameInCache = SaveCoverImageToCache(chapterParentPublication.coverUrl, (byte)RequestType.AtHomeServer); //Download Chapter-Images return DownloadChapterImages(imageUrls.ToArray(), chapter.GetArchiveFilePath(settings.downloadLocation), (byte)RequestType.AtHomeServer, comicInfoPath, progressToken:progressToken); } diff --git a/Tranga/MangaConnectors/MangaKatana.cs b/Tranga/MangaConnectors/MangaKatana.cs index 76fe5e3..8f8dfbb 100644 --- a/Tranga/MangaConnectors/MangaKatana.cs +++ b/Tranga/MangaConnectors/MangaKatana.cs @@ -119,8 +119,6 @@ public class MangaKatana : MangaConnector string posterUrl = document.DocumentNode.SelectSingleNode("//*[@id='single_book']/div[1]/div").Descendants("img").First() .GetAttributes().First(a => a.Name == "src").Value; - string coverFileNameInCache = SaveCoverImageToCache(posterUrl, 1); - string description = document.DocumentNode.SelectSingleNode("//*[@id='single_book']/div[3]/p").InnerText; while (description.StartsWith('\n')) description = description.Substring(1); @@ -134,7 +132,7 @@ public class MangaKatana : MangaConnector year = Convert.ToInt32(yearString); } - return new Publication(sortName, authors.ToList(), description, altTitles, tags.ToArray(), posterUrl, coverFileNameInCache, links, + return new Publication(sortName, authors.ToList(), description, altTitles, tags.ToArray(), posterUrl, links, year, originalLanguage, status, publicationId); } @@ -187,7 +185,8 @@ public class MangaKatana : MangaConnector { if (progressToken?.cancellationRequested ?? false) return HttpStatusCode.RequestTimeout; - Log($"Retrieving chapter-info {chapter} {chapter.parentPublication}"); + Publication chapterParentPublication = chapter.parentPublication; + Log($"Retrieving chapter-info {chapter} {chapterParentPublication}"); string requestUrl = chapter.url; // Leaving this in to check if the page exists DownloadClient.RequestResult requestResult = @@ -200,6 +199,9 @@ public class MangaKatana : MangaConnector string comicInfoPath = Path.GetTempFileName(); File.WriteAllText(comicInfoPath, chapter.GetComicInfoXmlString()); + if (chapterParentPublication.coverUrl is not null) + chapterParentPublication.coverFileNameInCache = SaveCoverImageToCache(chapterParentPublication.coverUrl, 1); + return DownloadChapterImages(imageUrls, chapter.GetArchiveFilePath(settings.downloadLocation), 1, comicInfoPath, "https://mangakatana.com/", progressToken:progressToken); } diff --git a/Tranga/MangaConnectors/Manganato.cs b/Tranga/MangaConnectors/Manganato.cs index d090bf9..0568995 100644 --- a/Tranga/MangaConnectors/Manganato.cs +++ b/Tranga/MangaConnectors/Manganato.cs @@ -111,8 +111,6 @@ public class Manganato : MangaConnector string posterUrl = document.DocumentNode.Descendants("span").First(s => s.HasClass("info-image")).Descendants("img").First() .GetAttributes().First(a => a.Name == "src").Value; - string coverFileNameInCache = SaveCoverImageToCache(posterUrl, 1); - string description = document.DocumentNode.Descendants("div").First(d => d.HasClass("panel-story-info-description")) .InnerText.Replace("Description :", ""); while (description.StartsWith('\n')) @@ -122,7 +120,7 @@ public class Manganato : MangaConnector .First(s => s.HasClass("chapter-time")).InnerText; int year = Convert.ToInt32(yearString.Split(',')[^1]) + 2000; - return new Publication(sortName, authors.ToList(), description, altTitles, tags.ToArray(), posterUrl, coverFileNameInCache, links, + return new Publication(sortName, authors.ToList(), description, altTitles, tags.ToArray(), posterUrl, links, year, originalLanguage, status, publicationId); } @@ -174,7 +172,8 @@ public class Manganato : MangaConnector { if (progressToken?.cancellationRequested ?? false) return HttpStatusCode.RequestTimeout; - Log($"Retrieving chapter-info {chapter} {chapter.parentPublication}"); + Publication chapterParentPublication = chapter.parentPublication; + Log($"Retrieving chapter-info {chapter} {chapterParentPublication}"); string requestUrl = chapter.url; DownloadClient.RequestResult requestResult = downloadClient.MakeRequest(requestUrl, 1); @@ -186,6 +185,9 @@ public class Manganato : MangaConnector string comicInfoPath = Path.GetTempFileName(); File.WriteAllText(comicInfoPath, chapter.GetComicInfoXmlString()); + if (chapterParentPublication.coverUrl is not null) + chapterParentPublication.coverFileNameInCache = SaveCoverImageToCache(chapterParentPublication.coverUrl, 1); + return DownloadChapterImages(imageUrls, chapter.GetArchiveFilePath(settings.downloadLocation), 1, comicInfoPath, "https://chapmanganato.com/", progressToken:progressToken); } diff --git a/Tranga/MangaConnectors/Mangasee.cs b/Tranga/MangaConnectors/Mangasee.cs index 75e99b0..c470eaf 100644 --- a/Tranga/MangaConnectors/Mangasee.cs +++ b/Tranga/MangaConnectors/Mangasee.cs @@ -133,7 +133,6 @@ public class Mangasee : MangaConnector HtmlNode posterNode = document.DocumentNode.Descendants("img").First(img => img.HasClass("img-fluid") && img.HasClass("bottom-5")); string posterUrl = posterNode.GetAttributeValue("src", ""); - string coverFileNameInCache = SaveCoverImageToCache(posterUrl, 1); HtmlNode attributes = document.DocumentNode.Descendants("div") .First(div => div.HasClass("col-md-9") && div.HasClass("col-sm-8") && div.HasClass("top-5")) @@ -171,7 +170,7 @@ public class Mangasee : MangaConnector foreach(string at in a) altTitles.Add((i++).ToString(), at); - return new Publication(sortName, authors, description, altTitles, tags.ToArray(), posterUrl, coverFileNameInCache, links, + return new Publication(sortName, authors, description, altTitles, tags.ToArray(), posterUrl, links, year, originalLanguage, status, publicationId); } @@ -245,6 +244,7 @@ public class Mangasee : MangaConnector { if (progressToken?.cancellationRequested ?? false) return HttpStatusCode.RequestTimeout; + Publication chapterParentPublication = chapter.parentPublication; while (this._browser is null && !(progressToken?.cancellationRequested??false)) { Log("Waiting for headless browser to download..."); @@ -253,7 +253,7 @@ public class Mangasee : MangaConnector if (progressToken?.cancellationRequested??false) return HttpStatusCode.RequestTimeout; - Log($"Retrieving chapter-info {chapter} {chapter.parentPublication}"); + Log($"Retrieving chapter-info {chapter} {chapterParentPublication}"); IPage page = _browser!.NewPageAsync().Result; IResponse response = page.GoToAsync(chapter.url).Result; if (response.Ok) @@ -270,6 +270,9 @@ public class Mangasee : MangaConnector string comicInfoPath = Path.GetTempFileName(); File.WriteAllText(comicInfoPath, chapter.GetComicInfoXmlString()); + if (chapterParentPublication.coverUrl is not null) + chapterParentPublication.coverFileNameInCache = SaveCoverImageToCache(chapterParentPublication.coverUrl, 1); + return DownloadChapterImages(urls.ToArray(), chapter.GetArchiveFilePath(settings.downloadLocation), 1, comicInfoPath, progressToken:progressToken); } return response.Status; diff --git a/Tranga/Publication.cs b/Tranga/Publication.cs index aceaee5..668fe3f 100644 --- a/Tranga/Publication.cs +++ b/Tranga/Publication.cs @@ -19,8 +19,8 @@ public struct Publication public string? description { get; } public string[] tags { get; } // ReSharper disable once UnusedAutoPropertyAccessor.Global - public string? posterUrl { get; } - public string? coverFileNameInCache { get; } + public string? coverUrl { get; } + public string? coverFileNameInCache { get; set; } // ReSharper disable once UnusedAutoPropertyAccessor.Global public Dictionary links { get; } // ReSharper disable once MemberCanBePrivate.Global @@ -36,15 +36,15 @@ public struct Publication private static readonly Regex LegalCharacters = new (@"[A-Z]*[a-z]*[0-9]* *\.*-*,*'*\'*\)*\(*~*!*"); [JsonConstructor] - public Publication(string sortName, List authors, string? description, Dictionary altTitles, string[] tags, string? posterUrl, string? coverFileNameInCache, Dictionary? links, int? year, string? originalLanguage, string status, string publicationId, string? folderName = null, float? ignoreChaptersBelow = 0) + public Publication(string sortName, List authors, string? description, Dictionary altTitles, string[] tags, string? coverUrl, Dictionary? links, int? year, string? originalLanguage, string status, string publicationId, string? folderName = null, float? ignoreChaptersBelow = 0) { this.sortName = sortName; this.authors = authors; this.description = description; this.altTitles = altTitles; this.tags = tags; - this.coverFileNameInCache = coverFileNameInCache; - this.posterUrl = posterUrl; + this.coverFileNameInCache = null; + this.coverUrl = coverUrl; this.links = links ?? new Dictionary(); this.year = year; this.originalLanguage = originalLanguage;