Changed Publication:

downloadUrl is now publicationId, internal to Connector
posterUrl is now a URL to the file, instead of an id
This commit is contained in:
glax 2023-05-22 16:45:55 +02:00
parent 972cba69ec
commit 9eb8ddbc40
3 changed files with 46 additions and 25 deletions

View File

@ -75,11 +75,11 @@ public class MangaDex : Connector
tags.Add(tagObject["attributes"]!["name"]!["en"]!.GetValue<string>());
}
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<string>() == "cover_art")!["id"]!.GetValue<string>();
posterId = relationships.FirstOrDefault(relationship => relationship!["type"]!.GetValue<string>() == "cover_art")!["id"]!.GetValue<string>();
}
Dictionary<string, string> linksDict = new();
@ -102,17 +102,21 @@ public class MangaDex : Connector
string status = attributes["status"]!.GetValue<string>();
Publication pub = new Publication(
string publicationId = manga["id"]!.GetValue<string>();
string? coverUrl = GetCoverUrl(publicationId, posterId);
Publication pub = new (
title,
description,
altTitlesDict,
tags.ToArray(),
poster,
coverUrl,
linksDict,
year,
originalLanguage,
status,
manga["id"]!.GetValue<string>()
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<JsonObject>(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<JsonObject>(requestResult.result);
if (result is null)
return null;
string fileName = result["data"]!["attributes"]!["fileName"]!.GetValue<string>();
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<JsonObject>(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>();
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];

View File

@ -9,7 +9,6 @@ namespace Tranga;
public readonly struct Publication
{
public string sortName { get; }
// ReSharper disable UnusedAutoPropertyAccessor.Global we need it, trust
public Dictionary<string,string> 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<string,string> altTitles, string[] tags, string? posterUrl, Dictionary<string,string>? links, int? year, string? originalLanguage, string status, string downloadUrl)
public Publication(string sortName, string? description, Dictionary<string,string> altTitles, string[] tags, string? posterUrl, Dictionary<string,string>? 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}"));

View File

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