2024-12-14 21:53:29 +01:00
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using API.MangaDownloadClients;
|
|
|
|
|
using HtmlAgilityPack;
|
|
|
|
|
|
|
|
|
|
namespace API.Schema.MangaConnectors;
|
|
|
|
|
|
|
|
|
|
public class MangaLife : MangaConnector
|
|
|
|
|
{
|
|
|
|
|
public MangaLife() : base("Manga4Life", ["en"], ["manga4life.com"])
|
|
|
|
|
{
|
|
|
|
|
this.downloadClient = new ChromiumDownloadClient();
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-15 23:00:35 +01:00
|
|
|
|
public override (Manga, Author[], MangaTag[], Link[], MangaAltTitle[])[] GetManga(string publicationTitle = "")
|
2024-12-14 21:53:29 +01:00
|
|
|
|
{
|
|
|
|
|
string sanitizedTitle = WebUtility.UrlEncode(publicationTitle);
|
|
|
|
|
string requestUrl = $"https://manga4life.com/search/?name={sanitizedTitle}";
|
|
|
|
|
RequestResult requestResult =
|
|
|
|
|
downloadClient.MakeRequest(requestUrl, RequestType.Default);
|
|
|
|
|
if ((int)requestResult.statusCode < 200 || (int)requestResult.statusCode >= 300)
|
2024-12-15 23:00:35 +01:00
|
|
|
|
return [];
|
2024-12-14 21:53:29 +01:00
|
|
|
|
|
|
|
|
|
if (requestResult.htmlDocument is null)
|
2024-12-15 23:00:35 +01:00
|
|
|
|
return [];
|
|
|
|
|
(Manga, Author[], MangaTag[], Link[], MangaAltTitle[])[] publications = ParsePublicationsFromHtml(requestResult.htmlDocument);
|
2024-12-14 21:53:29 +01:00
|
|
|
|
return publications;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-15 23:00:35 +01:00
|
|
|
|
public override (Manga, Author[], MangaTag[], Link[], MangaAltTitle[])? GetMangaFromId(string publicationId)
|
2024-12-14 21:53:29 +01:00
|
|
|
|
{
|
|
|
|
|
return GetMangaFromUrl($"https://manga4life.com/manga/{publicationId}");
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-15 23:00:35 +01:00
|
|
|
|
public override (Manga, Author[], MangaTag[], Link[], MangaAltTitle[])? GetMangaFromUrl(string url)
|
2024-12-14 21:53:29 +01:00
|
|
|
|
{
|
|
|
|
|
Regex publicationIdRex = new(@"https:\/\/(www\.)?manga4life.com\/manga\/(.*)(\/.*)*");
|
|
|
|
|
string publicationId = publicationIdRex.Match(url).Groups[2].Value;
|
|
|
|
|
|
|
|
|
|
RequestResult requestResult = this.downloadClient.MakeRequest(url, RequestType.MangaInfo);
|
|
|
|
|
if(requestResult.htmlDocument is not null)
|
|
|
|
|
return ParseSinglePublicationFromHtml(requestResult.htmlDocument, publicationId, url);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-15 23:00:35 +01:00
|
|
|
|
private (Manga, Author[], MangaTag[], Link[], MangaAltTitle[])[] ParsePublicationsFromHtml(HtmlDocument document)
|
2024-12-14 21:53:29 +01:00
|
|
|
|
{
|
|
|
|
|
HtmlNode resultsNode = document.DocumentNode.SelectSingleNode("//div[@class='BoxBody']/div[last()]/div[1]/div");
|
|
|
|
|
if (resultsNode.Descendants("div").Count() == 1 && resultsNode.Descendants("div").First().HasClass("NoResults"))
|
|
|
|
|
{
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-15 23:00:35 +01:00
|
|
|
|
List<(Manga, Author[], MangaTag[], Link[], MangaAltTitle[])> ret = new();
|
2024-12-14 21:53:29 +01:00
|
|
|
|
|
|
|
|
|
foreach (HtmlNode resultNode in resultsNode.SelectNodes("div"))
|
|
|
|
|
{
|
|
|
|
|
string url = resultNode.Descendants().First(d => d.HasClass("SeriesName")).GetAttributeValue("href", "");
|
2024-12-15 23:00:35 +01:00
|
|
|
|
(Manga, Author[], MangaTag[], Link[], MangaAltTitle[])? manga = GetMangaFromUrl($"https://manga4life.com{url}");
|
|
|
|
|
if (manga is { } x)
|
|
|
|
|
ret.Add(x);
|
2024-12-14 21:53:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-12-15 23:00:35 +01:00
|
|
|
|
private (Manga, Author[], MangaTag[], Link[], MangaAltTitle[]) ParseSinglePublicationFromHtml(HtmlDocument document, string publicationId, string websiteUrl)
|
2024-12-14 21:53:29 +01:00
|
|
|
|
{
|
|
|
|
|
string originalLanguage = "", status = "";
|
|
|
|
|
Dictionary<string, string> altTitles = new(), links = new();
|
|
|
|
|
HashSet<string> tags = new();
|
|
|
|
|
MangaReleaseStatus releaseStatus = MangaReleaseStatus.Unreleased;
|
|
|
|
|
|
|
|
|
|
HtmlNode posterNode = document.DocumentNode.SelectSingleNode("//div[@class='BoxBody']//div[@class='row']//img");
|
2024-12-15 23:00:35 +01:00
|
|
|
|
string coverUrl = posterNode.GetAttributeValue("src", "");
|
2024-12-14 21:53:29 +01:00
|
|
|
|
|
|
|
|
|
HtmlNode titleNode = document.DocumentNode.SelectSingleNode("//div[@class='BoxBody']//div[@class='row']//h1");
|
|
|
|
|
string sortName = titleNode.InnerText;
|
|
|
|
|
|
|
|
|
|
HtmlNode[] authorsNodes = document.DocumentNode
|
|
|
|
|
.SelectNodes("//div[@class='BoxBody']//div[@class='row']//span[text()='Author(s):']/..").Descendants("a")
|
|
|
|
|
.ToArray();
|
2024-12-15 23:00:35 +01:00
|
|
|
|
List<string> authorNames = new();
|
2024-12-14 21:53:29 +01:00
|
|
|
|
foreach (HtmlNode authorNode in authorsNodes)
|
2024-12-15 23:00:35 +01:00
|
|
|
|
authorNames.Add(authorNode.InnerText);
|
|
|
|
|
Author[] authors = authorNames.Select(a => new Author(a)).ToArray();
|
2024-12-14 21:53:29 +01:00
|
|
|
|
|
|
|
|
|
HtmlNode[] genreNodes = document.DocumentNode
|
|
|
|
|
.SelectNodes("//div[@class='BoxBody']//div[@class='row']//span[text()='Genre(s):']/..").Descendants("a")
|
|
|
|
|
.ToArray();
|
|
|
|
|
foreach (HtmlNode genreNode in genreNodes)
|
|
|
|
|
tags.Add(genreNode.InnerText);
|
2024-12-15 23:00:35 +01:00
|
|
|
|
MangaTag[] mangaTags = tags.Select(t => new MangaTag(t)).ToArray();
|
2024-12-14 21:53:29 +01:00
|
|
|
|
|
|
|
|
|
HtmlNode yearNode = document.DocumentNode
|
|
|
|
|
.SelectNodes("//div[@class='BoxBody']//div[@class='row']//span[text()='Released:']/..").Descendants("a")
|
|
|
|
|
.First();
|
2024-12-15 23:00:35 +01:00
|
|
|
|
uint year = uint.Parse(yearNode.InnerText);
|
2024-12-14 21:53:29 +01:00
|
|
|
|
|
|
|
|
|
HtmlNode[] statusNodes = document.DocumentNode
|
|
|
|
|
.SelectNodes("//div[@class='BoxBody']//div[@class='row']//span[text()='Status:']/..").Descendants("a")
|
|
|
|
|
.ToArray();
|
|
|
|
|
foreach (HtmlNode statusNode in statusNodes)
|
|
|
|
|
if (statusNode.InnerText.Contains("publish", StringComparison.CurrentCultureIgnoreCase))
|
|
|
|
|
status = statusNode.InnerText.Split(' ')[0];
|
|
|
|
|
switch (status.ToLower())
|
|
|
|
|
{
|
|
|
|
|
case "cancelled": releaseStatus = MangaReleaseStatus.Cancelled; break;
|
|
|
|
|
case "hiatus": releaseStatus = MangaReleaseStatus.OnHiatus; break;
|
|
|
|
|
case "discontinued": releaseStatus = MangaReleaseStatus.Cancelled; break;
|
|
|
|
|
case "complete": releaseStatus = MangaReleaseStatus.Completed; break;
|
|
|
|
|
case "ongoing": releaseStatus = MangaReleaseStatus.Continuing; break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HtmlNode descriptionNode = document.DocumentNode
|
|
|
|
|
.SelectNodes("//div[@class='BoxBody']//div[@class='row']//span[text()='Description:']/..")
|
|
|
|
|
.Descendants("div").First();
|
|
|
|
|
string description = descriptionNode.InnerText;
|
|
|
|
|
|
2024-12-15 23:00:35 +01:00
|
|
|
|
Manga manga = new (publicationId, sortName, description, websiteUrl, coverUrl, null, year,
|
|
|
|
|
originalLanguage, releaseStatus, -1, null, null,
|
2024-12-16 19:25:22 +01:00
|
|
|
|
this,
|
|
|
|
|
authors,
|
|
|
|
|
mangaTags,
|
2024-12-15 23:00:35 +01:00
|
|
|
|
[],
|
|
|
|
|
[]);
|
|
|
|
|
|
|
|
|
|
return (manga, authors, mangaTags, [], []);
|
2024-12-14 21:53:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override Chapter[] GetChapters(Manga manga, string language="en")
|
|
|
|
|
{
|
|
|
|
|
RequestResult result = downloadClient.MakeRequest($"https://manga4life.com/manga/{manga.MangaId}", RequestType.Default, clickButton:"[class*='ShowAllChapters']");
|
|
|
|
|
if ((int)result.statusCode < 200 || (int)result.statusCode >= 300 || result.htmlDocument is null)
|
|
|
|
|
{
|
|
|
|
|
return Array.Empty<Chapter>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HtmlNodeCollection chapterNodes = result.htmlDocument.DocumentNode.SelectNodes(
|
|
|
|
|
"//a[contains(concat(' ',normalize-space(@class),' '),' ChapterLink ')]");
|
|
|
|
|
string[] urls = chapterNodes.Select(node => node.GetAttributeValue("href", "")).ToArray();
|
|
|
|
|
Regex urlRex = new (@"-chapter-([0-9\\.]+)(-index-([0-9\\.]+))?");
|
|
|
|
|
|
|
|
|
|
List<Chapter> chapters = new();
|
|
|
|
|
foreach (string url in urls)
|
|
|
|
|
{
|
|
|
|
|
Match rexMatch = urlRex.Match(url);
|
|
|
|
|
|
|
|
|
|
float? volumeNumber = rexMatch.Groups[3].Success && rexMatch.Groups[3].Value.Length > 0 ?
|
|
|
|
|
float.Parse(rexMatch.Groups[3].Value) : null;
|
|
|
|
|
float chapterNumber = float.Parse(rexMatch.Groups[1].Value);
|
|
|
|
|
string fullUrl = $"https://manga4life.com{url}";
|
|
|
|
|
fullUrl = fullUrl.Replace(Regex.Match(url,"(-page-[0-9])").Value,"");
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
chapters.Add(new Chapter(manga, fullUrl, chapterNumber, volumeNumber, null));
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//Return Chapters ordered by Chapter-Number
|
|
|
|
|
return chapters.Order().ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal override string[] GetChapterImageUrls(Chapter chapter)
|
|
|
|
|
{
|
|
|
|
|
RequestResult requestResult = this.downloadClient.MakeRequest(chapter.Url, RequestType.Default);
|
|
|
|
|
if ((int)requestResult.statusCode < 200 || (int)requestResult.statusCode >= 300 || requestResult.htmlDocument is null)
|
|
|
|
|
{
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HtmlDocument document = requestResult.htmlDocument;
|
|
|
|
|
|
|
|
|
|
HtmlNode gallery = document.DocumentNode.Descendants("div").First(div => div.HasClass("ImageGallery"));
|
|
|
|
|
HtmlNode[] images = gallery.Descendants("img").Where(img => img.HasClass("img-fluid")).ToArray();
|
|
|
|
|
List<string> urls = new();
|
|
|
|
|
foreach(HtmlNode galleryImage in images)
|
|
|
|
|
urls.Add(galleryImage.GetAttributeValue("src", ""));
|
|
|
|
|
return urls.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|