From aa0dc4fa358ad02b84ea3871a3e2191c560af8d1 Mon Sep 17 00:00:00 2001 From: Andy Maesen Date: Thu, 6 Jul 2023 02:09:56 +0200 Subject: [PATCH] Fixes single result redirect --- Tranga-CLI/Tranga_Cli.cs | 24 +++++++++++++++++++++++- Tranga/Connector.cs | 16 ++++++++++++++++ Tranga/Connectors/MangaKatana.cs | 15 ++++++++++++++- 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/Tranga-CLI/Tranga_Cli.cs b/Tranga-CLI/Tranga_Cli.cs index 01e2835..f0d2b07 100644 --- a/Tranga-CLI/Tranga_Cli.cs +++ b/Tranga-CLI/Tranga_Cli.cs @@ -504,8 +504,30 @@ public static class Tranga_Cli Chapter[] availableChapters = connector.GetChapters(publication, "en"); int cIndex = 0; Console.WriteLine("Chapters:"); + + System.Text.StringBuilder sb = new(); foreach(Chapter chapter in availableChapters) - Console.WriteLine($"{cIndex++}: Vol.{chapter.volumeNumber} Ch.{chapter.chapterNumber} - {chapter.name}"); + { + sb.Append($"{cIndex++}: "); + + if(string.IsNullOrWhiteSpace(chapter.volumeNumber) == false) + { + sb.Append($"Vol.{chapter.volumeNumber} "); + } + + if(string.IsNullOrWhiteSpace(chapter.chapterNumber) == false) + { + sb.Append($"Ch.{chapter.chapterNumber} "); + } + + if(string.IsNullOrWhiteSpace(chapter.name) == false) + { + sb.Append($" - {chapter.name}"); + } + + Console.WriteLine(sb.ToString()); + sb.Clear(); + } Console.WriteLine("Enter q to abort"); Console.WriteLine($"Select Chapter(s):"); diff --git a/Tranga/Connector.cs b/Tranga/Connector.cs index d2e32ad..85613d3 100644 --- a/Tranga/Connector.cs +++ b/Tranga/Connector.cs @@ -341,6 +341,13 @@ public abstract class Connector logger?.WriteLine(this.GetType().ToString(), $"Request-Error {response.StatusCode}: {response.ReasonPhrase}"); return new RequestResult(response.StatusCode, Stream.Null); } + + // Request has been redirected to another page. For example, it redirects directly to the results when there is only 1 result + if(response.RequestMessage is not null && response.RequestMessage.RequestUri is not null) + { + return new RequestResult(response.StatusCode, response.Content.ReadAsStream(), true, response.RequestMessage.RequestUri.AbsoluteUri); + } + return new RequestResult(response.StatusCode, response.Content.ReadAsStream()); } @@ -348,12 +355,21 @@ public abstract class Connector { public HttpStatusCode statusCode { get; } public Stream result { get; } + public bool HasBeenRedirected { get; } + public string? RedirectedToUrl { get; } public RequestResult(HttpStatusCode statusCode, Stream result) { this.statusCode = statusCode; this.result = result; } + + public RequestResult(HttpStatusCode statusCode, Stream result, bool hasBeenRedirected, string redirectedTo) + : this(statusCode, result) + { + this.HasBeenRedirected = hasBeenRedirected; + RedirectedToUrl = redirectedTo; + } } } } \ No newline at end of file diff --git a/Tranga/Connectors/MangaKatana.cs b/Tranga/Connectors/MangaKatana.cs index 6f05b33..38b3f64 100644 --- a/Tranga/Connectors/MangaKatana.cs +++ b/Tranga/Connectors/MangaKatana.cs @@ -30,6 +30,14 @@ public class MangaKatana : Connector if ((int)requestResult.statusCode < 200 || (int)requestResult.statusCode >= 300) return Array.Empty(); + // If a single result is found, the user will be redirected to the results directly instead of a result page + if(requestResult.HasBeenRedirected + && requestResult.RedirectedToUrl is not null + && requestResult.RedirectedToUrl.Contains("mangakatana.com/manga")) + { + return new Publication[] { ParseSinglePublicationFromHtml(requestResult.result, requestResult.RedirectedToUrl.Split('/')[^1]) }; + } + return ParsePublicationsFromHtml(requestResult.result); } @@ -113,9 +121,14 @@ public class MangaKatana : Connector while (description.StartsWith('\n')) description = description.Substring(1); + int year = DateTime.Now.Year; string yearString = infoTable.Descendants("div").First(d => d.HasClass("updateAt")) .InnerText.Split('-')[^1]; - int year = Convert.ToInt32(yearString); + + if(yearString.Contains("ago") == false) + { + year = Convert.ToInt32(yearString); + } return new Publication(sortName, authors.ToList(), description, altTitles, tags.ToArray(), posterUrl, coverFileNameInCache, links, year, originalLanguage, status, publicationId);