2
0

Added download cover functionality

This commit is contained in:
glax 2023-05-18 19:56:06 +02:00
parent f92bcebe04
commit c283cbd9a9
3 changed files with 22 additions and 2 deletions

View File

@ -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)
{

View File

@ -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)
{

View File

@ -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<JsonObject>(requestResult.result);
if (result is null)
return;
string fileName = result!["data"]!["attributes"]!["fileName"]!.GetValue<string>();
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}"));
}
}