Added MangaDex Api. Updated project structure.

This commit is contained in:
2025-05-26 17:16:25 -04:00
parent 648aa95f32
commit ea8b4a36ff
61 changed files with 4937 additions and 197 deletions

View File

@@ -0,0 +1,6 @@
namespace MangaReader.Core.Sources;
public interface IMangaSourceComponent
{
string SourceId { get; }
}

View File

@@ -0,0 +1,6 @@
namespace MangaReader.Core.Sources.MangaDex.Api;
public class ArtistAttributes : PersonAttributes
{
}

View File

@@ -0,0 +1,6 @@
namespace MangaReader.Core.Sources.MangaDex.Api;
public class ArtistEntity : MangaDexEntity
{
public required ArtistAttributes Attributes { get; set; }
}

View File

@@ -0,0 +1,6 @@
namespace MangaReader.Core.Sources.MangaDex.Api;
public class AuthorAttributes : PersonAttributes
{
}

View File

@@ -0,0 +1,6 @@
namespace MangaReader.Core.Sources.MangaDex.Api;
public class AuthorEntity : MangaDexEntity
{
public required AuthorAttributes Attributes { get; set; }
}

View File

@@ -0,0 +1,9 @@
namespace MangaReader.Core.Sources.MangaDex.Api;
public class ChapterAttributes
{
public string? Chapter { get; set; }
public string? Title { get; set; }
public int Pages { get; set; }
public DateTime PublishAt { get; set; }
}

View File

@@ -0,0 +1,6 @@
namespace MangaReader.Core.Sources.MangaDex.Api;
public class ChapterEntity : MangaDexEntity
{
public required ChapterAttributes Attributes { get; set; }
}

View File

@@ -0,0 +1,12 @@
namespace MangaReader.Core.Sources.MangaDex.Api;
public class CoverArtAttributes
{
public string? Description { get; set; }
public string? Volume { get; set; }
public required string FileName { get; set; }
public string? Locale { get; set; }
public DateTime? CreatedAt { get; set; }
public DateTime? UpdatedAt { get; set; }
public int Version { get; set; }
}

View File

@@ -0,0 +1,6 @@
namespace MangaReader.Core.Sources.MangaDex.Api;
public class CoverArtEntity : MangaDexEntity
{
public required CoverArtAttributes Attributes { get; set; }
}

View File

@@ -0,0 +1,6 @@
namespace MangaReader.Core.Sources.MangaDex.Api;
public class CreatorEntity : MangaDexEntity
{
}

View File

@@ -0,0 +1,7 @@
namespace MangaReader.Core.Sources.MangaDex.Api;
public interface IMangaDexClient
{
Task<MangaDexResponse> GetMangaAsync(Guid mangaGuid, CancellationToken cancellationToken);
Task<MangaDexResponse> GetFeedAsync(Guid mangaGuid, CancellationToken cancellationToken);
}

View File

@@ -0,0 +1,9 @@
namespace MangaReader.Core.Sources.MangaDex.Api;
public class MangaAttributes
{
public Dictionary<string, string> Title { get; set; } = [];
public List<Dictionary<string, string>> AltTitles { get; set; } = [];
public Dictionary<string, string> Description { get; set; } = [];
public List<TagEntity> Tags { get; set; } = [];
}

View File

