From 5df63b00c2b82e6f8d2f5245bc564215b551b53f Mon Sep 17 00:00:00 2001 From: glax Date: Wed, 3 Jan 2024 17:31:00 +0100 Subject: [PATCH] Moved Struct RequestResult to own file --- Tranga/MangaConnectors/Bato.cs | 12 +++++------ Tranga/MangaConnectors/DownloadClient.cs | 24 --------------------- Tranga/MangaConnectors/MangaConnector.cs | 4 ++-- Tranga/MangaConnectors/MangaDex.cs | 12 +++++------ Tranga/MangaConnectors/MangaKatana.cs | 8 +++---- Tranga/MangaConnectors/MangaLife.cs | 8 +++---- Tranga/MangaConnectors/Manganato.cs | 8 +++---- Tranga/MangaConnectors/Mangasee.cs | 6 +++--- Tranga/MangaConnectors/Mangaworld.cs | 8 +++---- Tranga/MangaConnectors/RequestResult.cs | 27 ++++++++++++++++++++++++ 10 files changed, 60 insertions(+), 57 deletions(-) create mode 100644 Tranga/MangaConnectors/RequestResult.cs diff --git a/Tranga/MangaConnectors/Bato.cs b/Tranga/MangaConnectors/Bato.cs index 7ca30da..1614990 100644 --- a/Tranga/MangaConnectors/Bato.cs +++ b/Tranga/MangaConnectors/Bato.cs @@ -20,7 +20,7 @@ public class Bato : MangaConnector Log($"Searching Publications. Term=\"{publicationTitle}\""); string sanitizedTitle = string.Join(' ', Regex.Matches(publicationTitle, "[A-z]*").Where(m => m.Value.Length > 0)).ToLower(); string requestUrl = $"https://bato.to/v3x-search?word={sanitizedTitle}&lang=en"; - DownloadClient.RequestResult requestResult = + RequestResult requestResult = downloadClient.MakeRequest(requestUrl, 1); if ((int)requestResult.statusCode < 200 || (int)requestResult.statusCode >= 300) return Array.Empty(); @@ -43,7 +43,7 @@ public class Bato : MangaConnector public override Manga? GetMangaFromUrl(string url) { - DownloadClient.RequestResult requestResult = + RequestResult requestResult = downloadClient.MakeRequest(url, 1); if ((int)requestResult.statusCode < 200 || (int)requestResult.statusCode >= 300) return null; @@ -128,7 +128,7 @@ public class Bato : MangaConnector Log($"Getting chapters {manga}"); string requestUrl = $"https://bato.to/title/{manga.publicationId}"; // Leaving this in for verification if the page exists - DownloadClient.RequestResult requestResult = + RequestResult requestResult = downloadClient.MakeRequest(requestUrl, 1); if ((int)requestResult.statusCode < 200 || (int)requestResult.statusCode >= 300) return Array.Empty(); @@ -141,7 +141,7 @@ public class Bato : MangaConnector private List ParseChaptersFromHtml(Manga manga, string mangaUrl) { - DownloadClient.RequestResult result = downloadClient.MakeRequest(mangaUrl, 1); + RequestResult result = downloadClient.MakeRequest(mangaUrl, 1); if ((int)result.statusCode < 200 || (int)result.statusCode >= 300 || result.htmlDocument is null) { Log("Failed to load site"); @@ -183,7 +183,7 @@ public class Bato : MangaConnector Log($"Retrieving chapter-info {chapter} {chapterParentManga}"); string requestUrl = chapter.url; // Leaving this in to check if the page exists - DownloadClient.RequestResult requestResult = + RequestResult requestResult = downloadClient.MakeRequest(requestUrl, 1); if ((int)requestResult.statusCode < 200 || (int)requestResult.statusCode >= 300) { @@ -201,7 +201,7 @@ public class Bato : MangaConnector private string[] ParseImageUrlsFromHtml(string mangaUrl) { - DownloadClient.RequestResult requestResult = + RequestResult requestResult = downloadClient.MakeRequest(mangaUrl, 1); if ((int)requestResult.statusCode < 200 || (int)requestResult.statusCode >= 300) { diff --git a/Tranga/MangaConnectors/DownloadClient.cs b/Tranga/MangaConnectors/DownloadClient.cs index 80b050f..50d2578 100644 --- a/Tranga/MangaConnectors/DownloadClient.cs +++ b/Tranga/MangaConnectors/DownloadClient.cs @@ -42,28 +42,4 @@ internal abstract class DownloadClient : GlobalBase protected abstract RequestResult MakeRequestInternal(string url, string? referrer = null); public abstract void Close(); - - public struct RequestResult - { - public HttpStatusCode statusCode { get; } - public Stream result { get; } - public bool hasBeenRedirected { get; } - public string? redirectedToUrl { get; } - public HtmlDocument? htmlDocument { get; } - - public RequestResult(HttpStatusCode statusCode, HtmlDocument? htmlDocument, Stream result) - { - this.statusCode = statusCode; - this.htmlDocument = htmlDocument; - this.result = result; - } - - public RequestResult(HttpStatusCode statusCode, HtmlDocument? htmlDocument, Stream result, bool hasBeenRedirected, string redirectedTo) - : this(statusCode, htmlDocument, result) - { - this.hasBeenRedirected = hasBeenRedirected; - redirectedToUrl = redirectedTo; - } - } - } \ No newline at end of file diff --git a/Tranga/MangaConnectors/MangaConnector.cs b/Tranga/MangaConnectors/MangaConnector.cs index 31ffc31..cfd62a6 100644 --- a/Tranga/MangaConnectors/MangaConnector.cs +++ b/Tranga/MangaConnectors/MangaConnector.cs @@ -204,7 +204,7 @@ public abstract class MangaConnector : GlobalBase /// referrer used in html request header private HttpStatusCode DownloadImage(string imageUrl, string fullPath, byte requestType, string? referrer = null) { - DownloadClient.RequestResult requestResult = downloadClient.MakeRequest(imageUrl, requestType, referrer); + RequestResult requestResult = downloadClient.MakeRequest(imageUrl, requestType, referrer); if ((int)requestResult.statusCode < 200 || (int)requestResult.statusCode >= 300) return requestResult.statusCode; @@ -283,7 +283,7 @@ public abstract class MangaConnector : GlobalBase if (File.Exists(saveImagePath)) return filename; - DownloadClient.RequestResult coverResult = downloadClient.MakeRequest(url, requestType); + RequestResult coverResult = downloadClient.MakeRequest(url, requestType); using MemoryStream ms = new(); coverResult.result.CopyTo(ms); File.WriteAllBytes(saveImagePath, ms.ToArray()); diff --git a/Tranga/MangaConnectors/MangaDex.cs b/Tranga/MangaConnectors/MangaDex.cs index c0a39de..27a0403 100644 --- a/Tranga/MangaConnectors/MangaDex.cs +++ b/Tranga/MangaConnectors/MangaDex.cs @@ -39,7 +39,7 @@ public class MangaDex : MangaConnector while (offset < total) //As long as we haven't requested all "Pages" { //Request next Page - DownloadClient.RequestResult requestResult = + RequestResult requestResult = downloadClient.MakeRequest( $"https://api.mangadex.org/manga?limit={limit}&title={publicationTitle}&offset={offset}", (byte)RequestType.Manga); if ((int)requestResult.statusCode < 200 || (int)requestResult.statusCode >= 300) @@ -74,7 +74,7 @@ public class MangaDex : MangaConnector public override Manga? GetMangaFromId(string publicationId) { - DownloadClient.RequestResult requestResult = + RequestResult requestResult = downloadClient.MakeRequest($"https://api.mangadex.org/manga/{publicationId}", (byte)RequestType.Manga); if ((int)requestResult.statusCode < 200 || (int)requestResult.statusCode >= 300) return null; @@ -214,7 +214,7 @@ public class MangaDex : MangaConnector while (offset < total) { //Request next "Page" - DownloadClient.RequestResult requestResult = + RequestResult requestResult = downloadClient.MakeRequest( $"https://api.mangadex.org/manga/{manga.publicationId}/feed?limit={limit}&offset={offset}&translatedLanguage%5B%5D={language}", (byte)RequestType.Feed); if ((int)requestResult.statusCode < 200 || (int)requestResult.statusCode >= 300) @@ -267,7 +267,7 @@ public class MangaDex : MangaConnector Manga chapterParentManga = chapter.parentManga; Log($"Retrieving chapter-info {chapter} {chapterParentManga}"); //Request URLs for Chapter-Images - DownloadClient.RequestResult requestResult = + RequestResult requestResult = downloadClient.MakeRequest($"https://api.mangadex.org/at-home/server/{chapter.url}?forcePort443=false'", (byte)RequestType.AtHomeServer); if ((int)requestResult.statusCode < 200 || (int)requestResult.statusCode >= 300) { @@ -306,7 +306,7 @@ public class MangaDex : MangaConnector } //Request information where to download Cover - DownloadClient.RequestResult requestResult = + RequestResult requestResult = downloadClient.MakeRequest($"https://api.mangadex.org/cover/{posterId}", (byte)RequestType.CoverUrl); if ((int)requestResult.statusCode < 200 || (int)requestResult.statusCode >= 300) return null; @@ -327,7 +327,7 @@ public class MangaDex : MangaConnector List ret = new(); foreach (string authorId in authorIds) { - DownloadClient.RequestResult requestResult = + RequestResult requestResult = downloadClient.MakeRequest($"https://api.mangadex.org/author/{authorId}", (byte)RequestType.Author); if ((int)requestResult.statusCode < 200 || (int)requestResult.statusCode >= 300) return ret; diff --git a/Tranga/MangaConnectors/MangaKatana.cs b/Tranga/MangaConnectors/MangaKatana.cs index 98de08a..58a7529 100644 --- a/Tranga/MangaConnectors/MangaKatana.cs +++ b/Tranga/MangaConnectors/MangaKatana.cs @@ -20,7 +20,7 @@ public class MangaKatana : MangaConnector Log($"Searching Publications. Term=\"{publicationTitle}\""); string sanitizedTitle = string.Join('_', Regex.Matches(publicationTitle, "[A-z]*").Where(m => m.Value.Length > 0)).ToLower(); string requestUrl = $"https://mangakatana.com/?search={sanitizedTitle}&search_by=book_name"; - DownloadClient.RequestResult requestResult = + RequestResult requestResult = downloadClient.MakeRequest(requestUrl, 1); if ((int)requestResult.statusCode < 200 || (int)requestResult.statusCode >= 300) return Array.Empty(); @@ -46,7 +46,7 @@ public class MangaKatana : MangaConnector public override Manga? GetMangaFromUrl(string url) { - DownloadClient.RequestResult requestResult = + RequestResult requestResult = downloadClient.MakeRequest(url, 1); if ((int)requestResult.statusCode < 200 || (int)requestResult.statusCode >= 300) return null; @@ -157,7 +157,7 @@ public class MangaKatana : MangaConnector Log($"Getting chapters {manga}"); string requestUrl = $"https://mangakatana.com/manga/{manga.publicationId}"; // Leaving this in for verification if the page exists - DownloadClient.RequestResult requestResult = + RequestResult requestResult = downloadClient.MakeRequest(requestUrl, 1); if ((int)requestResult.statusCode < 200 || (int)requestResult.statusCode >= 300) return Array.Empty(); @@ -209,7 +209,7 @@ public class MangaKatana : MangaConnector Log($"Retrieving chapter-info {chapter} {chapterParentManga}"); string requestUrl = chapter.url; // Leaving this in to check if the page exists - DownloadClient.RequestResult requestResult = + RequestResult requestResult = downloadClient.MakeRequest(requestUrl, 1); if ((int)requestResult.statusCode < 200 || (int)requestResult.statusCode >= 300) { diff --git a/Tranga/MangaConnectors/MangaLife.cs b/Tranga/MangaConnectors/MangaLife.cs index 1e3f924..3d0d26e 100644 --- a/Tranga/MangaConnectors/MangaLife.cs +++ b/Tranga/MangaConnectors/MangaLife.cs @@ -20,7 +20,7 @@ public class MangaLife : MangaConnector Log($"Searching Publications. Term=\"{publicationTitle}\""); string sanitizedTitle = WebUtility.UrlEncode(publicationTitle); string requestUrl = $"https://manga4life.com/search/?name={sanitizedTitle}"; - DownloadClient.RequestResult requestResult = + RequestResult requestResult = downloadClient.MakeRequest(requestUrl, 1); if ((int)requestResult.statusCode < 200 || (int)requestResult.statusCode >= 300) return Array.Empty(); @@ -42,7 +42,7 @@ public class MangaLife : MangaConnector Regex publicationIdRex = new(@"https:\/\/manga4life.com\/manga\/(.*)(\/.*)*"); string publicationId = publicationIdRex.Match(url).Groups[1].Value; - DownloadClient.RequestResult requestResult = this.downloadClient.MakeRequest(url, 1); + RequestResult requestResult = this.downloadClient.MakeRequest(url, 1); if(requestResult.htmlDocument is not null) return ParseSinglePublicationFromHtml(requestResult.htmlDocument, publicationId); return null; @@ -133,7 +133,7 @@ public class MangaLife : MangaConnector public override Chapter[] GetChapters(Manga manga, string language="en") { Log($"Getting chapters {manga}"); - DownloadClient.RequestResult result = downloadClient.MakeRequest($"https://manga4life.com/manga/{manga.publicationId}", 1); + RequestResult result = downloadClient.MakeRequest($"https://manga4life.com/manga/{manga.publicationId}", 1); if ((int)result.statusCode < 200 || (int)result.statusCode >= 300 || result.htmlDocument is null) { return Array.Empty(); @@ -179,7 +179,7 @@ public class MangaLife : MangaConnector Log($"Retrieving chapter-info {chapter} {chapterParentManga}"); - DownloadClient.RequestResult requestResult = this.downloadClient.MakeRequest(chapter.url, 1); + RequestResult requestResult = this.downloadClient.MakeRequest(chapter.url, 1); if (requestResult.htmlDocument is null) { progressToken?.Cancel(); diff --git a/Tranga/MangaConnectors/Manganato.cs b/Tranga/MangaConnectors/Manganato.cs index 4367b1f..9db2d11 100644 --- a/Tranga/MangaConnectors/Manganato.cs +++ b/Tranga/MangaConnectors/Manganato.cs @@ -20,7 +20,7 @@ public class Manganato : MangaConnector Log($"Searching Publications. Term=\"{publicationTitle}\""); string sanitizedTitle = string.Join('_', Regex.Matches(publicationTitle, "[A-z]*").Where(str => str.Length > 0)).ToLower(); string requestUrl = $"https://manganato.com/search/story/{sanitizedTitle}"; - DownloadClient.RequestResult requestResult = + RequestResult requestResult = downloadClient.MakeRequest(requestUrl, 1); if ((int)requestResult.statusCode < 200 || (int)requestResult.statusCode >= 300) return Array.Empty(); @@ -61,7 +61,7 @@ public class Manganato : MangaConnector public override Manga? GetMangaFromUrl(string url) { - DownloadClient.RequestResult requestResult = + RequestResult requestResult = downloadClient.MakeRequest(url, 1); if ((int)requestResult.statusCode < 200 || (int)requestResult.statusCode >= 300) return null; @@ -142,7 +142,7 @@ public class Manganato : MangaConnector { Log($"Getting chapters {manga}"); string requestUrl = $"https://chapmanganato.com/{manga.publicationId}"; - DownloadClient.RequestResult requestResult = + RequestResult requestResult = downloadClient.MakeRequest(requestUrl, 1); if ((int)requestResult.statusCode < 200 || (int)requestResult.statusCode >= 300) return Array.Empty(); @@ -196,7 +196,7 @@ public class Manganato : MangaConnector Manga chapterParentManga = chapter.parentManga; Log($"Retrieving chapter-info {chapter} {chapterParentManga}"); string requestUrl = chapter.url; - DownloadClient.RequestResult requestResult = + RequestResult requestResult = downloadClient.MakeRequest(requestUrl, 1); if ((int)requestResult.statusCode < 200 || (int)requestResult.statusCode >= 300) { diff --git a/Tranga/MangaConnectors/Mangasee.cs b/Tranga/MangaConnectors/Mangasee.cs index e4e54b6..ed92f12 100644 --- a/Tranga/MangaConnectors/Mangasee.cs +++ b/Tranga/MangaConnectors/Mangasee.cs @@ -29,7 +29,7 @@ public class Mangasee : MangaConnector { Log($"Searching Publications. Term=\"{publicationTitle}\""); string requestUrl = "https://mangasee123.com/_search.php"; - DownloadClient.RequestResult requestResult = + RequestResult requestResult = downloadClient.MakeRequest(requestUrl, 1); if ((int)requestResult.statusCode < 200 || (int)requestResult.statusCode >= 300) { @@ -121,7 +121,7 @@ public class Mangasee : MangaConnector Regex publicationIdRex = new(@"https:\/\/mangasee123.com\/manga\/(.*)(\/.*)*"); string publicationId = publicationIdRex.Match(url).Groups[1].Value; - DownloadClient.RequestResult requestResult = this.downloadClient.MakeRequest(url, 1); + RequestResult requestResult = this.downloadClient.MakeRequest(url, 1); if(requestResult.htmlDocument is not null) return ParseSinglePublicationFromHtml(requestResult.htmlDocument, publicationId); return null; @@ -232,7 +232,7 @@ public class Mangasee : MangaConnector Log($"Retrieving chapter-info {chapter} {chapterParentManga}"); - DownloadClient.RequestResult requestResult = this.downloadClient.MakeRequest(chapter.url, 1); + RequestResult requestResult = this.downloadClient.MakeRequest(chapter.url, 1); if (requestResult.htmlDocument is null) { progressToken?.Cancel(); diff --git a/Tranga/MangaConnectors/Mangaworld.cs b/Tranga/MangaConnectors/Mangaworld.cs index 97e8f6f..a64a656 100644 --- a/Tranga/MangaConnectors/Mangaworld.cs +++ b/Tranga/MangaConnectors/Mangaworld.cs @@ -20,7 +20,7 @@ public class Mangaworld: MangaConnector Log($"Searching Publications. Term=\"{publicationTitle}\""); string sanitizedTitle = string.Join(' ', Regex.Matches(publicationTitle, "[A-z]*").Where(str => str.Length > 0)).ToLower(); string requestUrl = $"https://www.mangaworld.bz/archive?keyword={sanitizedTitle}"; - DownloadClient.RequestResult requestResult = + RequestResult requestResult = downloadClient.MakeRequest(requestUrl, 1); if ((int)requestResult.statusCode < 200 || (int)requestResult.statusCode >= 300) return Array.Empty(); @@ -61,7 +61,7 @@ public class Mangaworld: MangaConnector public override Manga? GetMangaFromUrl(string url) { - DownloadClient.RequestResult requestResult = + RequestResult requestResult = downloadClient.MakeRequest(url, 1); if ((int)requestResult.statusCode < 200 || (int)requestResult.statusCode >= 300) return null; @@ -131,7 +131,7 @@ public class Mangaworld: MangaConnector { Log($"Getting chapters {manga}"); string requestUrl = $"https://www.mangaworld.bz/manga/{manga.publicationId}"; - DownloadClient.RequestResult requestResult = + RequestResult requestResult = downloadClient.MakeRequest(requestUrl, 1); if ((int)requestResult.statusCode < 200 || (int)requestResult.statusCode >= 300) return Array.Empty(); @@ -190,7 +190,7 @@ public class Mangaworld: MangaConnector Manga chapterParentManga = chapter.parentManga; Log($"Retrieving chapter-info {chapter} {chapterParentManga}"); string requestUrl = $"{chapter.url}?style=list"; - DownloadClient.RequestResult requestResult = + RequestResult requestResult = downloadClient.MakeRequest(requestUrl, 1); if ((int)requestResult.statusCode < 200 || (int)requestResult.statusCode >= 300) { diff --git a/Tranga/MangaConnectors/RequestResult.cs b/Tranga/MangaConnectors/RequestResult.cs new file mode 100644 index 0000000..4ba4a89 --- /dev/null +++ b/Tranga/MangaConnectors/RequestResult.cs @@ -0,0 +1,27 @@ +using System.Net; +using HtmlAgilityPack; + +namespace Tranga.MangaConnectors; + +public struct RequestResult +{ + public HttpStatusCode statusCode { get; } + public Stream result { get; } + public bool hasBeenRedirected { get; } + public string? redirectedToUrl { get; } + public HtmlDocument? htmlDocument { get; } + + public RequestResult(HttpStatusCode statusCode, HtmlDocument? htmlDocument, Stream result) + { + this.statusCode = statusCode; + this.htmlDocument = htmlDocument; + this.result = result; + } + + public RequestResult(HttpStatusCode statusCode, HtmlDocument? htmlDocument, Stream result, bool hasBeenRedirected, string redirectedTo) + : this(statusCode, htmlDocument, result) + { + this.hasBeenRedirected = hasBeenRedirected; + redirectedToUrl = redirectedTo; + } +} \ No newline at end of file