From 668a3b3a96e3161789fc338da777942fa53387dd Mon Sep 17 00:00:00 2001 From: glax Date: Wed, 4 Oct 2023 18:14:12 +0200 Subject: [PATCH] MangaDex nullchecking in response --- Tranga/MangaConnectors/MangaDex.cs | 43 ++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/Tranga/MangaConnectors/MangaDex.cs b/Tranga/MangaConnectors/MangaDex.cs index cd0226e..89d7d2e 100644 --- a/Tranga/MangaConnectors/MangaDex.cs +++ b/Tranga/MangaConnectors/MangaDex.cs @@ -51,16 +51,23 @@ public class MangaDex : MangaConnector if (result is null) break; - total = result["total"]!.GetValue(); //Update the total number of Publications - - JsonArray mangaInResult = result["data"]!.AsArray(); //Manga-data-Array - //Loop each Manga and extract information from JSON - foreach (JsonNode? mangaNode in mangaInResult) + if(result.ContainsKey("total")) + total = result["total"]!.GetValue(); //Update the total number of Publications + else continue; + + if (result.ContainsKey("data")) { - Log($"Getting publication data. {++loadedPublicationData}/{total}"); - Manga manga = MangaFromJsonObject((JsonObject)mangaNode); - retManga.Add(manga); //Add Publication (Manga) to result - } + JsonArray mangaInResult = result["data"]!.AsArray(); //Manga-data-Array + //Loop each Manga and extract information from JSON + foreach (JsonNode? mangaNode in mangaInResult) + { + if(mangaNode is null) + continue; + Log($"Getting publication data. {++loadedPublicationData}/{total}"); + if(MangaFromJsonObject((JsonObject) mangaNode) is { } manga) + retManga.Add(manga); //Add Publication (Manga) to result + } + }else continue; } Log($"Retrieved {retManga.Count} publications. Term=\"{publicationTitle}\""); return retManga.ToArray(); @@ -81,20 +88,30 @@ public class MangaDex : MangaConnector return null; } - private Manga MangaFromJsonObject(JsonObject manga) + private Manga? MangaFromJsonObject(JsonObject manga) { + if (!manga.ContainsKey("attributes")) + return null; JsonObject attributes = manga["attributes"]!.AsObject(); - + + if(!manga.ContainsKey("id")) + return null; string publicationId = manga["id"]!.GetValue(); + if(!attributes.ContainsKey("title")) + return null; string title = attributes["title"]!.AsObject().ContainsKey("en") && attributes["title"]!["en"] is not null ? attributes["title"]!["en"]!.GetValue() : attributes["title"]![((IDictionary)attributes["title"]!.AsObject()).Keys.First()]!.GetValue(); + if(!attributes.ContainsKey("description")) + return null; string? description = attributes["description"]!.AsObject().ContainsKey("en") && attributes["description"]!["en"] is not null ? attributes["description"]!["en"]!.GetValue() : null; + if(!attributes.ContainsKey("altTitles")) + return null; JsonArray altTitlesObject = attributes["altTitles"]!.AsArray(); Dictionary altTitlesDict = new(); foreach (JsonNode? altTitleNode in altTitlesObject) @@ -104,6 +121,8 @@ public class MangaDex : MangaConnector altTitlesDict.TryAdd(key, altTitleObject[key]!.GetValue()); } + if(!attributes.ContainsKey("tags")) + return null; JsonArray tagsObject = attributes["tags"]!.AsArray(); HashSet tags = new(); foreach (JsonNode? tagNode in tagsObject) @@ -149,6 +168,8 @@ public class MangaDex : MangaConnector ? attributes["originalLanguage"]!.GetValue() : null; + if(!attributes.ContainsKey("status")) + return null; string status = attributes["status"]!.GetValue(); Manga pub = new(