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

@ -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}"));
}
}