@@ -0,0 +1,39 @@
using MangaReader.Core.HttpService;
using System.Text.Json;
namespace MangaReader.Core.Sources.MangaDex.Api
{
public class MangaDexClient(IHttpService httpService) : IMangaDexClient
{
private static readonly JsonSerializerOptions _jsonSerializerOptions;
static MangaDexClient()
{
_jsonSerializerOptions = new()
{
PropertyNameCaseInsensitive = true
};
_jsonSerializerOptions.Converters.Add(new MangaDexResponseConverter());
_jsonSerializerOptions.Converters.Add(new MangaDexEntityConverter());
}
private async Task<MangaDexResponse> GetAsync(string url, CancellationToken cancellationToken)
{
string response = await httpService.GetStringAsync(url, cancellationToken);
return JsonSerializer.Deserialize<MangaDexResponse>(response, _jsonSerializerOptions)
?? new() { Response = "failed", Result = "unknown" };
}
public async Task<MangaDexResponse> GetMangaAsync(Guid mangaGuid, CancellationToken cancellationToken)
{
return await GetAsync($"https://api.mangadex.org/manga/{mangaGuid}?includes[]=artist&includes[]=author&includes[]=cover_art", cancellationToken);
}
public async Task<MangaDexResponse> GetFeedAsync(Guid mangaGuid, CancellationToken cancellationToken)
{
return await GetAsync($"https://api.mangadex.org/manga/{mangaGuid}/feed?translatedLanguage[]=en&limit=96&includes[]=scanlation_group&includes[]=user&order[volume]=desc&order[chapter]=desc&offset=0&contentRating[]=safe&contentRating[]=suggestive&contentRating[]=erotica&contentRating[]=pornographic", cancellationToken);
}
}
}

View File

@@ -0,0 +1,6 @@
namespace MangaReader.Core.Sources.MangaDex.Api;
public class MangaDexCollectionResponse : MangaDexResponse
{
public List<MangaDexEntity> Data { get; set; } = [];
}

View File

@@ -0,0 +1,8 @@
namespace MangaReader.Core.Sources.MangaDex.Api;
public class MangaDexEntity
{
public required Guid Id { get; set; }
public required string Type { get; set; }
public List<MangaDexEntity> Relationships { get; set; } = [];
}

View File

@@ -0,0 +1,33 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace MangaReader.Core.Sources.MangaDex.Api;
public class MangaDexEntityConverter : JsonConverter<MangaDexEntity>
{
public override MangaDexEntity? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
using var doc = JsonDocument.ParseValue(ref reader);
var root = doc.RootElement;
var type = root.GetProperty("type").GetString();
return type switch
{
"manga" => JsonSerializer.Deserialize<MangaEntity>(root.GetRawText(), options),
"author" => JsonSerializer.Deserialize<AuthorEntity>(root.GetRawText(), options),
"artist" => JsonSerializer.Deserialize<ArtistEntity>(root.GetRawText(), options),
"creator" => JsonSerializer.Deserialize<CreatorEntity>(root.GetRawText(), options),
"tag" => JsonSerializer.Deserialize<TagEntity>(root.GetRawText(), options),
"chapter" => JsonSerializer.Deserialize<ChapterEntity>(root.GetRawText(), options),
"scanlation_group" => JsonSerializer.Deserialize<ScanlationGroupEntity>(root.GetRawText(), options),
"cover_art" => JsonSerializer.Deserialize<CoverArtEntity>(root.GetRawText(), options),
_ => throw new NotSupportedException($"Unknown type '{type}'")
};
}
public override void Write(Utf8JsonWriter writer, MangaDexEntity value, JsonSerializerOptions options)
{
JsonSerializer.Serialize(writer, (object)value, options);
}
}

View File

@@ -0,0 +1,6 @@
namespace MangaReader.Core.Sources.MangaDex.Api;
public class MangaDexEntityResponse : MangaDexResponse
{
public MangaDexEntity? Data { get; set; }
}

View File

@@ -0,0 +1,7 @@
namespace MangaReader.Core.Sources.MangaDex.Api;
public class MangaDexResponse
{
public required string Result { get; set; }
public required string Response { get; set; }
}

View File

