diff --git a/Tranga/Connectors/MangaDex.cs b/Tranga/Connectors/MangaDex.cs index debe784..afdb5e2 100644 --- a/Tranga/Connectors/MangaDex.cs +++ b/Tranga/Connectors/MangaDex.cs @@ -75,11 +75,11 @@ public class MangaDex : Connector tags.Add(tagObject["attributes"]!["name"]!["en"]!.GetValue()); } - string? poster = null; + string? posterId = null; if (manga.ContainsKey("relationships") && manga["relationships"] is not null) { JsonArray relationships = manga["relationships"]!.AsArray(); - poster = relationships.FirstOrDefault(relationship => relationship!["type"]!.GetValue() == "cover_art")!["id"]!.GetValue(); + posterId = relationships.FirstOrDefault(relationship => relationship!["type"]!.GetValue() == "cover_art")!["id"]!.GetValue(); } Dictionary linksDict = new(); @@ -102,17 +102,21 @@ public class MangaDex : Connector string status = attributes["status"]!.GetValue(); - Publication pub = new Publication( + string publicationId = manga["id"]!.GetValue(); + + string? coverUrl = GetCoverUrl(publicationId, posterId); + + Publication pub = new ( title, description, altTitlesDict, tags.ToArray(), - poster, + coverUrl, linksDict, year, originalLanguage, status, - manga["id"]!.GetValue() + publicationId ); publications.Add(pub); //Add Publication (Manga) to result } @@ -134,7 +138,7 @@ public class MangaDex : Connector //Request next "Page" DownloadClient.RequestResult requestResult = downloadClient.MakeRequest( - $"https://api.mangadex.org/manga/{publication.downloadUrl}/feed?limit={limit}&offset={offset}&translatedLanguage%5B%5D={language}"); + $"https://api.mangadex.org/manga/{publication.publicationId}/feed?limit={limit}&offset={offset}&translatedLanguage%5B%5D={language}"); if (requestResult.statusCode != HttpStatusCode.OK) break; JsonObject? result = JsonSerializer.Deserialize(requestResult.result); @@ -203,6 +207,29 @@ public class MangaDex : Connector DownloadChapterImages(imageUrls.ToArray(), CreateFullFilepath(publication, chapter), downloadClient, logger, comicInfoPath); } + private string? GetCoverUrl(string publicationId, string? posterId) + { + if (posterId is null) + { + logger?.WriteLine(this.GetType().ToString(), $"No posterId"); + return null; + } + + //Request information where to download Cover + DownloadClient.RequestResult requestResult = + downloadClient.MakeRequest($"https://api.mangadex.org/cover/{posterId}"); + if (requestResult.statusCode != HttpStatusCode.OK) + return null; + JsonObject? result = JsonSerializer.Deserialize(requestResult.result); + if (result is null) + return null; + + string fileName = result["data"]!["attributes"]!["fileName"]!.GetValue(); + + string coverUrl = $"https://uploads.mangadex.org/covers/{publicationId}/{fileName}"; + return coverUrl; + } + public override void DownloadCover(Publication publication) { logger?.WriteLine(this.GetType().ToString(), $"Download cover {publication.sortName}"); @@ -218,19 +245,14 @@ public class MangaDex : Connector return; } - //Request information where to download Cover - DownloadClient.RequestResult requestResult = - downloadClient.MakeRequest($"https://api.mangadex.org/cover/{publication.posterUrl}"); - if (requestResult.statusCode != HttpStatusCode.OK) - return; - JsonObject? result = JsonSerializer.Deserialize(requestResult.result); - if (result is null) + if (publication.posterUrl is null) + { + logger?.WriteLine(this.GetType().ToString(), $"No posterurl in publication"); return; + } - string fileName = result["data"]!["attributes"]!["fileName"]!.GetValue(); + string coverUrl = publication.posterUrl; - string coverUrl = $"https://uploads.mangadex.org/covers/{publication.downloadUrl}/{fileName}"; - //Get file-extension (jpg, png) string[] split = coverUrl.Split('.'); string extension = split[^1]; diff --git a/Tranga/Publication.cs b/Tranga/Publication.cs index b378f60..60ebdf7 100644 --- a/Tranga/Publication.cs +++ b/Tranga/Publication.cs @@ -9,7 +9,6 @@ namespace Tranga; public readonly struct Publication { public string sortName { get; } - // ReSharper disable UnusedAutoPropertyAccessor.Global we need it, trust public Dictionary altTitles { get; } // ReSharper disable trice MemberCanBePrivate.Global, trust public string? description { get; } @@ -20,10 +19,10 @@ public readonly struct Publication public string? originalLanguage { get; } public string status { get; } public string folderName { get; } - public string downloadUrl { get; } - [JsonIgnore]public string internalId { get; } + 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 downloadUrl) + public Publication(string sortName, string? description, Dictionary altTitles, string[] tags, string? posterUrl, Dictionary? links, int? year, string? originalLanguage, string status, string publicationId) { this.sortName = sortName; this.description = description; @@ -34,7 +33,7 @@ public readonly struct Publication this.year = year; this.originalLanguage = originalLanguage; this.status = status; - this.downloadUrl = downloadUrl; + this.publicationId = publicationId; this.folderName = string.Concat(sortName.Split(Path.GetInvalidPathChars().Concat(Path.GetInvalidFileNameChars()).ToArray())); string onlyLowerAscii = this.sortName.ToLower().Where(Char.IsAscii).ToString()!; this.internalId = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{onlyLowerAscii}{this.year}")); diff --git a/Tranga/TaskManager.cs b/Tranga/TaskManager.cs index eb616fa..d324edc 100644 --- a/Tranga/TaskManager.cs +++ b/Tranga/TaskManager.cs @@ -163,7 +163,7 @@ public class TaskManager //Check if same task already exists if (!_allTasks.Any(trangaTask => trangaTask.task == task && trangaTask.connectorName == connector.name && - trangaTask.publication?.downloadUrl == publication?.downloadUrl)) + trangaTask.publication?.internalId == publication?.internalId)) { if(task != TrangaTask.Task.UpdatePublications) _chapterCollection.TryAdd((Publication)publication!, new List()); @@ -196,10 +196,10 @@ public class TaskManager { if(_allTasks.RemoveWhere(trangaTask => trangaTask.task == task && trangaTask.connectorName == connectorName && - trangaTask.publication?.downloadUrl == publication?.downloadUrl) > 0) - logger?.WriteLine(this.GetType().ToString(), $"Removed Task {task} {publication?.sortName} {publication?.downloadUrl}."); + trangaTask.publication?.internalId == publication?.internalId) > 0) + logger?.WriteLine(this.GetType().ToString(), $"Removed Task {task} {publication?.sortName} {publication?.internalId}."); else - logger?.WriteLine(this.GetType().ToString(), $"No Task {task} {publication?.sortName} {publication?.downloadUrl} could be found."); + logger?.WriteLine(this.GetType().ToString(), $"No Task {task} {publication?.sortName} {publication?.internalId} could be found."); } ExportData(); }