mirror of
https://github.com/C9Glax/tranga.git
synced 2025-06-19 17:47:53 +02:00
MangaConnectors do not have to return an Object with 6 Parameters.
Job-Start Logic readable and optimized More robust Database design
This commit is contained in:
@ -1,8 +1,6 @@
|
||||
using System.Net;
|
||||
using System.Text.Json.Nodes;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Text.RegularExpressions;
|
||||
using API.MangaDownloadClients;
|
||||
using JsonSerializer = System.Text.Json.JsonSerializer;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace API.Schema.MangaConnectors;
|
||||
|
||||
@ -11,313 +9,327 @@ public class MangaDex : MangaConnector
|
||||
//https://api.mangadex.org/docs/3-enumerations/#language-codes--localization
|
||||
//https://en.wikipedia.org/wiki/List_of_ISO_639_language_codes
|
||||
//https://gist.github.com/Josantonius/b455e315bc7f790d14b136d61d9ae469
|
||||
public MangaDex() : base("MangaDex", ["en","pt","pt-br","it","de","ru","aa","ab","ae","af","ak","am","an","ar-ae","ar-bh","ar-dz","ar-eg","ar-iq","ar-jo","ar-kw","ar-lb","ar-ly","ar-ma","ar-om","ar-qa","ar-sa","ar-sy","ar-tn","ar-ye","ar","as","av","ay","az","ba","be","bg","bh","bi","bm","bn","bo","br","bs","ca","ce","ch","co","cr","cs","cu","cv","cy","da","de-at","de-ch","de-de","de-li","de-lu","div","dv","dz","ee","el","en-au","en-bz","en-ca","en-cb","en-gb","en-ie","en-jm","en-nz","en-ph","en-tt","en-us","en-za","en-zw","eo","es-ar","es-bo","es-cl","es-co","es-cr","es-do","es-ec","es-es","es-gt","es-hn","es-la","es-mx","es-ni","es-pa","es-pe","es-pr","es-py","es-sv","es-us","es-uy","es-ve","es","et","eu","fa","ff","fi","fj","fo","fr-be","fr-ca","fr-ch","fr-fr","fr-lu","fr-mc","fr","fy","ga","gd","gl","gn","gu","gv","ha","he","hi","ho","hr-ba","hr-hr","hr","ht","hu","hy","hz","ia","id","ie","ig","ii","ik","in","io","is","it-ch","it-it","iu","iw","ja","ja-ro","ji","jv","jw","ka","kg","ki","kj","kk","kl","km","kn","ko","ko-ro","kr","ks","ku","kv","kw","ky","kz","la","lb","lg","li","ln","lo","ls","lt","lu","lv","mg","mh","mi","mk","ml","mn","mo","mr","ms-bn","ms-my","ms","mt","my","na","nb","nd","ne","ng","nl-be","nl-nl","nl","nn","no","nr","ns","nv","ny","oc","oj","om","or","os","pa","pi","pl","ps","pt-pt","qu-bo","qu-ec","qu-pe","qu","rm","rn","ro","rw","sa","sb","sc","sd","se-fi","se-no","se-se","se","sg","sh","si","sk","sl","sm","sn","so","sq","sr-ba","sr-sp","sr","ss","st","su","sv-fi","sv-se","sv","sw","sx","syr","ta","te","tg","th","ti","tk","tl","tn","to","tr","ts","tt","tw","ty","ug","uk","ur","us","uz","ve","vi","vo","wa","wo","xh","yi","yo","za","zh-cn","zh-hk","zh-mo","zh-ro","zh-sg","zh-tw","zh","zu"], ["mangadex.org"], "https://mangadex.org/favicon.ico")
|
||||
public MangaDex() : base("MangaDex",
|
||||
["en","pt","pt-br","it","de","ru","aa","ab","ae","af","ak","am","an","ar-ae","ar-bh","ar-dz","ar-eg","ar-iq","ar-jo","ar-kw","ar-lb","ar-ly","ar-ma","ar-om","ar-qa","ar-sa","ar-sy","ar-tn","ar-ye","ar","as","av","ay","az","ba","be","bg","bh","bi","bm","bn","bo","br","bs","ca","ce","ch","co","cr","cs","cu","cv","cy","da","de-at","de-ch","de-de","de-li","de-lu","div","dv","dz","ee","el","en-au","en-bz","en-ca","en-cb","en-gb","en-ie","en-jm","en-nz","en-ph","en-tt","en-us","en-za","en-zw","eo","es-ar","es-bo","es-cl","es-co","es-cr","es-do","es-ec","es-es","es-gt","es-hn","es-la","es-mx","es-ni","es-pa","es-pe","es-pr","es-py","es-sv","es-us","es-uy","es-ve","es","et","eu","fa","ff","fi","fj","fo","fr-be","fr-ca","fr-ch","fr-fr","fr-lu","fr-mc","fr","fy","ga","gd","gl","gn","gu","gv","ha","he","hi","ho","hr-ba","hr-hr","hr","ht","hu","hy","hz","ia","id","ie","ig","ii","ik","in","io","is","it-ch","it-it","iu","iw","ja","ja-ro","ji","jv","jw","ka","kg","ki","kj","kk","kl","km","kn","ko","ko-ro","kr","ks","ku","kv","kw","ky","kz","la","lb","lg","li","ln","lo","ls","lt","lu","lv","mg","mh","mi","mk","ml","mn","mo","mr","ms-bn","ms-my","ms","mt","my","na","nb","nd","ne","ng","nl-be","nl-nl","nl","nn","no","nr","ns","nv","ny","oc","oj","om","or","os","pa","pi","pl","ps","pt-pt","qu-bo","qu-ec","qu-pe","qu","rm","rn","ro","rw","sa","sb","sc","sd","se-fi","se-no","se-se","se","sg","sh","si","sk","sl","sm","sn","so","sq","sr-ba","sr-sp","sr","ss","st","su","sv-fi","sv-se","sv","sw","sx","syr","ta","te","tg","th","ti","tk","tl","tn","to","tr","ts","tt","tw","ty","ug","uk","ur","us","uz","ve","vi","vo","wa","wo","xh","yi","yo","za","zh-cn","zh-hk","zh-mo","zh-ro","zh-sg","zh-tw","zh","zu"],
|
||||
["mangadex.org"],
|
||||
"https://mangadex.org/favicon.ico")
|
||||
{
|
||||
this.downloadClient = new HttpDownloadClient();
|
||||
}
|
||||
|
||||
public override (Manga, List<Author>?, List<MangaTag>?, List<Link>?, List<MangaAltTitle>?)[] GetManga(string publicationTitle = "")
|
||||
private const int Limit = 100;
|
||||
public override Manga[] SearchManga(string mangaSearchName)
|
||||
{
|
||||
const int limit = 100; //How many values we want returned at once
|
||||
int offset = 0; //"Page"
|
||||
int total = int.MaxValue; //How many total results are there, is updated on first request
|
||||
HashSet<(Manga, List<Author>?, List<MangaTag>?, List<Link>?, List<MangaAltTitle>?)> retManga = new();
|
||||
List<JsonNode> results = new();
|
||||
Log.Info($"Searching Manga: {mangaSearchName}");
|
||||
List<Manga> mangas = new ();
|
||||
|
||||
//Request all search-results
|
||||
while (offset < total) //As long as we haven't requested all "Pages"
|
||||
int offset = 0;
|
||||
int total = int.MaxValue;
|
||||
while(offset < total)
|
||||
{
|
||||
//Request next Page
|
||||
string requestUrl =
|
||||
$"https://api.mangadex.org/manga?limit={limit}&title={publicationTitle}&offset={offset}" +
|
||||
$"&includes[]=manga&includes[]=cover_art&includes[]=author&includes[]=artist&includes[]=tag";
|
||||
RequestResult requestResult = downloadClient.MakeRequest(requestUrl, RequestType.MangaInfo);
|
||||
if ((int)requestResult.statusCode < 200 || (int)requestResult.statusCode >= 300)
|
||||
{
|
||||
Log.Info($"{requestResult.statusCode}: {requestUrl}");
|
||||
break;
|
||||
}
|
||||
JsonObject? result = JsonSerializer.Deserialize<JsonObject>(requestResult.result);
|
||||
|
||||
offset += limit;
|
||||
if (result is null)
|
||||
{
|
||||
Log.Info($"result was null: {requestUrl}");
|
||||
break;
|
||||
}
|
||||
|
||||
if(result.ContainsKey("total"))
|
||||
total = result["total"]!.GetValue<int>(); //Update the total number of Publications
|
||||
else continue;
|
||||
$"https://api.mangadex.org/manga?limit={Limit}&offset={offset}&title={mangaSearchName}" +
|
||||
$"&contentRating%5B%5D=safe&contentRating%5B%5D=suggestive&contentRating%5B%5D=erotica" +
|
||||
$"&includes%5B%5D=manga&includes%5B%5D=cover_art&includes%5B%5D=author&includes%5B%5D=artist&includes%5B%5D=tag'";
|
||||
offset += Limit;
|
||||
|
||||
if (result.ContainsKey("data"))
|
||||
results.AddRange(result["data"]!.AsArray()!);//Manga-data-Array
|
||||
RequestResult result = downloadClient.MakeRequest(requestUrl, RequestType.MangaDexFeed);
|
||||
if ((int)result.statusCode < 200 || (int)result.statusCode >= 300)
|
||||
{
|
||||
Log.Error("Request failed");
|
||||
return [];
|
||||
}
|
||||
|
||||
using StreamReader sr = new (result.result);
|
||||
JObject jObject = JObject.Parse(sr.ReadToEnd());
|
||||
|
||||
if (jObject.Value<string>("result") != "ok")
|
||||
{
|
||||
JArray? errors = jObject["errors"] as JArray;
|
||||
Log.Error($"Request failed: {string.Join(',', errors?.Select(e => e.Value<string>("title")) ?? [])}");
|
||||
return [];
|
||||
}
|
||||
|
||||
total = jObject.Value<int>("total");
|
||||
|
||||
JArray? data = jObject.Value<JArray>("data");
|
||||
if (data is null)
|
||||
{
|
||||
Log.Error("Data was null");
|
||||
return [];
|
||||
}
|
||||
|
||||
mangas.AddRange(data.Select(ParseMangaFromJToken));
|
||||
}
|
||||
|
||||
foreach (JsonNode mangaNode in results)
|
||||
{
|
||||
if(MangaFromJsonObject(mangaNode.AsObject()) is { } manga)
|
||||
retManga.Add(manga); //Add Publication (Manga) to result
|
||||
}
|
||||
return retManga.ToArray();
|
||||
Log.Info($"Search {mangaSearchName} yielded {mangas.Count} results.");
|
||||
return mangas.ToArray();
|
||||
}
|
||||
|
||||
public override (Manga, List<Author>?, List<MangaTag>?, List<Link>?, List<MangaAltTitle>?)? GetMangaFromId(string publicationId)
|
||||
private static readonly Regex GetMangaIdFromUrl = new(@"https?:\/\/mangadex\.org\/title\/([a-z0-9-]+)\/?.*");
|
||||
public override Manga? GetMangaFromUrl(string url)
|
||||
{
|
||||
string url = $"https://api.mangadex.org/manga/{publicationId}" +
|
||||
$"?includes%5B%5D=manga&includes%5B%5D=cover_art&includes%5B%5D=author&includes%5B%5D=artist&includes%5B%5D=tag";
|
||||
RequestResult requestResult = downloadClient.MakeRequest(url, RequestType.MangaInfo);
|
||||
if ((int)requestResult.statusCode < 200 || (int)requestResult.statusCode >= 300)
|
||||
Log.Info($"Getting Manga: {url}");
|
||||
if (!UrlMatchesConnector(url))
|
||||
{
|
||||
Log.Info($"{requestResult.statusCode}: {url}");
|
||||
Log.Debug($"Url is not for Connector. {url}");
|
||||
return null;
|
||||
}
|
||||
JsonObject? result = JsonSerializer.Deserialize<JsonObject>(requestResult.result);
|
||||
if(result is not null)
|
||||
return MangaFromJsonObject(result["data"]!.AsObject());
|
||||
Log.Info($"result was null: {url}");
|
||||
return null;
|
||||
}
|
||||
|
||||
public override (Manga, List<Author>?, List<MangaTag>?, List<Link>?, List<MangaAltTitle>?)? GetMangaFromUrl(string url)
|
||||
{
|
||||
Regex idRex = new (@"https:\/\/mangadex.org\/title\/([A-z0-9-]*)\/.*");
|
||||
string id = idRex.Match(url).Groups[1].Value;
|
||||
Match match = GetMangaIdFromUrl.Match(url);
|
||||
if (!match.Success || !match.Groups[1].Success)
|
||||
{
|
||||
Log.Debug($"Url is not for Connector (Could not retrieve id). {url}");
|
||||
return null;
|
||||
}
|
||||
string id = match.Groups[1].Value;
|
||||
|
||||
return GetMangaFromId(id);
|
||||
}
|
||||
|
||||
private (Manga, List<Author>?, List<MangaTag>?, List<Link>?, List<MangaAltTitle>?)? MangaFromJsonObject(JsonObject manga)
|
||||
public override Manga? GetMangaFromId(string mangaIdOnSite)
|
||||
{
|
||||
if (!manga.TryGetPropertyValue("id", out JsonNode? idNode))
|
||||
Log.Info($"Getting Manga: {mangaIdOnSite}");
|
||||
string requestUrl =
|
||||
$"https://api.mangadex.org/manga/{mangaIdOnSite}" +
|
||||
$"?includes%5B%5D=manga&includes%5B%5D=cover_art&includes%5B%5D=author&includes%5B%5D=artist&includes%5B%5D=tag'";
|
||||
|
||||
RequestResult result = downloadClient.MakeRequest(requestUrl, RequestType.MangaDexFeed);
|
||||
if ((int)result.statusCode < 200 || (int)result.statusCode >= 300)
|
||||
{
|
||||
Log.Info("id was null");
|
||||
Log.Error("Request failed");
|
||||
return null;
|
||||
}
|
||||
string publicationId = idNode!.GetValue<string>();
|
||||
|
||||
if (!manga.TryGetPropertyValue("attributes", out JsonNode? attributesNode))
|
||||
{
|
||||
Log.Info("attributes was null");
|
||||
return null;
|
||||
}
|
||||
JsonObject attributes = attributesNode!.AsObject();
|
||||
|
||||
if (!attributes.TryGetPropertyValue("title", out JsonNode? titleNode))
|
||||
{
|
||||
Log.Info("title was null");
|
||||
return null;
|
||||
}
|
||||
string sortName = titleNode!.AsObject().ContainsKey("en") switch
|
||||
{
|
||||
true => titleNode.AsObject()["en"]!.GetValue<string>(),
|
||||
false => titleNode.AsObject().First().Value!.GetValue<string>()
|
||||
};
|
||||
|
||||
Dictionary<string, string> altTitlesDict = new();
|
||||
if (attributes.TryGetPropertyValue("altTitles", out JsonNode? altTitlesNode))
|
||||
{
|
||||
foreach (JsonNode? altTitleNode in altTitlesNode!.AsArray())
|
||||
{
|
||||
JsonObject altTitleNodeObject = altTitleNode!.AsObject();
|
||||
altTitlesDict.TryAdd(altTitleNodeObject.First().Key, altTitleNodeObject.First().Value!.GetValue<string>());
|
||||
}
|
||||
}
|
||||
List<MangaAltTitle> altTitles = altTitlesDict.Select(t => new MangaAltTitle(t.Key, t.Value)).ToList();
|
||||
using StreamReader sr = new (result.result);
|
||||
JObject jObject = JObject.Parse(sr.ReadToEnd());
|
||||
|
||||
if (!attributes.TryGetPropertyValue("description", out JsonNode? descriptionNode))
|
||||
if (jObject.Value<string>("result") != "ok")
|
||||
{
|
||||
Log.Info("description was null");
|
||||
return null;
|
||||
}
|
||||
string description = descriptionNode!.AsObject().ContainsKey("en") switch
|
||||
{
|
||||
true => descriptionNode.AsObject()["en"]!.GetValue<string>(),
|
||||
false => descriptionNode.AsObject().FirstOrDefault().Value?.GetValue<string>() ?? ""
|
||||
};
|
||||
|
||||
Dictionary<string, string> linksDict = new();
|
||||
if (attributes.TryGetPropertyValue("links", out JsonNode? linksNode) && linksNode is not null)
|
||||
foreach (KeyValuePair<string, JsonNode?> linkKv in linksNode!.AsObject())
|
||||
linksDict.TryAdd(linkKv.Key, linkKv.Value.GetValue<string>());
|
||||
List<Link> links = linksDict.Select(x => new Link(x.Key, x.Value)).ToList();
|
||||
|
||||
string? originalLanguage =
|
||||
attributes.TryGetPropertyValue("originalLanguage", out JsonNode? originalLanguageNode) switch
|
||||
{
|
||||
true => originalLanguageNode?.GetValue<string>(),
|
||||
false => null
|
||||
};
|
||||
|
||||
MangaReleaseStatus releaseStatus = MangaReleaseStatus.Unreleased;
|
||||
if (attributes.TryGetPropertyValue("status", out JsonNode? statusNode))
|
||||
{
|
||||
releaseStatus = statusNode?.GetValue<string>().ToLower() switch
|
||||
{
|
||||
"ongoing" => MangaReleaseStatus.Continuing,
|
||||
"completed" => MangaReleaseStatus.Completed,
|
||||
"hiatus" => MangaReleaseStatus.OnHiatus,
|
||||
"cancelled" => MangaReleaseStatus.Cancelled,
|
||||
_ => MangaReleaseStatus.Unreleased
|
||||
};
|
||||
}
|
||||
|
||||
uint year = attributes.TryGetPropertyValue("year", out JsonNode? yearNode) switch
|
||||
{
|
||||
true => yearNode?.GetValue<uint>()??0,
|
||||
false => 0
|
||||
};
|
||||
|
||||
HashSet<string> tags = new(128);
|
||||
if (attributes.TryGetPropertyValue("tags", out JsonNode? tagsNode))
|
||||
foreach (JsonNode? tagNode in tagsNode!.AsArray())
|
||||
tags.Add(tagNode!["attributes"]!["name"]!["en"]!.GetValue<string>());
|
||||
List<MangaTag> mangaTags = tags.Select(t => new MangaTag(t)).ToList();
|
||||
|
||||
if (!manga.TryGetPropertyValue("relationships", out JsonNode? relationshipsNode))
|
||||
{
|
||||
Log.Info("relationships was null");
|
||||
JArray? errors = jObject["errors"] as JArray;
|
||||
Log.Error($"Request failed: {string.Join(',', errors?.Select(e => e.Value<string>("title")) ?? [])}");
|
||||
return null;
|
||||
}
|
||||
|
||||
JsonNode? coverNode = relationshipsNode!.AsArray()
|
||||
.FirstOrDefault(rel => rel!["type"]!.GetValue<string>().Equals("cover_art"));
|
||||
if (coverNode is null)
|
||||
JObject? data = jObject["data"] as JObject;
|
||||
if (data is null)
|
||||
{
|
||||
Log.Info("coverNode was null");
|
||||
Log.Error("Data was null");
|
||||
return null;
|
||||
}
|
||||
string fileName = coverNode["attributes"]!["fileName"]!.GetValue<string>();
|
||||
string coverUrl = $"https://uploads.mangadex.org/covers/{publicationId}/{fileName}";
|
||||
|
||||
List<string> authorNames = new();
|
||||
JsonNode?[] authorNodes = relationshipsNode.AsArray()
|
||||
.Where(rel => rel!["type"]!.GetValue<string>().Equals("author") || rel!["type"]!.GetValue<string>().Equals("artist")).ToArray();
|
||||
foreach (JsonNode? authorNode in authorNodes)
|
||||
{
|
||||
string authorName = authorNode!["attributes"]!["name"]!.GetValue<string>();
|
||||
if(!authorNames.Contains(authorName))
|
||||
authorNames.Add(authorName);
|
||||
}
|
||||
List<Author> authors = authorNames.Select(a => new Author(a)).ToList();
|
||||
|
||||
Manga pub = new (publicationId, sortName, description, $"https://mangadex.org/title/{publicationId}", coverUrl, null, year,
|
||||
originalLanguage, releaseStatus, -1,
|
||||
this,
|
||||
authors,
|
||||
mangaTags,
|
||||
links,
|
||||
altTitles);
|
||||
|
||||
return (pub, authors, mangaTags, links, altTitles);
|
||||
Manga manga = ParseMangaFromJToken(data);
|
||||
return manga;
|
||||
}
|
||||
|
||||
public override Chapter[] GetChapters(Manga manga, string language="en")
|
||||
public override Chapter[] GetChapters(Manga manga, string? language = null)
|
||||
{
|
||||
const int limit = 100; //How many values we want returned at once
|
||||
int offset = 0; //"Page"
|
||||
int total = int.MaxValue; //How many total results are there, is updated on first request
|
||||
List<Chapter> chapters = new();
|
||||
//As long as we haven't requested all "Pages"
|
||||
while (offset < total)
|
||||
Log.Info($"Getting Chapters: {manga.IdOnConnectorSite}");
|
||||
List<Chapter> chapters = new ();
|
||||
|
||||
int offset = 0;
|
||||
int total = int.MaxValue;
|
||||
while(offset < total)
|
||||
{
|
||||
//Request next "Page"
|
||||
string requestUrl = $"https://api.mangadex.org/manga/{manga.IdOnConnectorSite}/feed?limit={limit}&offset={offset}&translatedLanguage%5B%5D={language}" +
|
||||
$"&contentRating%5B%5D=safe&contentRating%5B%5D=suggestive&contentRating%5B%5D=erotica&contentRating%5B%5D=pornographic";
|
||||
RequestResult requestResult = downloadClient.MakeRequest(requestUrl, RequestType.MangaDexFeed);
|
||||
if ((int)requestResult.statusCode < 200 || (int)requestResult.statusCode >= 300)
|
||||
{
|
||||
Log.Info($"{requestResult.statusCode}: {requestUrl}");
|
||||
break;
|
||||
}
|
||||
JsonObject? result = JsonSerializer.Deserialize<JsonObject>(requestResult.result);
|
||||
|
||||
offset += limit;
|
||||
if (result is null)
|
||||
{
|
||||
Log.Info($"result was null: {requestUrl}");
|
||||
break;
|
||||
}
|
||||
|
||||
total = result["total"]!.GetValue<int>();
|
||||
JsonArray chaptersInResult = result["data"]!.AsArray();
|
||||
//Loop through all Chapters in result and extract information from JSON
|
||||
foreach (JsonNode? jsonNode in chaptersInResult)
|
||||
{
|
||||
JsonObject chapter = (JsonObject)jsonNode!;
|
||||
JsonObject attributes = chapter["attributes"]!.AsObject();
|
||||
|
||||
string chapterId = chapter["id"]!.GetValue<string>();
|
||||
string url = $"https://mangadex.org/chapter/{chapterId}";
|
||||
|
||||
string? title = attributes.ContainsKey("title") && attributes["title"] is not null
|
||||
? attributes["title"]!.GetValue<string>()
|
||||
: null;
|
||||
|
||||
int? volume = attributes.ContainsKey("volume") && attributes["volume"] is not null
|
||||
? int.Parse(attributes["volume"]!.GetValue<string>())
|
||||
: null;
|
||||
|
||||
string? chapterNumStr = attributes.ContainsKey("chapter") && attributes["chapter"] is not null
|
||||
? attributes["chapter"]!.GetValue<string>()
|
||||
: null;
|
||||
|
||||
string chapterNumber = new(chapterNumStr);
|
||||
|
||||
|
||||
if (attributes.ContainsKey("pages") && attributes["pages"] is not null &&
|
||||
attributes["pages"]!.GetValue<int>() < 1)
|
||||
{
|
||||
Log.Info($"No pages: {chapterId}");
|
||||
continue;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Chapter newChapter = new(manga, url, chapterNumber, volume, title);
|
||||
if(!chapters.Contains(newChapter))
|
||||
chapters.Add(newChapter);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Debug(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
string requestUrl =
|
||||
$"https://api.mangadex.org/manga/{manga.IdOnConnectorSite}/feed?limit={Limit}&offset={offset}&" +
|
||||
$"translatedLanguage%5B%5D={language}&" +
|
||||
$"contentRating%5B%5D=safe&contentRating%5B%5D=suggestive&contentRating%5B%5D=erotica&includeFutureUpdates=0&includes%5B%5D=";
|
||||
offset += Limit;
|
||||
|
||||
//Return Chapters ordered by Chapter-Number
|
||||
RequestResult result = downloadClient.MakeRequest(requestUrl, RequestType.MangaDexFeed);
|
||||
if ((int)result.statusCode < 200 || (int)result.statusCode >= 300)
|
||||
{
|
||||
Log.Error("Request failed");
|
||||
return [];
|
||||
}
|
||||
|
||||
using StreamReader sr = new (result.result);
|
||||
JObject jObject = JObject.Parse(sr.ReadToEnd());
|
||||
|
||||
if (jObject.Value<string>("result") != "ok")
|
||||
{
|
||||
JArray? errors = jObject["errors"] as JArray;
|
||||
Log.Error($"Request failed: {string.Join(',', errors?.Select(e => e.Value<string>("title")) ?? [])}");
|
||||
return [];
|
||||
}
|
||||
|
||||
total = jObject.Value<int>("total");
|
||||
|
||||
JArray? data = jObject.Value<JArray>("data");
|
||||
if (data is null)
|
||||
{
|
||||
Log.Error("Data was null");
|
||||
return [];
|
||||
}
|
||||
|
||||
chapters.AddRange(data.Select(d => ParseChapterFromJToken(manga, d)));
|
||||
}
|
||||
|
||||
Log.Info($"Request for chapters for {manga.Name} yielded {chapters.Count} results.");
|
||||
return chapters.ToArray();
|
||||
}
|
||||
|
||||
private static readonly Regex GetChapterIdFromUrl = new(@"https?:\/\/mangadex\.org\/chapter\/([a-z0-9-]+)\/?.*");
|
||||
internal override string[] GetChapterImageUrls(Chapter chapter)
|
||||
{//Request URLs for Chapter-Images
|
||||
Match m = Regex.Match(chapter.Url, @"https?:\/\/mangadex.org\/chapter\/([0-9\-a-z]+)");
|
||||
if (!m.Success)
|
||||
{
|
||||
Log.Info($"Getting Chapter Image-Urls: {chapter.Url}");
|
||||
if (!UrlMatchesConnector(chapter.Url))
|
||||
{
|
||||
Log.Error($"Could not parse Chapter ID: {chapter.Url}");
|
||||
Log.Debug($"Url is not for Connector. {chapter.Url}");
|
||||
return [];
|
||||
}
|
||||
|
||||
string url = $"https://api.mangadex.org/at-home/server/{m.Groups[1].Value}?forcePort443=false";
|
||||
RequestResult requestResult =
|
||||
downloadClient.MakeRequest(url, RequestType.MangaDexImage);
|
||||
if ((int)requestResult.statusCode < 200 || (int)requestResult.statusCode >= 300)
|
||||
Match match = GetChapterIdFromUrl.Match(chapter.Url);
|
||||
if (!match.Success || !match.Groups[1].Success)
|
||||
{
|
||||
Log.Info($"{requestResult.statusCode}: {url}");
|
||||
Log.Debug($"Url is not for Connector (Could not retrieve id). {chapter.Url}");
|
||||
return [];
|
||||
}
|
||||
JsonObject? result = JsonSerializer.Deserialize<JsonObject>(requestResult.result);
|
||||
if (result is null)
|
||||
|
||||
string id = match.Groups[1].Value;
|
||||
string requestUrl = $"https://api.mangadex.org/at-home/server/{id}";
|
||||
|
||||
RequestResult result = downloadClient.MakeRequest(requestUrl, RequestType.Default);
|
||||
if ((int)result.statusCode < 200 || (int)result.statusCode >= 300)
|
||||
{
|
||||
Log.Info($"Result was null: {url}");
|
||||
Log.Error("Request failed");
|
||||
return [];
|
||||
}
|
||||
string baseUrl = result["baseUrl"]!.GetValue<string>();
|
||||
string hash = result["chapter"]!["hash"]!.GetValue<string>();
|
||||
JsonArray imageFileNames = result["chapter"]!["data"]!.AsArray();
|
||||
//Loop through all imageNames and construct urls (imageUrl)
|
||||
List<string> imageUrls = new();
|
||||
foreach (JsonNode? image in imageFileNames)
|
||||
imageUrls.Add($"{baseUrl}/data/{hash}/{image!.GetValue<string>()}");
|
||||
return imageUrls.ToArray();
|
||||
|
||||
using StreamReader sr = new (result.result);
|
||||
JObject jObject = JObject.Parse(sr.ReadToEnd());
|
||||
|
||||
if (jObject.Value<string>("result") != "ok")
|
||||
{
|
||||
JArray? errors = jObject["errors"] as JArray;
|
||||
Log.Error($"Request failed: {string.Join(',', errors?.Select(e => e.Value<string>("title")) ?? [])}");
|
||||
return [];
|
||||
}
|
||||
|
||||
string? baseUrl = jObject.Value<string>("baseUrl");
|
||||
JToken? chapterToken = jObject["chapter"];
|
||||
string? hash = chapterToken?.Value<string>("hash");
|
||||
JArray? data = chapterToken?["data"] as JArray;
|
||||
|
||||
if (baseUrl is null || hash is null || data is null)
|
||||
{
|
||||
Log.Error("Data was null");
|
||||
return [];
|
||||
}
|
||||
|
||||
IEnumerable<string> urls = data.Select(t => $"{baseUrl}/data/{hash}/{t.Value<string>()}");
|
||||
|
||||
return urls.ToArray();
|
||||
}
|
||||
|
||||
private Manga ParseMangaFromJToken(JToken jToken)
|
||||
{
|
||||
string? id = jToken.Value<string>("id");
|
||||
|
||||
JObject? attributes = jToken["attributes"] as JObject;
|
||||
string? name = attributes?["title"]?.Value<string>("en");
|
||||
string? description = attributes?["description"]?.Value<string>("en");
|
||||
string? status = attributes?["status"]?.Value<string>();
|
||||
uint? year = attributes?["year"]?.Value<uint>();
|
||||
string? originalLanguage = attributes?["originalLanguage"]?.Value<string>();
|
||||
JArray? altTitlesJArray = attributes?["altTitles"] as JArray;
|
||||
JArray? tagsJArray = attributes?["tags"] as JArray;
|
||||
|
||||
JArray? relationships = jToken["relationships"] as JArray;
|
||||
string? coverFileName =
|
||||
relationships?.FirstOrDefault(r => r["type"]?.Value<string>() == "cover_art")?["attributes"]?.Value<string>("fileName");
|
||||
|
||||
if (id is null || attributes is null || name is null || description is null || status is null ||
|
||||
altTitlesJArray is null || tagsJArray is null || relationships is null || coverFileName is null)
|
||||
throw new Exception("jToken was not in expected format");
|
||||
|
||||
List<Link> links = attributes["links"]?
|
||||
.ToObject<Dictionary<string,string>>()?
|
||||
.Select(kv =>
|
||||
{
|
||||
//https://api.mangadex.org/docs/3-enumerations/#manga-links-data
|
||||
string url = kv.Key switch
|
||||
{
|
||||
"al" => $"https://anilist.co/manga/{kv.Value}",
|
||||
"ap" => $"https://www.anime-planet.com/manga/{kv.Value}",
|
||||
"bw" => $"https://bookwalker.jp/{kv.Value}",
|
||||
"mu" => $"https://www.mangaupdates.com/series.html?id={kv.Value}",
|
||||
"nu" => $"https://www.novelupdates.com/series/{kv.Value}",
|
||||
"mal" => $"https://myanimelist.net/manga/{kv.Value}",
|
||||
_ => kv.Value
|
||||
};
|
||||
string key = kv.Key switch
|
||||
{
|
||||
"al" => "AniList",
|
||||
"ap" => "Anime Planet",
|
||||
"bw" => "BookWalker",
|
||||
"mu" => "Manga Updates",
|
||||
"nu" => "Novel Updates",
|
||||
"kt" => "Kitsu.io",
|
||||
"amz" => "Amazon",
|
||||
"ebj" => "eBookJapan",
|
||||
"mal" => "MyAnimeList",
|
||||
"cdj" => "CDJapan",
|
||||
_ => kv.Key
|
||||
};
|
||||
return new Link(key, url);
|
||||
}).ToList()!;
|
||||
|
||||
List<MangaAltTitle> altTitles = altTitlesJArray
|
||||
.Select(t =>
|
||||
{
|
||||
JObject? j = t as JObject;
|
||||
JProperty? p = j?.Properties().First();
|
||||
if (p is null)
|
||||
return null;
|
||||
return new MangaAltTitle(p.Name, p.Value.ToString());
|
||||
}).Where(x => x is not null).ToList()!;
|
||||
|
||||
List<MangaTag> tags = tagsJArray
|
||||
.Where(t => t.Value<string>("type") == "tag")
|
||||
.Select(t => t["attributes"]?["name"]?.Value<string>("en"))
|
||||
.Select(str => str is not null ? new MangaTag(str) : null)
|
||||
.Where(x => x is not null).ToList()!;
|
||||
|
||||
List<Author> authors = relationships
|
||||
.Where(r => r["type"]?.Value<string>() == "author")
|
||||
.Select(t => t["attributes"]?.Value<string>("name"))
|
||||
.Select(str => str is not null ? new Author(str) : null)
|
||||
.Where(x => x is not null).ToList()!;
|
||||
|
||||
|
||||
MangaReleaseStatus releaseStatus = status switch
|
||||
{
|
||||
"completed" => MangaReleaseStatus.Completed,
|
||||
"ongoing" => MangaReleaseStatus.Continuing,
|
||||
"cancelled" => MangaReleaseStatus.Cancelled,
|
||||
"hiatus" => MangaReleaseStatus.OnHiatus,
|
||||
_ => MangaReleaseStatus.Unreleased
|
||||
};
|
||||
string websiteUrl = $"https://mangadex.org/title/{id}";
|
||||
string coverUrl = $"https://uploads.mangadex.org/covers/{id}/{coverFileName}";
|
||||
|
||||
return new Manga(id, name, description, websiteUrl, coverUrl, releaseStatus, this,
|
||||
authors, tags, links,altTitles,
|
||||
null, 0f, year, originalLanguage);
|
||||
}
|
||||
|
||||
private Chapter ParseChapterFromJToken(Manga parentManga, JToken jToken)
|
||||
{
|
||||
string? id = jToken.Value<string>("id");
|
||||
JToken? attributes = jToken["attributes"];
|
||||
string? chapter = attributes?.Value<string>("chapter");
|
||||
string? volumeStr = attributes?.Value<string>("volume");
|
||||
int? volume = null;
|
||||
string? title = attributes?.Value<string>("title");
|
||||
|
||||
if(id is null || chapter is null)
|
||||
throw new Exception("jToken was not in expected format");
|
||||
if(volumeStr is not null)
|
||||
volume = int.Parse(volumeStr);
|
||||
|
||||
string url = $"https://mangadex.org/chapter/{id}";
|
||||
return new Chapter(parentManga, url, chapter, volume, title);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user