@@ -0,0 +1,62 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace MangaReader.Core.Sources.MangaDex.Api;
public class MangaDexResponseConverter : JsonConverter<MangaDexResponse>
{
public override MangaDexResponse? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
using JsonDocument doc = JsonDocument.ParseValue(ref reader);
JsonElement root = doc.RootElement;
string result = root.GetProperty("result").GetString() ?? "fail";
string response = root.GetProperty("response").GetString() ?? "unknown";
JsonElement dataProperty = root.GetProperty("data");
if (response == "collection" && dataProperty.ValueKind == JsonValueKind.Array)
{
MangaDexCollectionResponse collectionResponse = new()
{
Result = result,
Response = response,
Data = []
};
foreach (var item in dataProperty.EnumerateArray())
{
MangaDexEntity? entity = JsonSerializer.Deserialize<MangaDexEntity>(item.GetRawText(), options);
if (entity != null)
collectionResponse.Data.Add(entity);
}
return collectionResponse;
}
else if (response == "entity" && dataProperty.ValueKind == JsonValueKind.Object)
{
MangaDexEntityResponse entityResponse = new()
{
Result = result,
Response = response,
Data = JsonSerializer.Deserialize<MangaDexEntity>(dataProperty.GetRawText(), options)
};
return entityResponse;
}
else
{
return new()
{
Result = result,
Response = response
};
}
}
public override void Write(Utf8JsonWriter writer, MangaDexResponse value, JsonSerializerOptions options)
{
JsonSerializer.Serialize(writer, (object)value, options);
}
}

View File

@@ -0,0 +1,6 @@
namespace MangaReader.Core.Sources.MangaDex.Api;
public class MangaEntity : MangaDexEntity
{
public required MangaAttributes Attributes { get; set; }
}

View File

@@ -0,0 +1,24 @@
namespace MangaReader.Core.Sources.MangaDex.Api;
public class PersonAttributes
{
public string? ImageUrl { get; set; }
public Dictionary<string, string> Biography { get; set; } = [];
public string? Twitter { get; set; }
public string? Pixiv { get; set; }
public string? MelonBook { get; set; }
public string? Fanbox { get; set; }
public string? Booth { get; set; }
public string? Namicomi { get; set; }
public string? NicoVideo { get; set; }
public string? Skeb { get; set; }
public string? Fantia { get; set; }
public string? Tumblr { get; set; }
public string? Youtube { get; set; }
public string? Weibo { get; set; }
public string? Naver { get; set; }
public string? Website { get; set; }
public DateTime? CreatedAt { get; set; }
public DateTime? UpdatedAt { get; set; }
public int Version { get; set; }
}

View File

@@ -0,0 +1,8 @@
namespace MangaReader.Core.Sources.MangaDex.Api;
public class ScanlationGroupAttributes
{
public string Name { get; set; } = default!;
public string? Website { get; set; }
// etc...
}

View File

@@ -0,0 +1,6 @@
namespace MangaReader.Core.Sources.MangaDex.Api;
public class ScanlationGroupEntity : MangaDexEntity
{
public required ScanlationGroupAttributes Attributes { get; set; }
}

View File

@@ -0,0 +1,9 @@
namespace MangaReader.Core.Sources.MangaDex.Api;
public class TagAttributes
{
public Dictionary<string, string> Name { get; set; } = [];
public Dictionary<string, string> Description { get; set; } = [];
public required string Group { get; set; }
public int Version { get; set; }
}

View File

@@ -0,0 +1,6 @@
namespace MangaReader.Core.Sources.MangaDex.Api;
public class TagEntity : MangaDexEntity
{
public required TagAttributes Attributes { get; set; }
}

View File

@@ -0,0 +1,36 @@
using MangaReader.Core.HttpService;
using MangaReader.Core.Metadata;
namespace MangaReader.Core.Sources.MangaDex.Metadata;
//public class MangaDexMetadataProvider(IHttpService httpService) : IMangaMetadataProvider, IMangaSourceComponent
//{
// public string SourceId => "MangaDex";
// public async Task<SourceManga> GetManga(string url)
// {
// Guid mangaGuid = GetSourceMangaGuid(url);
// await GetSomething(mangaGuid);
// throw new NotImplementedException();
// }
// private static Guid GetSourceMangaGuid(string url)
// {
// string[] parts = url.Split('/');
// if (parts.Length < 5 || Guid.TryParse(parts[4], out Guid mangaGuid) == false)
// {
// throw new Exception("Unable to get guid from MangaDex url: " + url);
// }
// return mangaGuid;
// }
// private async Task GetSomething(Guid mangaGuid)
// {
// // https://api.mangadex.org/manga/ee96e2b7-9af2-4864-9656-649f4d3b6fec?includes[]=artist&includes[]=author&includes[]=cover_art
// await httpService.GetStringAsync($"https://api.mangadex.org/manga/{mangaGuid}/feed?translatedLanguage[]=en&limit=96&includes[]=scanlation_group&includes[]=user&order[volume]=desc&order[chapter]=desc&offset=0&contentRating[]=safe&contentRating[]=suggestive&contentRating[]=erotica&contentRating[]=pornographic");
// }
//}

