From c283cbd9a9d8dbd5e68da0f1e6c16aa35bcc0d15 Mon Sep 17 00:00:00 2001 From: glax <--local> Date: Thu, 18 May 2023 19:56:06 +0200 Subject: [PATCH] Added download cover functionality --- Tranga-CLI/Tranga_Cli.cs | 3 +++ Tranga/Connector.cs | 1 + Tranga/Connectors/MangaDex.cs | 20 ++++++++++++++++++-- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/Tranga-CLI/Tranga_Cli.cs b/Tranga-CLI/Tranga_Cli.cs index 4880347..bd41fdc 100644 --- a/Tranga-CLI/Tranga_Cli.cs +++ b/Tranga-CLI/Tranga_Cli.cs @@ -29,6 +29,9 @@ public static class Tranga_Cli Chapter[] allChapteres = connector.GetChapters(selectedPub, "en"); Chapter[] downloadChapters = SelectChapters(allChapteres); + + if(downloadChapters.Length > 0) + connector.DownloadCover(selectedPub); foreach (Chapter chapter in downloadChapters) { diff --git a/Tranga/Connector.cs b/Tranga/Connector.cs index 36d43fe..9bf774f 100644 --- a/Tranga/Connector.cs +++ b/Tranga/Connector.cs @@ -16,6 +16,7 @@ public abstract class Connector public abstract Chapter[] GetChapters(Publication publication, string language = ""); public abstract void DownloadChapter(Publication publication, Chapter chapter); //where to? protected abstract void DownloadImage(string url, string savePath); + public abstract void DownloadCover(Publication publication); internal void DownloadChapter(string[] imageUrls, string saveArchiveFilePath) { diff --git a/Tranga/Connectors/MangaDex.cs b/Tranga/Connectors/MangaDex.cs index 6dd6aa7..b904e2d 100644 --- a/Tranga/Connectors/MangaDex.cs +++ b/Tranga/Connectors/MangaDex.cs @@ -3,8 +3,6 @@ using System.Text.Json; using System.Text.Json.Nodes; namespace Tranga.Connectors; -//TODO Download covers: https://api.mangadex.org/docs/retrieving-covers/ https://api.mangadex.org/docs/swagger.html#/ - public class MangaDex : Connector { public override string name { get; } @@ -190,4 +188,22 @@ public class MangaDex : Connector requestResult.result.ReadExactly(buffer, 0, buffer.Length); File.WriteAllBytes(savePath, buffer); } + + public override void DownloadCover(Publication publication) + { + DownloadClient.RequestResult requestResult = _downloadClient.MakeRequest($"https://uploads.mangadex.org/cover/{publication.posterUrl}"); + JsonObject? result = JsonSerializer.Deserialize(requestResult.result); + if (result is null) + return; + + string fileName = result!["data"]!["attributes"]!["fileName"]!.GetValue(); + + string coverUrl = $"https://uploads.mangadex.org/covers/{publication.downloadUrl}/{fileName}"; + string[] split = coverUrl.Split('.'); + string extension = split[split.Length - 1]; + + string outFolderPath = Path.Join(downloadLocation, publication.folderName); + Directory.CreateDirectory(outFolderPath); + DownloadImage(coverUrl, Path.Join(downloadLocation, publication.folderName, $"cover.{extension}")); + } } \ No newline at end of file