2023-05-18 18:20:37 +02:00
|
|
|
|
using System.Globalization;
|
2023-05-19 18:11:14 +02:00
|
|
|
|
using System.Net;
|
2023-05-18 18:20:37 +02:00
|
|
|
|
using System.Text.Json;
|
2023-05-18 15:48:54 +02:00
|
|
|
|
using System.Text.Json.Nodes;
|
|
|
|
|
|
|
|
|
|
namespace Tranga.Connectors;
|
|
|
|
|
public class MangaDex : Connector
|
|
|
|
|
{
|
|
|
|
|
public override string name { get; }
|
2023-05-18 18:55:11 +02:00
|
|
|
|
private readonly DownloadClient _downloadClient = new (750);
|
2023-05-18 15:48:54 +02:00
|
|
|
|
|
2023-05-18 18:51:19 +02:00
|
|
|
|
public MangaDex(string downloadLocation) : base(downloadLocation)
|
2023-05-18 15:48:54 +02:00
|
|
|
|
{
|
2023-05-19 17:35:29 +02:00
|
|
|
|
name = "MangaDex";
|
2023-05-18 15:48:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-18 16:41:14 +02:00
|
|
|
|
public override Publication[] GetPublications(string publicationTitle = "")
|
2023-05-18 15:48:54 +02:00
|
|
|
|
{
|
|
|
|
|
const int limit = 100;
|
|
|
|
|
int offset = 0;
|
|
|
|
|
int total = int.MaxValue;
|
|
|
|
|
HashSet<Publication> publications = new();
|
|
|
|
|
while (offset < total)
|
|
|
|
|
{
|
2023-05-19 18:11:14 +02:00
|
|
|
|
DownloadClient.RequestResult requestResult =
|
|
|
|
|
_downloadClient.MakeRequest(
|
|
|
|
|
$"https://api.mangadex.org/manga?limit={limit}&title={publicationTitle}&offset={offset}");
|
|
|
|
|
if (requestResult.statusCode != HttpStatusCode.OK)
|
|
|
|
|
break;
|
2023-05-18 15:48:54 +02:00
|
|
|
|
JsonObject? result = JsonSerializer.Deserialize<JsonObject>(requestResult.result);
|
2023-05-18 18:20:04 +02:00
|
|
|
|
offset += limit;
|
2023-05-18 15:48:54 +02:00
|
|
|
|
if (result is null)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
total = result["total"]!.GetValue<int>();
|
|
|
|
|
JsonArray mangaInResult = result["data"]!.AsArray();
|
2023-05-18 18:55:11 +02:00
|
|
|
|
foreach (JsonNode? mangeNode in mangaInResult)
|
2023-05-18 15:48:54 +02:00
|
|
|
|
{
|
2023-05-18 18:55:11 +02:00
|
|
|
|
JsonObject manga = (JsonObject)mangeNode!;
|
|
|
|
|
JsonObject attributes = manga["attributes"]!.AsObject();
|
2023-05-18 15:48:54 +02:00
|
|
|
|
|
|
|
|
|
string title = attributes["title"]!.AsObject().ContainsKey("en") && attributes["title"]!["en"] is not null
|
|
|
|
|
? attributes["title"]!["en"]!.GetValue<string>()
|
|
|
|
|
: "";
|
|
|
|
|
|
|
|
|
|
string? description = attributes["description"]!.AsObject().ContainsKey("en") && attributes["description"]!["en"] is not null
|
|
|
|
|
? attributes["description"]!["en"]!.GetValue<string?>()
|
|
|
|
|
: null;
|
|
|
|
|
|
|
|
|
|
JsonArray altTitlesObject = attributes["altTitles"]!.AsArray();
|
|
|
|
|
string[,] altTitles = new string[altTitlesObject.Count, 2];
|
|
|
|
|
int titleIndex = 0;
|
2023-05-18 18:55:11 +02:00
|
|
|
|
foreach (JsonNode? altTitleNode in altTitlesObject)
|
2023-05-18 15:48:54 +02:00
|
|
|
|
{
|
2023-05-18 18:55:11 +02:00
|
|
|
|
JsonObject altTitleObject = (JsonObject)altTitleNode!;
|
|
|
|
|
string key = ((IDictionary<string, JsonNode?>)altTitleObject).Keys.ToArray()[0];
|
2023-05-18 15:48:54 +02:00
|
|
|
|
altTitles[titleIndex, 0] = key;
|
|
|
|
|
altTitles[titleIndex++, 1] = altTitleObject[key]!.GetValue<string>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JsonArray tagsObject = attributes["tags"]!.AsArray();
|
|
|
|
|
HashSet<string> tags = new();
|
2023-05-18 18:55:11 +02:00
|
|
|
|
foreach (JsonNode? tagNode in tagsObject)
|
2023-05-18 15:48:54 +02:00
|
|
|
|
{
|
2023-05-18 18:55:11 +02:00
|
|
|
|
JsonObject tagObject = (JsonObject)tagNode!;
|
|
|
|
|
if(tagObject["attributes"]!["name"]!.AsObject().ContainsKey("en"))
|
|
|
|
|
tags.Add(tagObject["attributes"]!["name"]!["en"]!.GetValue<string>());
|
2023-05-18 15:48:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-18 17:21:22 +02:00
|
|
|
|
string? poster = 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>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string[,]? links = null;
|
|
|
|
|
if (attributes.ContainsKey("links") && attributes["links"] is not null)
|
2023-05-18 15:48:54 +02:00
|
|
|
|
{
|
2023-05-18 17:21:22 +02:00
|
|
|
|
JsonObject linksObject = attributes["links"]!.AsObject();
|
|
|
|
|
links = new string[linksObject.Count, 2];
|
|
|
|
|
int linkIndex = 0;
|
|
|
|
|
foreach (string key in ((IDictionary<string, JsonNode?>)linksObject).Keys)
|
|
|
|
|
{
|
|
|
|
|
links[linkIndex, 0] = key;
|
|
|
|
|
links[linkIndex++, 1] = linksObject[key]!.GetValue<string>();
|
|
|
|
|
}
|
2023-05-18 15:48:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int? year = attributes.ContainsKey("year") && attributes["year"] is not null
|
|
|
|
|
? attributes["year"]!.GetValue<int?>()
|
|
|
|
|
: null;
|
|
|
|
|
|
|
|
|
|
string? originalLanguage = attributes.ContainsKey("originalLanguage") && attributes["originalLanguage"] is not null
|
|
|
|
|
? attributes["originalLanguage"]!.GetValue<string?>()
|
|
|
|
|
: null;
|
|
|
|
|
|
|
|
|
|
string status = attributes["status"]!.GetValue<string>();
|
|
|
|
|
|
|
|
|
|
Publication pub = new Publication(
|
|
|
|
|
title,
|
|
|
|
|
description,
|
|
|
|
|
altTitles,
|
|
|
|
|
tags.ToArray(),
|
|
|
|
|
poster,
|
|
|
|
|
links,
|
|
|
|
|
year,
|
|
|
|
|
originalLanguage,
|
|
|
|
|
status,
|
|
|
|
|
manga["id"]!.GetValue<string>()
|
|
|
|
|
);
|
|
|
|
|
publications.Add(pub);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return publications.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-18 18:19:04 +02:00
|
|
|
|
public override Chapter[] GetChapters(Publication publication, string language = "")
|
2023-05-18 15:48:54 +02:00
|
|
|
|
{
|
2023-05-18 16:04:03 +02:00
|
|
|
|
const int limit = 100;
|
|
|
|
|
int offset = 0;
|
|
|
|
|
string id = publication.downloadUrl;
|
|
|
|
|
int total = int.MaxValue;
|
|
|
|
|
List<Chapter> chapters = new();
|
|
|
|
|
while (offset < total)
|
|
|
|
|
{
|
|
|
|
|
DownloadClient.RequestResult requestResult =
|
2023-05-19 18:11:14 +02:00
|
|
|
|
_downloadClient.MakeRequest(
|
|
|
|
|
$"https://api.mangadex.org/manga/{id}/feed?limit={limit}&offset={offset}&translatedLanguage%5B%5D={language}");
|
|
|
|
|
if (requestResult.statusCode != HttpStatusCode.OK)
|
|
|
|
|
break;
|
2023-05-18 16:04:03 +02:00
|
|
|
|
JsonObject? result = JsonSerializer.Deserialize<JsonObject>(requestResult.result);
|
2023-05-18 18:20:04 +02:00
|
|
|
|
|
|
|
|
|
offset += limit;
|
2023-05-18 16:04:03 +02:00
|
|
|
|
if (result is null)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
total = result["total"]!.GetValue<int>();
|
|
|
|
|
JsonArray chaptersInResult = result["data"]!.AsArray();
|
2023-05-18 18:55:11 +02:00
|
|
|
|
foreach (JsonNode? jsonNode in chaptersInResult)
|
2023-05-18 16:04:03 +02:00
|
|
|
|
{
|
2023-05-18 18:55:11 +02:00
|
|
|
|
JsonObject chapter = (JsonObject)jsonNode!;
|
|
|
|
|
JsonObject attributes = chapter["attributes"]!.AsObject();
|
2023-05-18 17:22:02 +02:00
|
|
|
|
string chapterId = chapter["id"]!.GetValue<string>();
|
2023-05-18 16:21:21 +02:00
|
|
|
|
|
2023-05-18 16:04:03 +02:00
|
|
|
|
string? title = attributes.ContainsKey("title") && attributes["title"] is not null
|
|
|
|
|
? attributes["title"]!.GetValue<string>()
|
|
|
|
|
: null;
|
|
|
|
|
|
|
|
|
|
string? volume = attributes.ContainsKey("volume") && attributes["volume"] is not null
|
|
|
|
|
? attributes["volume"]!.GetValue<string>()
|
|
|
|
|
: null;
|
|
|
|
|
|
|
|
|
|
string? chapterNum = attributes.ContainsKey("chapter") && attributes["chapter"] is not null
|
|
|
|
|
? attributes["chapter"]!.GetValue<string>()
|
|
|
|
|
: null;
|
|
|
|
|
|
2023-05-19 19:32:47 +02:00
|
|
|
|
chapters.Add(new Chapter(title, volume, chapterNum, chapterId));
|
2023-05-18 16:04:03 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-18 18:20:37 +02:00
|
|
|
|
|
2023-05-18 18:55:11 +02:00
|
|
|
|
NumberFormatInfo chapterNumberFormatInfo = new()
|
2023-05-18 18:20:37 +02:00
|
|
|
|
{
|
|
|
|
|
NumberDecimalSeparator = "."
|
|
|
|
|
};
|
|
|
|
|
return chapters.OrderBy(chapter => Convert.ToSingle(chapter.chapterNumber, chapterNumberFormatInfo)).ToArray();
|
2023-05-18 15:48:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-18 16:21:02 +02:00
|
|
|
|
public override void DownloadChapter(Publication publication, Chapter chapter)
|
2023-05-18 15:48:54 +02:00
|
|
|
|
{
|
2023-05-18 16:21:54 +02:00
|
|
|
|
DownloadClient.RequestResult requestResult =
|
2023-05-18 17:18:41 +02:00
|
|
|
|
_downloadClient.MakeRequest($"https://api.mangadex.org/at-home/server/{chapter.url}?forcePort443=false'");
|
2023-05-19 18:11:14 +02:00
|
|
|
|
if (requestResult.statusCode != HttpStatusCode.OK)
|
|
|
|
|
return;
|
2023-05-18 16:21:54 +02:00
|
|
|
|
JsonObject? result = JsonSerializer.Deserialize<JsonObject>(requestResult.result);
|
|
|
|
|
if (result is null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
string baseUrl = result["baseUrl"]!.GetValue<string>();
|
2023-05-18 17:21:06 +02:00
|
|
|
|
string hash = result["chapter"]!["hash"]!.GetValue<string>();
|
|
|
|
|
JsonArray imageFileNames = result["chapter"]!["data"]!.AsArray();
|
|
|
|
|
HashSet<string> imageUrls = new();
|
2023-05-18 18:55:11 +02:00
|
|
|
|
foreach (JsonNode? image in imageFileNames)
|
2023-05-18 17:42:26 +02:00
|
|
|
|
imageUrls.Add($"{baseUrl}/data/{hash}/{image!.GetValue<string>()}");
|
2023-05-18 17:21:06 +02:00
|
|
|
|
|
2023-05-19 19:44:59 +02:00
|
|
|
|
DownloadChapterImages(imageUrls.ToArray(), Path.Join(downloadLocation, publication.folderName, chapter.fileName), this._downloadClient);
|
2023-05-18 15:48:54 +02:00
|
|
|
|
}
|
2023-05-18 19:56:06 +02:00
|
|
|
|
|
|
|
|
|
public override void DownloadCover(Publication publication)
|
|
|
|
|
{
|
2023-05-19 16:27:30 +02:00
|
|
|
|
string publicationPath = Path.Join(downloadLocation, publication.folderName);
|
2023-05-19 18:11:14 +02:00
|
|
|
|
Directory.CreateDirectory(publicationPath);
|
|
|
|
|
DirectoryInfo dirInfo = new (publicationPath);
|
2023-05-19 16:27:30 +02:00
|
|
|
|
foreach(FileInfo fileInfo in dirInfo.EnumerateFiles())
|
|
|
|
|
if (fileInfo.Name.Contains("cover."))
|
|
|
|
|
return;
|
2023-05-19 18:11:14 +02:00
|
|
|
|
|
|
|
|
|
DownloadClient.RequestResult requestResult =
|
|
|
|
|
_downloadClient.MakeRequest($"https://api.mangadex.org/cover/{publication.posterUrl}");
|
|
|
|
|
if (requestResult.statusCode != HttpStatusCode.OK)
|
|
|
|
|
return;
|
2023-05-18 19:56:06 +02:00
|
|
|
|
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);
|
2023-05-19 19:44:59 +02:00
|
|
|
|
DownloadImage(coverUrl, Path.Join(downloadLocation, publication.folderName, $"cover.{extension}"), this._downloadClient);
|
2023-05-18 19:56:06 +02:00
|
|
|
|
}
|
2023-05-18 15:48:54 +02:00
|
|
|
|
}
|