View File

@@ -0,0 +1,75 @@
using MangaReader.Core.HttpService;
using MangaReader.Core.Sources;
using MangaReader.Core.Sources.MangaDex.Search;
using System.Text;
using System.Text.RegularExpressions;
namespace MangaReader.Core.Search.MangaDex;
public partial class MangaDexSearchProvider(IHttpService httpService) : MangaSearchProviderBase<MangaDexSearchResult>(httpService), IMangaSourceComponent
{
[GeneratedRegex(@"[^a-z0-9\s-]")]
private static partial Regex InvalidSlugCharactersRegex();
[GeneratedRegex(@"\s+")]
private static partial Regex WhitespaceRegex();
public string SourceId => "MangaDex";
protected override string GetSearchUrl(string keyword)
{
string normalizedKeyword = keyword.ToLowerInvariant().Normalize(NormalizationForm.FormD);
return $"https://api.mangadex.org/manga?title={normalizedKeyword}&limit=5";
}
protected override MangaSearchResult[] GetSearchResult(MangaDexSearchResult searchResult)
{
List<MangaSearchResult> mangaSearchResults = [];
foreach (MangaDexSearchResultData searchResultData in searchResult.Data)
{
string title = searchResultData.Attributes.Title.FirstOrDefault().Value;
string slug = GenerateSlug(title);
MangaSearchResult mangaSearchResult = new()
{
Title = title,
Url = $"https://mangadex.org/title/{searchResultData.Id}/{slug}",
Thumbnail = GetThumbnail(searchResultData)
};
mangaSearchResults.Add(mangaSearchResult);
}
return [.. mangaSearchResults];
}
public static string GenerateSlug(string title)
{
// title.ToLowerInvariant().Normalize(NormalizationForm.FormD);
title = title.ToLowerInvariant();
//title = InvalidSlugCharactersRegex().Replace(title, ""); // remove invalid chars
title = InvalidSlugCharactersRegex().Replace(title, "-"); // replace invalid chars with dash
title = WhitespaceRegex().Replace(title, "-"); // replace spaces with dash
return title.Trim('-');
}
private static string? GetThumbnail(MangaDexSearchResultData searchResultData)
{
var coverArtRelationship = searchResultData.Relationships.FirstOrDefault(x => x.Type == "cover_art");
if (coverArtRelationship == null)
return null;
if (coverArtRelationship.Attributes.TryGetValue("fileName", out object? fileNameValue) == false)
return null;
if (fileNameValue == null)
return null;
return $"https://mangadex.org/covers/{searchResultData.Id}/{fileNameValue}";
}
}

View File

@@ -0,0 +1,8 @@
namespace MangaReader.Core.Sources.MangaDex.Search;
public class MangaDexSearchResult
{
public required string Result { get; set; }
public required string Response { get; set; }
public MangaDexSearchResultData[] Data { get; set; } = [];
}

View File

@@ -0,0 +1,8 @@
namespace MangaReader.Core.Sources.MangaDex.Search;
public class MangaDexSearchResultData
{
public required Guid Id { get; set; }
public required MangaDexSearchResultDataAttributes Attributes { get; set; }
public MangaDexSearchResultDataRelationship[] Relationships { get; set; } = [];
}

View File

@@ -0,0 +1,8 @@
namespace MangaReader.Core.Sources.MangaDex.Search;
public class MangaDexSearchResultDataAttributes
{
public Dictionary<string, string> Title { get; set; } = [];
public List<Dictionary<string, string>> AltTitles { get; set; } = [];
public Dictionary<string, string> Description { get; set; } = [];
}

View File

