From 0e0ba1796e57b42c090444fbf79f2d31caafdc33 Mon Sep 17 00:00:00 2001 From: glax Date: Mon, 22 May 2023 17:20:07 +0200 Subject: [PATCH] closes #7 --- Tranga/Connector.cs | 1 + Tranga/Connectors/MangaDex.cs | 28 ++++++++++++++++++++++++---- Tranga/Publication.cs | 4 +++- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/Tranga/Connector.cs b/Tranga/Connector.cs index 38d4e58..e2908b8 100644 --- a/Tranga/Connector.cs +++ b/Tranga/Connector.cs @@ -85,6 +85,7 @@ public abstract class Connector new XElement("Tags", string.Join(',',publication.tags)), new XElement("LanguageISO", publication.originalLanguage), new XElement("Title", chapter.name), + new XElement("Writer", publication.author), new XElement("Volume", chapter.volumeNumber), new XElement("Number", chapter.chapterNumber) //TODO check if this is correct at some point ); diff --git a/Tranga/Connectors/MangaDex.cs b/Tranga/Connectors/MangaDex.cs index afdb5e2..c221a67 100644 --- a/Tranga/Connectors/MangaDex.cs +++ b/Tranga/Connectors/MangaDex.cs @@ -49,6 +49,8 @@ public class MangaDex : Connector JsonObject manga = (JsonObject)mangeNode!; JsonObject attributes = manga["attributes"]!.AsObject(); + string publicationId = manga["id"]!.GetValue(); + 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(); @@ -76,11 +78,15 @@ public class MangaDex : Connector } string? posterId = null; + string? authorId = null; if (manga.ContainsKey("relationships") && manga["relationships"] is not null) { JsonArray relationships = manga["relationships"]!.AsArray(); posterId = relationships.FirstOrDefault(relationship => relationship!["type"]!.GetValue() == "cover_art")!["id"]!.GetValue(); + authorId = relationships.FirstOrDefault(relationship => relationship!["type"]!.GetValue() == "author")!["id"]!.GetValue(); } + string? coverUrl = GetCoverUrl(publicationId, posterId); + string? author = GetAuthor(authorId); Dictionary linksDict = new(); if (attributes.ContainsKey("links") && attributes["links"] is not null) @@ -102,12 +108,9 @@ public class MangaDex : Connector string status = attributes["status"]!.GetValue(); - string publicationId = manga["id"]!.GetValue(); - - string? coverUrl = GetCoverUrl(publicationId, posterId); - Publication pub = new ( title, + author, description, altTitlesDict, tags.ToArray(), @@ -230,6 +233,23 @@ public class MangaDex : Connector return coverUrl; } + private string? GetAuthor(string? authorId) + { + if (authorId is null) + return null; + + DownloadClient.RequestResult requestResult = + downloadClient.MakeRequest($"https://api.mangadex.org/author/{authorId}"); + if (requestResult.statusCode != HttpStatusCode.OK) + return null; + JsonObject? result = JsonSerializer.Deserialize(requestResult.result); + if (result is null) + return null; + + string author = result["data"]!["attributes"]!["name"]!.GetValue(); + return author; + } + public override void DownloadCover(Publication publication) { logger?.WriteLine(this.GetType().ToString(), $"Download cover {publication.sortName}"); diff --git a/Tranga/Publication.cs b/Tranga/Publication.cs index 60ebdf7..692f636 100644 --- a/Tranga/Publication.cs +++ b/Tranga/Publication.cs @@ -9,6 +9,7 @@ namespace Tranga; public readonly struct Publication { public string sortName { get; } + public string? author { get; } public Dictionary altTitles { get; } // ReSharper disable trice MemberCanBePrivate.Global, trust public string? description { get; } @@ -22,9 +23,10 @@ public readonly struct Publication public string publicationId { get; } public string internalId { get; } - public Publication(string sortName, string? description, Dictionary altTitles, string[] tags, string? posterUrl, Dictionary? links, int? year, string? originalLanguage, string status, string publicationId) + public Publication(string sortName, string? author, string? description, Dictionary altTitles, string[] tags, string? posterUrl, Dictionary? links, int? year, string? originalLanguage, string status, string publicationId) { this.sortName = sortName; + this.author = author; this.description = description; this.altTitles = altTitles; this.tags = tags;