From 140aac8f8779a096091dddc55bd6d023de94d0f9 Mon Sep 17 00:00:00 2001 From: glax <--local> Date: Thu, 18 May 2023 16:04:03 +0200 Subject: [PATCH] Added functionality to GetChapters. --- Tranga/Connectors/MangaDex.cs | 36 ++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/Tranga/Connectors/MangaDex.cs b/Tranga/Connectors/MangaDex.cs index 234f79c..e5441d9 100644 --- a/Tranga/Connectors/MangaDex.cs +++ b/Tranga/Connectors/MangaDex.cs @@ -104,7 +104,41 @@ public class MangaDex : Connector public override Chapter[] GetChapters(Publication publication) { - throw new NotImplementedException(); + const int limit = 100; + int offset = 0; + string id = publication.downloadUrl; + int total = int.MaxValue; + List chapters = new(); + while (offset < total) + { + offset += limit; + DownloadClient.RequestResult requestResult = + _downloadClient.GetPage($"https://api.mangadex.org/manga/{id}/feed?limit={limit}&offset={offset}"); + JsonObject? result = JsonSerializer.Deserialize(requestResult.result); + if (result is null) + break; + + total = result["total"]!.GetValue(); + JsonArray chaptersInResult = result["data"]!.AsArray(); + foreach (JsonObject chapter in chaptersInResult) + { + JsonObject attributes = chapter!["attributes"]!.AsObject(); + string? title = attributes.ContainsKey("title") && attributes["title"] is not null + ? attributes["title"]!.GetValue() + : null; + + string? volume = attributes.ContainsKey("volume") && attributes["volume"] is not null + ? attributes["volume"]!.GetValue() + : null; + + string? chapterNum = attributes.ContainsKey("chapter") && attributes["chapter"] is not null + ? attributes["chapter"]!.GetValue() + : null; + + chapters.Add(new Chapter(publication, title, volume, chapterNum)); + } + } + return chapters.OrderBy(chapter => chapter.chapterNumber).ToArray(); } public override void DownloadChapter(Chapter chapter)