@@ -0,0 +1,8 @@
namespace MangaReader.Core.Sources.MangaDex.Search;
public class MangaDexSearchResultDataRelationship
{
public required Guid Id { get; set; }
public required string Type { get; set; }
public Dictionary<string, object> Attributes { get; set; } = [];
}

View File

@@ -0,0 +1,69 @@
using HtmlAgilityPack;
namespace MangaReader.Core.Sources.MangaNato.Metadata;
public class MangaNatoMangaDocument
{
public HtmlNode? StoryInfoNode { get; }
public HtmlNode? TitleNode { get; }
public HtmlNode? StoryInfoRightNode { get; }
public HtmlNode? VariationsTableInfo { get; }
public HtmlNodeCollection? VariationsTableValueNodes { get; }
public HtmlNode? AlternateTitlesNode { get; }
public HtmlNode? AuthorsNode { get; }
public HtmlNode? StatusNode { get; }
public HtmlNode? GenresNode { get; }
public HtmlNode? StoryInfoRightExtentNode { get; }
public HtmlNodeCollection? StoryInfoRightExtentValueNodes { get; }
public HtmlNode? UpdateDateNode { get; }
public HtmlNode? ViewsNode { get; }
public HtmlNode? ReviewAggregateNode { get; }
public HtmlNode? RatingNode { get; }
public HtmlNode? AverageRatingNode { get; }
public HtmlNode? BestRatingNode { get; }
public HtmlNode? VotesNode { get; set; }
public HtmlNode? StoryDescriptionNode { get; }
public List<HtmlNode> StoryDescriptionTextNodes { get; }
public HtmlNode? StoryChapterListNode { get; }
public HtmlNodeCollection? ChapterNodes { get; }
public MangaNatoMangaDocument(HtmlDocument document)
{
StoryInfoNode = document.DocumentNode.SelectSingleNode(".//div[@class='panel-story-info']");
TitleNode = StoryInfoNode?.SelectSingleNode(".//h1");
StoryDescriptionNode = StoryInfoNode?.SelectSingleNode(".//div[@class='panel-story-info-description']");
StoryDescriptionTextNodes = StoryDescriptionNode?.ChildNodes.Skip(2).Take(StoryDescriptionNode.ChildNodes.Count - 2).ToList() ?? [];
StoryInfoRightNode = StoryInfoNode?.SelectSingleNode(".//div[@class='story-info-right']");
VariationsTableInfo = StoryInfoRightNode?.SelectSingleNode(".//table[@class='variations-tableInfo']");
VariationsTableValueNodes = VariationsTableInfo?.SelectNodes(".//td[@class='table-value']");
AlternateTitlesNode = VariationsTableValueNodes?.FirstOrDefault();
if (VariationsTableValueNodes != null && VariationsTableValueNodes.Count >= 3)
{
AuthorsNode = VariationsTableValueNodes[1];
StatusNode = VariationsTableValueNodes[2];
GenresNode = VariationsTableValueNodes[3];
}
StoryInfoRightExtentNode = StoryInfoRightNode?.SelectSingleNode(".//div[@class='story-info-right-extent']");
StoryInfoRightExtentValueNodes = StoryInfoRightExtentNode?.SelectNodes(".//span[@class='stre-value']");
if (StoryInfoRightExtentValueNodes != null && StoryInfoRightExtentValueNodes.Count >= 2)
{
UpdateDateNode = StoryInfoRightExtentValueNodes[0];
ViewsNode = StoryInfoRightExtentValueNodes[1];
}
// v:Review-aggregate
ReviewAggregateNode = StoryInfoRightNode?.SelectSingleNode(".//em[@typeof='v:Review-aggregate']");
RatingNode = ReviewAggregateNode?.SelectSingleNode(".//em[@typeof='v:Rating']");
AverageRatingNode = RatingNode?.SelectSingleNode(".//em[@property='v:average']");
BestRatingNode = RatingNode?.SelectSingleNode(".//em[@property='v:best']");
VotesNode = ReviewAggregateNode?.SelectSingleNode(".//em[@property='v:votes']");
StoryChapterListNode = document.DocumentNode.SelectSingleNode(".//div[@class='panel-story-chapter-list']");
ChapterNodes = StoryChapterListNode?.SelectNodes(".//li[@class='a-h']");
}
}

