From 8fde7f88857445ba246b3a3c3ab3106878bfe3b9 Mon Sep 17 00:00:00 2001 From: glax <--local> Date: Thu, 18 May 2023 15:48:54 +0200 Subject: [PATCH] Added first Connector: MangaDex Can fetch all Mangas so far. --- Tranga/Connectors/MangaDex.cs | 114 ++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 Tranga/Connectors/MangaDex.cs diff --git a/Tranga/Connectors/MangaDex.cs b/Tranga/Connectors/MangaDex.cs new file mode 100644 index 0000000..f34169d --- /dev/null +++ b/Tranga/Connectors/MangaDex.cs @@ -0,0 +1,114 @@ +using System.Text.Json; +using System.Text.Json.Nodes; + +namespace Tranga.Connectors; + +public class MangaDex : Connector +{ + public override string name { get; } + private DownloadClient _downloadClient = new (); + + public MangaDex() + { + name = "MangaDex.org"; + } + + public override Publication[] GetPublications() + { + const int limit = 100; + string publicationsUrl = $"https://api.mangadex.org/manga?limit={limit}&offset="; + int offset = 0; + int total = int.MaxValue; + HashSet publications = new(); + while (offset < total) + { + DownloadClient.RequestResult requestResult = _downloadClient.GetPage(string.Concat(publicationsUrl, "0")); + JsonObject? result = JsonSerializer.Deserialize(requestResult.result); + if (result is null) + break; + + total = result["total"]!.GetValue(); + JsonArray mangaInResult = result["data"]!.AsArray(); + foreach (JsonObject manga in mangaInResult) + { + JsonObject attributes = manga["attributes"].AsObject(); + + string title = attributes["title"]!.AsObject().ContainsKey("en") && attributes["title"]!["en"] is not null + ? attributes["title"]!["en"]!.GetValue() + : ""; + + string? description = attributes["description"]!.AsObject().ContainsKey("en") && attributes["description"]!["en"] is not null + ? attributes["description"]!["en"]!.GetValue() + : null; + + JsonArray altTitlesObject = attributes["altTitles"]!.AsArray(); + string[,] altTitles = new string[altTitlesObject.Count, 2]; + int titleIndex = 0; + foreach (JsonObject altTitleObject in altTitlesObject) + { + string key = ((IDictionary)altTitleObject!).Keys.ToArray()[0]; + altTitles[titleIndex, 0] = key; + altTitles[titleIndex++, 1] = altTitleObject[key]!.GetValue(); + } + + JsonArray tagsObject = attributes["tags"]!.AsArray(); + HashSet tags = new(); + foreach (JsonObject tagObject in tagsObject) + { + if(tagObject!["attributes"]!["name"]!.AsObject().ContainsKey("en")) + tags.Add(tagObject!["attributes"]!["name"]!["en"]!.GetValue()); + } + + JsonArray relationships = manga["relationships"]!.AsArray(); + string poster = relationships.FirstOrDefault(relationship => relationship["type"].GetValue() == "cover_art")["id"].GetValue(); + + JsonObject linksObject = attributes["links"]!.AsObject(); + string[,] links = new string[linksObject.Count, 2]; + int linkIndex = 0; + foreach (string key in ((IDictionary)linksObject).Keys) + { + links[linkIndex, 0] = key; + links[linkIndex++, 1] = linksObject[key]!.GetValue(); + } + + int? year = attributes.ContainsKey("year") && attributes["year"] is not null + ? attributes["year"]!.GetValue() + : null; + + string? originalLanguage = attributes.ContainsKey("originalLanguage") && attributes["originalLanguage"] is not null + ? attributes["originalLanguage"]!.GetValue() + : null; + + string status = attributes["status"]!.GetValue(); + + Publication pub = new Publication( + title, + description, + altTitles, + tags.ToArray(), + poster, + links, + year, + originalLanguage, + status, + this, + manga["id"]!.GetValue() + ); + publications.Add(pub); + } + offset += limit; + } + + return publications.ToArray(); + } + + public override Chapter[] GetChapters(Publication publication) + { + throw new NotImplementedException(); + } + + public override void DownloadChapter(Chapter chapter) + { + throw new NotImplementedException(); + } +} \ No newline at end of file