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

@@ -1,6 +1,6 @@
using MangaReader.Core.Search;
using MangaReader.Core.Search.MangaDex;
using MangaReader.Core.Search.NatoManga;
using MangaReader.Core.Sources.NatoManga.Search;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System;
using System.Collections.Generic;

View File

@@ -2,5 +2,6 @@
public class HttpService(HttpClient httpClient) : IHttpService
{
public Task<string> GetStringAsync(string url) => httpClient.GetStringAsync(url);
public Task<string> GetStringAsync(string url, CancellationToken cancellationToken)
=> httpClient.GetStringAsync(url, cancellationToken);
}

View File

@@ -2,5 +2,5 @@
public interface IHttpService
{
Task<string> GetStringAsync(string url);
Task<string> GetStringAsync(string url, CancellationToken cancellationToken);
}

View File

@@ -0,0 +1,6 @@
namespace MangaReader.Core.Metadata;
public interface IMangaMetadataProvider
{
SourceManga GetManga(string url);
}

View File

@@ -1,4 +1,4 @@
namespace MangaReader.Core.WebCrawlers;
namespace MangaReader.Core.Metadata;
public enum MangaStatus
{

View File

@@ -1,8 +1,8 @@
using HtmlAgilityPack;
namespace MangaReader.Core.WebCrawlers;
namespace MangaReader.Core.Metadata;
public abstract class MangaWebCrawler : IMangaWebCrawler
public abstract class MangaWebCrawler : IMangaMetadataProvider
{
public abstract SourceManga GetManga(string url);

View File

@@ -1,4 +1,4 @@
namespace MangaReader.Core.WebCrawlers;
namespace MangaReader.Core.Metadata;
public class SourceManga
{

View File

@@ -1,4 +1,4 @@
namespace MangaReader.Core.WebCrawlers;
namespace MangaReader.Core.Metadata;
public class SourceMangaChapter
{

View File

@@ -1,4 +1,4 @@
using MangaReader.Core.WebCrawlers;
using MangaReader.Core.Metadata;
namespace MangaReader.Core.Pipeline;

View File

@@ -1,5 +1,5 @@
using MangaReader.Core.Data;
using MangaReader.Core.WebCrawlers;
using MangaReader.Core.Metadata;
using Microsoft.EntityFrameworkCore;
using System.Text.RegularExpressions;

View File

@@ -2,5 +2,5 @@
public interface IMangaSearchCoordinator
{
Task<MangaSearchResult[]> SearchAsync(string keyword, CancellationToken cancellationToken);
Task<Dictionary<string, MangaSearchResult[]>> SearchAsync(string keyword, CancellationToken cancellationToken);
}

View File

@@ -1,12 +1,25 @@
namespace MangaReader.Core.Search;
using MangaReader.Core.Sources;
namespace MangaReader.Core.Search;
public class MangaSearchCoordinator(IEnumerable<IMangaSearchProvider> searchProviders) : IMangaSearchCoordinator
{
public async Task<MangaSearchResult[]> SearchAsync(string keyword, CancellationToken cancellationToken)
public async Task<Dictionary<string, MangaSearchResult[]>> SearchAsync(string keyword, CancellationToken cancellationToken)
{
var searchTasks = searchProviders.Select(searchProvider => searchProvider.SearchAsync(keyword, cancellationToken));
var searchTasks = searchProviders.Select(searchProvider =>
GetMangaSearchResultsAsync(searchProvider, keyword, cancellationToken));
var allResults = await Task.WhenAll(searchTasks);
return [.. allResults.SelectMany(mangaSearchResults => mangaSearchResults)];
return allResults.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
}
private static async Task<KeyValuePair<string, MangaSearchResult[]>> GetMangaSearchResultsAsync(
IMangaSearchProvider searchProvider, string keyword, CancellationToken cancellationToken)
{
string sourceName = searchProvider is IMangaSourceComponent sourceComponent ? sourceComponent.SourceId : "Unknown";
MangaSearchResult[] results = await searchProvider.SearchAsync(keyword, cancellationToken);
return new KeyValuePair<string, MangaSearchResult[]>(sourceName, results);
}
}

View File

@@ -12,7 +12,7 @@ public abstract class MangaSearchProviderBase<T>(IHttpService httpService) : IMa
public async Task<MangaSearchResult[]> SearchAsync(string keyword, CancellationToken cancellationToken)
{
T? searchResult = await GetSearchResultAsync(keyword);
T? searchResult = await GetSearchResultAsync(keyword, cancellationToken);
if (searchResult == null)
return [];
@@ -20,10 +20,10 @@ public abstract class MangaSearchProviderBase<T>(IHttpService httpService) : IMa
return GetSearchResult(searchResult);
}
private async Task<T?> GetSearchResultAsync(string keyword)
private async Task<T?> GetSearchResultAsync(string keyword, CancellationToken cancellationToken)
{
string url = GetSearchUrl(keyword);
string response = await httpService.GetStringAsync(url);
string response = await httpService.GetStringAsync(url, cancellationToken);
return JsonSerializer.Deserialize<T>(response, _jsonSerializerOptions);
}

View File

@@ -2,7 +2,6 @@
public record MangaSearchResult
{
public required string Source { get; init; }
public required string Url { get; init; }
public required string Title { get; init; }
public string? Thumbnail { get; init; }

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

@@ -1,10 +1,12 @@
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)
public partial class MangaDexSearchProvider(IHttpService httpService) : MangaSearchProviderBase<MangaDexSearchResult>(httpService), IMangaSourceComponent
{
[GeneratedRegex(@"[^a-z0-9\s-]")]
private static partial Regex InvalidSlugCharactersRegex();
@@ -12,6 +14,8 @@ public partial class MangaDexSearchProvider(IHttpService httpService) : MangaSea
[GeneratedRegex(@"\s+")]
private static partial Regex WhitespaceRegex();
public string SourceId => "MangaDex";
protected override string GetSearchUrl(string keyword)
{
string normalizedKeyword = keyword.ToLowerInvariant().Normalize(NormalizationForm.FormD);
@@ -30,7 +34,6 @@ public partial class MangaDexSearchProvider(IHttpService httpService) : MangaSea
MangaSearchResult mangaSearchResult = new()
{
Source = "MangaDex",
Title = title,
Url = $"https://mangadex.org/title/{searchResultData.Id}/{slug}",
Thumbnail = GetThumbnail(searchResultData)

View File

@@ -1,4 +1,4 @@
namespace MangaReader.Core.Search.MangaDex;
namespace MangaReader.Core.Sources.MangaDex.Search;
public class MangaDexSearchResult
{

View File

@@ -1,4 +1,4 @@
namespace MangaReader.Core.Search.MangaDex;
namespace MangaReader.Core.Sources.MangaDex.Search;
public class MangaDexSearchResultData
{

View File

@@ -1,4 +1,4 @@
namespace MangaReader.Core.Search.MangaDex;
namespace MangaReader.Core.Sources.MangaDex.Search;
public class MangaDexSearchResultDataAttributes
{

View File

@@ -1,4 +1,4 @@
namespace MangaReader.Core.Search.MangaDex;
namespace MangaReader.Core.Sources.MangaDex.Search;
public class MangaDexSearchResultDataRelationship
{

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

@@ -1,8 +1,9 @@
using HtmlAgilityPack;
using MangaReader.Core.Metadata;
using System.Text;
using System.Web;
namespace MangaReader.Core.WebCrawlers.MangaNato;
namespace MangaReader.Core.Sources.MangaNato.Metadata;
public class MangaNatoWebCrawler : MangaWebCrawler
{
@@ -13,14 +14,14 @@ public class MangaNatoWebCrawler : MangaWebCrawler
SourceManga manga = new()
{
Title = node.TitleNode.InnerText,
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 = int.Parse(node.VotesNode.InnerText),
Votes = node.VotesNode != null ? int.Parse(node.VotesNode.InnerText) : 0,
Views = GetViews(node.ViewsNode),
Description = GetTextFromNodes(node.StoryDescriptionTextNodes),
Chapters = GetChapters(node.ChapterNodes)
@@ -29,19 +30,25 @@ public class MangaNatoWebCrawler : MangaWebCrawler
return manga;
}
private static List<string> GetAlternateTitles(HtmlNode node)
private static List<string> GetAlternateTitles(HtmlNode? node)
{
return node.InnerText.Split(';').Select(x => x.Trim()).ToList();
if (node == null)
return [];
return [.. node.InnerText.Split(';').Select(x => x.Trim())];
}
private static List<string> GetAuthors(HtmlNode node)
private static List<string> GetAuthors(HtmlNode? node)
{
return node.InnerText.Split('-').Select(x => x.Trim()).ToList();
if (node == null)
return [];
return [.. node.InnerText.Split('-').Select(x => x.Trim())];
}
private static MangaStatus GetStatus(HtmlNode node)
private static MangaStatus GetStatus(HtmlNode? node)
{
return node.InnerText switch
return node?.InnerText switch
{
"Ongoing" => MangaStatus.Ongoing,
"Completed" => MangaStatus.Complete,
@@ -49,22 +56,31 @@ public class MangaNatoWebCrawler : MangaWebCrawler
};
}
private static List<string> GetGenres(HtmlNode node)
private static List<string> GetGenres(HtmlNode? node)
{
return node.InnerText.Split('-').Select(x => x.Trim()).ToList();
if (node == null)
return [];
return [.. node.InnerText.Split('-').Select(x => x.Trim())];
}
private static DateTime GetUpdateDate(HtmlNode node)
private static DateTime? GetUpdateDate(HtmlNode? node)
{
List<string> dateAndTime = node.InnerText.Split('-').Select(x => x.Trim()).ToList();
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)
private static long GetViews(HtmlNode? node)
{
if (node == null)
return 0;
string text = node.InnerText;
if (int.TryParse(text, out int number))
@@ -93,31 +109,37 @@ public class MangaNatoWebCrawler : MangaWebCrawler
};
}
private static int GetRatingPercent(HtmlNode averageNode, HtmlNode bestNode)
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)
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')]");
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,
Url = chapterNameNode.Attributes["href"].Value,
Name = chapterNameNode?.InnerText ?? string.Empty,
Url = chapterNameNode?.Attributes["href"].Value ?? string.Empty,
Views = GetViews(chapterViewNode),
UploadDate = DateTime.Parse(chapterTimeNode.Attributes["title"].Value)
UploadDate = chapterTimeNode != null ? DateTime.Parse(chapterTimeNode.Attributes["title"].Value) : null
};
chapters.Add(chapter);
@@ -126,8 +148,11 @@ public class MangaNatoWebCrawler : MangaWebCrawler
return chapters;
}
private static float GetChapterNumber(HtmlNode chapterNameNode)
private static float GetChapterNumber(HtmlNode? chapterNameNode)
{
if (chapterNameNode == null)
return 0;
string url = chapterNameNode.Attributes["href"].Value;
int index = url.IndexOf("/chapter-");

View File

@@ -1,6 +1,6 @@
using HtmlAgilityPack;
namespace MangaReader.Core.WebCrawlers.NatoManga;
namespace MangaReader.Core.Sources.NatoManga.Metadata;
public class NatoMangaHtmlDocument
{

View File

@@ -1,11 +1,12 @@
using HtmlAgilityPack;
using System.Text;
using System.Web;
using MangaReader.Core.Metadata;
namespace MangaReader.Core.WebCrawlers.NatoManga;
namespace MangaReader.Core.Sources.NatoManga.Metadata;
public class NatoMangaWebCrawler : MangaWebCrawler
public class NatoMangaWebCrawler : MangaWebCrawler, IMangaSourceComponent
{
public string SourceId => "NatoManga";
public override SourceManga GetManga(string url)
{
HtmlDocument document = GetHtmlDocument(url);
@@ -14,47 +15,19 @@ public class NatoMangaWebCrawler : MangaWebCrawler
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 = int.Parse(node.VotesNode.InnerText),
//Views = GetViews(node.ViewsNode),
//Description = GetTextFromNodes(node.StoryDescriptionTextNodes),
Chapters = GetChapters(node.ChapterNodes)
};
return manga;
}
private static List<string> GetAlternateTitles(HtmlNode node)
{
return node.InnerText.Split(';').Select(x => x.Trim()).ToList();
}
private static List<string> GetAuthors(HtmlNode node)
{
return node.InnerText.Split('-').Select(x => x.Trim()).ToList();
}
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 [];
HtmlNodeCollection genreNodes = node.SelectNodes(".//a");
HtmlNodeCollection? genreNodes = node.SelectNodes(".//a");
if (genreNodes == null)
return [];
@@ -62,15 +35,6 @@ public class NatoMangaWebCrawler : MangaWebCrawler
return [.. genreNodes.Select(genreNode => genreNode.InnerText.Trim())];
}
private static DateTime GetUpdateDate(HtmlNode node)
{
List<string> dateAndTime = node.InnerText.Split('-').Select(x => x.Trim()).ToList();
DateOnly date = DateOnly.Parse(dateAndTime[0]);
TimeOnly time = TimeOnly.Parse(dateAndTime[1]);
return date.ToDateTime(time);
}
private static long GetViews(HtmlNode node)
{
string text = node.InnerText.Trim();
@@ -108,14 +72,6 @@ public class NatoMangaWebCrawler : MangaWebCrawler
};
}
private static int GetRatingPercent(HtmlNode averageNode, HtmlNode bestNode)
{
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 = [];
@@ -164,23 +120,4 @@ public class NatoMangaWebCrawler : MangaWebCrawler
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

@@ -1,12 +1,15 @@
using MangaReader.Core.HttpService;
using MangaReader.Core.Search;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
namespace MangaReader.Core.Search.NatoManga;
namespace MangaReader.Core.Sources.NatoManga.Search;
public class NatoMangaSearchProvider(IHttpService httpService) : MangaSearchProviderBase<NatoMangaSearchResult[]>(httpService)
public partial class NatoMangaSearchProvider(IHttpService httpService) : MangaSearchProviderBase<NatoMangaSearchResult[]>(httpService), IMangaSourceComponent
{
public string SourceId => "NatoManga";
protected override string GetSearchUrl(string keyword)
{
string formattedSeachWord = GetFormattedSearchWord(keyword);
@@ -33,10 +36,10 @@ public class NatoMangaSearchProvider(IHttpService httpService) : MangaSearchProv
}
// Replace non-alphanumeric characters with underscores
string cleaned = Regex.Replace(sb.ToString(), @"[^a-z0-9]+", "_");
string cleaned = NonAlphaNumericCharactersRegex().Replace(sb.ToString(), "_");
// Trim and collapse underscores
cleaned = Regex.Replace(cleaned, "_{2,}", "_").Trim('_');
cleaned = ExtendedUnderscoresRegex().Replace(cleaned, "_").Trim('_');
return cleaned;
}
@@ -46,7 +49,6 @@ public class NatoMangaSearchProvider(IHttpService httpService) : MangaSearchProv
IEnumerable<MangaSearchResult> mangaSearchResults = searchResult.Select(searchResult =>
new MangaSearchResult()
{
Source = "NatoManga",
Title = searchResult.Name,
Thumbnail = searchResult.Thumb,
Url = searchResult.Url
@@ -54,4 +56,10 @@ public class NatoMangaSearchProvider(IHttpService httpService) : MangaSearchProv
return [.. mangaSearchResults];
}
[GeneratedRegex(@"[^a-z0-9]+")]
private static partial Regex NonAlphaNumericCharactersRegex();
[GeneratedRegex("_{2,}")]
private static partial Regex ExtendedUnderscoresRegex();
}

View File

@@ -1,4 +1,4 @@
namespace MangaReader.Core.Search.NatoManga;
namespace MangaReader.Core.Sources.NatoManga.Search;
public record NatoMangaSearchResult
{

View File

@@ -1,6 +0,0 @@
namespace MangaReader.Core.WebCrawlers;
public interface IMangaWebCrawler
{
SourceManga GetManga(string url);
}

View File

@@ -1,61 +0,0 @@
using HtmlAgilityPack;
namespace MangaReader.Core.WebCrawlers.MangaNato;
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[0];
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']");
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']");
}
}