View File

@@ -0,0 +1,185 @@
using HtmlAgilityPack;
using MangaReader.Core.Metadata;
using System.Text;
using System.Web;
namespace MangaReader.Core.Sources.MangaNato.Metadata;
public class MangaNatoWebCrawler : MangaWebCrawler
{
public override SourceManga GetManga(string url)
{
HtmlDocument document = GetHtmlDocument(url);
MangaNatoMangaDocument node = new(document);
SourceManga manga = new()
{
Title = node.TitleNode?.InnerText ?? string.Empty,
AlternateTitles = GetAlternateTitles(node.AlternateTitlesNode),
Authors = GetAuthors(node.AuthorsNode),
Status = GetStatus(node.StatusNode),
Genres = GetGenres(node.GenresNode),
UpdateDate = GetUpdateDate(node.UpdateDateNode),
RatingPercent = GetRatingPercent(node.AverageRatingNode, node.BestRatingNode),
Votes = node.VotesNode != null ? int.Parse(node.VotesNode.InnerText) : 0,
Views = GetViews(node.ViewsNode),
Description = GetTextFromNodes(node.StoryDescriptionTextNodes),
Chapters = GetChapters(node.ChapterNodes)
};
return manga;
}
private static List<string> GetAlternateTitles(HtmlNode? node)
{
if (node == null)
return [];
return [.. node.InnerText.Split(';').Select(x => x.Trim())];
}
private static List<string> GetAuthors(HtmlNode? node)
{
if (node == null)
return [];
return [.. node.InnerText.Split('-').Select(x => x.Trim())];
}
private static MangaStatus GetStatus(HtmlNode? node)
{
return node?.InnerText switch
{
"Ongoing" => MangaStatus.Ongoing,
"Completed" => MangaStatus.Complete,
_ => MangaStatus.Unknown,
};
}
private static List<string> GetGenres(HtmlNode? node)
{
if (node == null)
return [];
return [.. node.InnerText.Split('-').Select(x => x.Trim())];
}
private static DateTime? GetUpdateDate(HtmlNode? node)
{
if (node == null)
return null;
List<string> dateAndTime = [.. node.InnerText.Split('-').Select(x => x.Trim())];
DateOnly date = DateOnly.Parse(dateAndTime[0]);
TimeOnly time = TimeOnly.Parse(dateAndTime[1]);
return date.ToDateTime(time);
}
private static long GetViews(HtmlNode? node)
{
if (node == null)
return 0;
string text = node.InnerText;
if (int.TryParse(text, out int number))
return number;
ReadOnlySpan<char> shortText = text.AsSpan(0, text.Length - 1);
if (double.TryParse(shortText, out double formattedNumber) == false)
return 0;
char suffix = text[^1];
long multiplier = GetMultiplier(suffix);
return (int)(formattedNumber * multiplier);
}
private static long GetMultiplier(char c)
{
return c switch
{
'K' => 1_000,
'M' => 1_000_000,
'B' => 1_000_000_000,
'T' => 1_000_000_000_000,
_ => 0,
};
}
private static int GetRatingPercent(HtmlNode? averageNode, HtmlNode? bestNode)
{
if (averageNode == null || bestNode == null)
return 0;
double average = Convert.ToDouble(averageNode.InnerText);
double best = Convert.ToDouble(bestNode.InnerText);
return (int)Math.Round(average / best * 100);
}
private static List<SourceMangaChapter> GetChapters(HtmlNodeCollection? chapterNodes)
{
List<SourceMangaChapter> chapters = [];
if (chapterNodes == null)
return chapters;
foreach (var node in chapterNodes)
{
HtmlNode? chapterNameNode = node.SelectSingleNode(".//a[contains(@class, 'chapter-name')]");
HtmlNode? chapterViewNode = node.SelectSingleNode(".//span[contains(@class, 'chapter-view')]");
HtmlNode? chapterTimeNode = node.SelectSingleNode(".//span[contains(@class, 'chapter-time')]");
SourceMangaChapter chapter = new()
{
Number = GetChapterNumber(chapterNameNode),
Name = chapterNameNode?.InnerText ?? string.Empty,
Url = chapterNameNode?.Attributes["href"].Value ?? string.Empty,
Views = GetViews(chapterViewNode),
UploadDate = chapterTimeNode != null ? DateTime.Parse(chapterTimeNode.Attributes["title"].Value) : null
};
chapters.Add(chapter);
}
return chapters;
}
private static float GetChapterNumber(HtmlNode? chapterNameNode)
{
if (chapterNameNode == null)
return 0;
string url = chapterNameNode.Attributes["href"].Value;
int index = url.IndexOf("/chapter-");
if (index == -1)
return 0;
string chapterNumber = url[(index + "/chapter-".Length)..];
return float.Parse(chapterNumber);
}
private static string GetTextFromNodes(List<HtmlNode> nodes)
{
StringBuilder stringBuilder = new();
foreach (HtmlNode node in nodes)
{
if (node.Name == "br")
{
stringBuilder.AppendLine();
}
else
{
stringBuilder.Append(HttpUtility.HtmlDecode(node.InnerText).Replace("\r\n", "").Trim());
}
}
return stringBuilder.ToString();
}
}

