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