Project restructuring.

This commit is contained in:
2025-05-26 22:03:08 -04:00
parent ea8b4a36ff
commit 6accb373cd
26 changed files with 421 additions and 156 deletions

View File

@@ -2,5 +2,5 @@
public class ArtistEntity : MangaDexEntity
{
public required ArtistAttributes Attributes { get; set; }
public ArtistAttributes? Attributes { get; set; }
}

View File

@@ -2,5 +2,5 @@
public class AuthorEntity : MangaDexEntity
{
public required AuthorAttributes Attributes { get; set; }
public AuthorAttributes? Attributes { get; set; }
}

View File

@@ -2,8 +2,15 @@
public class ChapterAttributes
{
public string? Volume { get; set; }
public string? Chapter { get; set; }
public string? Title { get; set; }
public int Pages { get; set; }
public string? TranslatedLanguage { get; set; }
public string? ExternalUrl { get; set; }
public DateTime PublishAt { get; set; }
public DateTime ReadableAt { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
public int Pages { get; set; }
public int Version { get; set; }
}

View File

@@ -2,5 +2,5 @@
public class ChapterEntity : MangaDexEntity
{
public required ChapterAttributes Attributes { get; set; }
public ChapterAttributes? Attributes { get; set; }
}

View File

@@ -2,5 +2,5 @@
public class CoverArtEntity : MangaDexEntity
{
public required CoverArtAttributes Attributes { get; set; }
public CoverArtAttributes? Attributes { get; set; }
}

View File

@@ -2,6 +2,10 @@
public interface IMangaDexClient
{
Task<MangaDexResponse> GetMangaAsync(Guid mangaGuid, CancellationToken cancellationToken);
Task<MangaDexResponse> GetFeedAsync(Guid mangaGuid, CancellationToken cancellationToken);
Task<MangaDexResponse?> SearchMangaByTitleAsync(string title, CancellationToken cancellationToken);
Task<MangaDexResponse?> SearchMangaByAuthorAsync(string author, CancellationToken cancellationToken);
Task<MangaDexResponse?> SearchMangaByGroupAsync(string group, CancellationToken cancellationToken);
Task<MangaDexResponse?> GetMangaAsync(Guid mangaGuid, CancellationToken cancellationToken);
Task<MangaDexResponse?> GetFeedAsync(Guid mangaGuid, CancellationToken cancellationToken);
Task<MangaDexChapterResponse?> GetChapterAsync(Guid chapterGuid, CancellationToken cancellationToken);
}

View File

@@ -0,0 +1,8 @@
namespace MangaReader.Core.Sources.MangaDex.Api;
public class MangaDexChapter
{
public required string Hash { get; set; }
public List<string> Data { get; set; } = [];
public List<string> DataSaver { get; set; } = [];
}

View File

@@ -0,0 +1,8 @@
namespace MangaReader.Core.Sources.MangaDex.Api;
public class MangaDexChapterResponse
{
public required string Result { get; set; }
public required string BaseUrl { get; set; }
public MangaDexChapter? Chapter { get; set; }
}

View File

@@ -1,4 +1,5 @@
using MangaReader.Core.HttpService;
using System.Text;
using System.Text.Json;
namespace MangaReader.Core.Sources.MangaDex.Api
@@ -18,22 +19,55 @@ namespace MangaReader.Core.Sources.MangaDex.Api
_jsonSerializerOptions.Converters.Add(new MangaDexEntityConverter());
}
private async Task<MangaDexResponse> GetAsync(string url, CancellationToken cancellationToken)
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" };
return JsonSerializer.Deserialize<MangaDexResponse>(response, _jsonSerializerOptions);
}
public async Task<MangaDexResponse> GetMangaAsync(Guid mangaGuid, CancellationToken cancellationToken)
public async Task<MangaDexResponse?> SearchMangaByTitleAsync(string title, CancellationToken cancellationToken)
{
string normalizedKeyword = GetNormalizedKeyword(title);
return await GetAsync($"https://api.mangadex.org/manga?title={normalizedKeyword}&limit=5", cancellationToken);
}
public async Task<MangaDexResponse?> SearchMangaByAuthorAsync(string author, CancellationToken cancellationToken)
{
string normalizedKeyword = GetNormalizedKeyword(author);
return await GetAsync($"https://api.mangadex.org/manga?author={normalizedKeyword}&limit=5", cancellationToken);
}
public async Task<MangaDexResponse?> SearchMangaByGroupAsync(string group, CancellationToken cancellationToken)
{
string normalizedKeyword = GetNormalizedKeyword(group);
return await GetAsync($"https://api.mangadex.org/manga?group={normalizedKeyword}&limit=5", cancellationToken);
}
protected static string GetNormalizedKeyword(string keyword)
{
return keyword.ToLowerInvariant().Normalize(NormalizationForm.FormD);
}
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)
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);
}
public async Task<MangaDexChapterResponse?> GetChapterAsync(Guid chapterGuid, CancellationToken cancellationToken)
{
string url = $"https://api.mangadex.org/at-home/server/{chapterGuid}?forcePort443=false";
string response = await httpService.GetStringAsync(url, cancellationToken);
return JsonSerializer.Deserialize<MangaDexChapterResponse>(response, _jsonSerializerOptions);
}
}
}

View File

@@ -22,6 +22,7 @@ public class MangaDexEntityConverter : JsonConverter<MangaDexEntity>
"chapter" => JsonSerializer.Deserialize<ChapterEntity>(root.GetRawText(), options),
"scanlation_group" => JsonSerializer.Deserialize<ScanlationGroupEntity>(root.GetRawText(), options),
"cover_art" => JsonSerializer.Deserialize<CoverArtEntity>(root.GetRawText(), options),
"user" => JsonSerializer.Deserialize<UserEntity>(root.GetRawText(), options),
_ => throw new NotSupportedException($"Unknown type '{type}'")
};
}