View File

@@ -0,0 +1,21 @@
using HtmlAgilityPack;
namespace MangaReader.Core.Sources.NatoManga.Metadata;
public class NatoMangaHtmlDocument
{
public HtmlNode? MangaInfoTextNode { get; }
public HtmlNode? TitleNode { get; }
public HtmlNode? GenresNode { get; }
public HtmlNode? ChapterListNode { get; }
public HtmlNodeCollection? ChapterNodes { get; }
public NatoMangaHtmlDocument(HtmlDocument document)
{
MangaInfoTextNode = document.DocumentNode.SelectSingleNode(".//ul[@class='manga-info-text']");
TitleNode = MangaInfoTextNode?.SelectSingleNode(".//li//h1");
GenresNode = MangaInfoTextNode?.SelectSingleNode(".//li[@class='genres']");
ChapterListNode = document.DocumentNode.SelectSingleNode(".//div[@class='chapter-list']");
ChapterNodes = ChapterListNode?.SelectNodes(".//div[@class='row']");
}
}

View File

@@ -0,0 +1,123 @@
using HtmlAgilityPack;
using MangaReader.Core.Metadata;
namespace MangaReader.Core.Sources.NatoManga.Metadata;
public class NatoMangaWebCrawler : MangaWebCrawler, IMangaSourceComponent
{
public string SourceId => "NatoManga";
public override SourceManga GetManga(string url)
{
HtmlDocument document = GetHtmlDocument(url);
NatoMangaHtmlDocument node = new(document);
SourceManga manga = new()
{
Title = node.TitleNode?.InnerText ?? string.Empty,
Genres = GetGenres(node.GenresNode),
Chapters = GetChapters(node.ChapterNodes)
};
return manga;
}
private static List<string> GetGenres(HtmlNode? node)
{
if (node == null)
return [];
HtmlNodeCollection? genreNodes = node.SelectNodes(".//a");
if (genreNodes == null)
return [];
return [.. genreNodes.Select(genreNode => genreNode.InnerText.Trim())];
}
private static long GetViews(HtmlNode node)
{
string text = node.InnerText.Trim();
if (int.TryParse(text, out int number))
return number;
if (double.TryParse(text, out double doubleNumber))
return (int)doubleNumber;
ReadOnlySpan<char> shortText = text.AsSpan(0, text.Length - 1);
if (double.TryParse(shortText, out double formattedNumber) == false)
return 0;
char suffix = text[^1];
//if (char.GetNumericValue(suffix) > -1)
// return (int)formattedNumber;
long multiplier = GetMultiplier(suffix);
return (int)(formattedNumber * multiplier);
}
private static long GetMultiplier(char c)
{
return c switch
{
'K' => 1_000,
'M' => 1_000_000,
'B' => 1_000_000_000,
'T' => 1_000_000_000_000,
_ => 0,
};
}
private static List<SourceMangaChapter> GetChapters(HtmlNodeCollection? chapterNodes)
{
List<SourceMangaChapter> chapters = [];
if (chapterNodes == null)
return chapters;
foreach (var node in chapterNodes)
{
HtmlNodeCollection? chapterPropertyNodes = node.SelectNodes(".//span");
if (chapterPropertyNodes == null || chapterPropertyNodes.Count < 3)
continue;
HtmlNode? chapterNameNode = chapterPropertyNodes[0].SelectSingleNode(".//a");
HtmlNode chapterViewNode = chapterPropertyNodes[1];
HtmlNode chapterTimeNode = chapterPropertyNodes[2];
if (chapterNameNode == null)
continue;
SourceMangaChapter chapter = new()
{
Number = GetChapterNumber(chapterNameNode),
Name = chapterNameNode.InnerText,
Url = chapterNameNode.Attributes["href"].Value,
Views = GetViews(chapterViewNode),
UploadDate = DateTime.Parse(chapterTimeNode.Attributes["title"].Value)
};
chapters.Add(chapter);
}
return chapters;
}
private static float GetChapterNumber(HtmlNode chapterNameNode)
{
string url = chapterNameNode.Attributes["href"].Value;
int index = url.IndexOf("/chapter-");
if (index == -1)
return 0;
string chapterNumber = url[(index + "/chapter-".Length)..].Replace('-', '.');
return float.Parse(chapterNumber);
}
}

