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; }
}