View File

@@ -2,5 +2,5 @@
public class MangaEntity : MangaDexEntity
{
public required MangaAttributes Attributes { get; set; }
public MangaAttributes? Attributes { get; set; }
}

View File

@@ -2,6 +2,7 @@
public class PersonAttributes
{
public required string Name { get; set; }
public string? ImageUrl { get; set; }
public Dictionary<string, string> Biography { get; set; } = [];
public string? Twitter { get; set; }

View File

@@ -2,5 +2,5 @@
public class TagEntity : MangaDexEntity
{
public required TagAttributes Attributes { get; set; }
public TagAttributes? Attributes { get; set; }
}

View File

@@ -0,0 +1,8 @@
namespace MangaReader.Core.Sources.MangaDex.Api;
public class UserAttributes
{
public required string UserName { get; set; }
public List<string> Roles { get; set; } = [];
public int Version { get; set; }
}

View File

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

View File

@@ -1,12 +1,10 @@
using MangaReader.Core.HttpService;
using MangaReader.Core.Sources;
using MangaReader.Core.Sources.MangaDex.Search;
using System.Text;
using MangaReader.Core.Search;
using MangaReader.Core.Sources.MangaDex.Api;
using System.Text.RegularExpressions;
namespace MangaReader.Core.Search.MangaDex;
namespace MangaReader.Core.Sources.MangaDex.Search;
public partial class MangaDexSearchProvider(IHttpService httpService) : MangaSearchProviderBase<MangaDexSearchResult>(httpService), IMangaSourceComponent
public partial class MangaDexSearchProvider(IMangaDexClient mangaDexClient) : IMangaSearchProvider, IMangaSourceComponent
{
[GeneratedRegex(@"[^a-z0-9\s-]")]
private static partial Regex InvalidSlugCharactersRegex();
@@ -16,35 +14,50 @@ public partial class MangaDexSearchProvider(IHttpService httpService) : MangaSea
public string SourceId => "MangaDex";
protected override string GetSearchUrl(string keyword)
public async Task<MangaSearchResult[]> SearchAsync(string keyword, CancellationToken cancellationToken)
{
string normalizedKeyword = keyword.ToLowerInvariant().Normalize(NormalizationForm.FormD);
MangaDexResponse? response = await mangaDexClient.SearchMangaByTitleAsync(keyword, cancellationToken);
return $"https://api.mangadex.org/manga?title={normalizedKeyword}&limit=5";
}
if (response == null || (response is not MangaDexCollectionResponse collectionResponse))
return [];
protected override MangaSearchResult[] GetSearchResult(MangaDexSearchResult searchResult)
{
List<MangaSearchResult> mangaSearchResults = [];
foreach (MangaDexSearchResultData searchResultData in searchResult.Data)
foreach (MangaDexEntity entity in collectionResponse.Data)
{
string title = searchResultData.Attributes.Title.FirstOrDefault().Value;
string slug = GenerateSlug(title);
MangaSearchResult? mangaSearchResult = GetMangaSearchResult(entity);
MangaSearchResult mangaSearchResult = new()
{
Title = title,
Url = $"https://mangadex.org/title/{searchResultData.Id}/{slug}",
Thumbnail = GetThumbnail(searchResultData)
};
if (mangaSearchResult == null)
continue;
mangaSearchResults.Add(mangaSearchResult);
}
return [.. mangaSearchResults];
}
private static MangaSearchResult? GetMangaSearchResult(MangaDexEntity entity)
{
if (entity is not MangaEntity mangaEntity)
return null;
if (mangaEntity.Attributes == null)
return null;
string title = mangaEntity.Attributes.Title.FirstOrDefault().Value;
string slug = GenerateSlug(title);
MangaSearchResult mangaSearchResult = new()
{
Title = title,
Url = $"https://mangadex.org/title/{mangaEntity.Id}/{slug}",
Thumbnail = GetThumbnail(mangaEntity)
};
return mangaSearchResult;
}
public static string GenerateSlug(string title)
{
// title.ToLowerInvariant().Normalize(NormalizationForm.FormD);
@@ -57,19 +70,19 @@ public partial class MangaDexSearchProvider(IHttpService httpService) : MangaSea
return title.Trim('-');
}
private static string? GetThumbnail(MangaDexSearchResultData searchResultData)
private static string? GetThumbnail(MangaDexEntity mangaDexEntity)
{
var coverArtRelationship = searchResultData.Relationships.FirstOrDefault(x => x.Type == "cover_art");
CoverArtEntity? coverArtEntity = (CoverArtEntity?)mangaDexEntity.Relationships.FirstOrDefault(entity =>
entity is CoverArtEntity);
if (coverArtRelationship == null)
if (coverArtEntity == null || string.IsNullOrWhiteSpace(coverArtEntity.Attributes?.FileName))
return null;
if (coverArtRelationship.Attributes.TryGetValue("fileName", out object? fileNameValue) == false)
string? fileName = coverArtEntity.Attributes?.FileName;
if (string.IsNullOrWhiteSpace(coverArtEntity.Attributes?.FileName))
return null;
if (fileNameValue == null)
return null;
return $"https://mangadex.org/covers/{searchResultData.Id}/{fileNameValue}";
return $"https://mangadex.org/covers/{mangaDexEntity.Id}/{fileName}";
}
}