View File

@@ -0,0 +1,65 @@
using MangaReader.Core.HttpService;
using MangaReader.Core.Search;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
namespace MangaReader.Core.Sources.NatoManga.Search;
public partial class NatoMangaSearchProvider(IHttpService httpService) : MangaSearchProviderBase<NatoMangaSearchResult[]>(httpService), IMangaSourceComponent
{
public string SourceId => "NatoManga";
protected override string GetSearchUrl(string keyword)
{
string formattedSeachWord = GetFormattedSearchWord(keyword);
return $"https://www.natomanga.com/home/search/json?searchword={formattedSeachWord}";
}
private static string GetFormattedSearchWord(string input)
{
if (string.IsNullOrWhiteSpace(input))
return string.Empty;
// Convert to lowercase and normalize to decompose accents
string normalized = input.ToLowerInvariant()
.Normalize(NormalizationForm.FormD);
// Remove diacritics
var sb = new StringBuilder();
foreach (var c in normalized)
{
var unicodeCategory = CharUnicodeInfo.GetUnicodeCategory(c);
if (unicodeCategory != UnicodeCategory.NonSpacingMark)
sb.Append(c);
}
// Replace non-alphanumeric characters with underscores
string cleaned = NonAlphaNumericCharactersRegex().Replace(sb.ToString(), "_");
// Trim and collapse underscores
cleaned = ExtendedUnderscoresRegex().Replace(cleaned, "_").Trim('_');
return cleaned;
}
protected override MangaSearchResult[] GetSearchResult(NatoMangaSearchResult[] searchResult)
{
IEnumerable<MangaSearchResult> mangaSearchResults = searchResult.Select(searchResult =>
new MangaSearchResult()
{
Title = searchResult.Name,
Thumbnail = searchResult.Thumb,
Url = searchResult.Url
});
return [.. mangaSearchResults];
}
[GeneratedRegex(@"[^a-z0-9]+")]
private static partial Regex NonAlphaNumericCharactersRegex();
[GeneratedRegex("_{2,}")]
private static partial Regex ExtendedUnderscoresRegex();
}

View File

@@ -0,0 +1,12 @@
namespace MangaReader.Core.Sources.NatoManga.Search;
public record NatoMangaSearchResult
{
public int Id { get; init; }
public string? Author { get; init; }
public required string Name { get; init; }
public string? ChapterLatest { get; init; }
public required string Url { get; init; }
public string? Thumb { get; init; }
public string? Slug { get; init; }
}