Added MangaDex Api. Updated project structure.
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
namespace MangaReader.Core.Sources.MangaDex.Api;
|
||||
|
||||
public class ArtistAttributes : PersonAttributes
|
||||
{
|
||||
|
||||
}
|
||||
6
MangaReader.Core/Sources/MangaDex/Api/ArtistEntity.cs
Normal file
6
MangaReader.Core/Sources/MangaDex/Api/ArtistEntity.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace MangaReader.Core.Sources.MangaDex.Api;
|
||||
|
||||
public class ArtistEntity : MangaDexEntity
|
||||
{
|
||||
public required ArtistAttributes Attributes { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace MangaReader.Core.Sources.MangaDex.Api;
|
||||
|
||||
public class AuthorAttributes : PersonAttributes
|
||||
{
|
||||
|
||||
}
|
||||
6
MangaReader.Core/Sources/MangaDex/Api/AuthorEntity.cs
Normal file
6
MangaReader.Core/Sources/MangaDex/Api/AuthorEntity.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace MangaReader.Core.Sources.MangaDex.Api;
|
||||
|
||||
public class AuthorEntity : MangaDexEntity
|
||||
{
|
||||
public required AuthorAttributes Attributes { get; set; }
|
||||
}
|
||||
@@ -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; }
|
||||
}
|
||||
6
MangaReader.Core/Sources/MangaDex/Api/ChapterEntity.cs
Normal file
6
MangaReader.Core/Sources/MangaDex/Api/ChapterEntity.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace MangaReader.Core.Sources.MangaDex.Api;
|
||||
|
||||
public class ChapterEntity : MangaDexEntity
|
||||
{
|
||||
public required ChapterAttributes Attributes { get; set; }
|
||||
}
|
||||
12
MangaReader.Core/Sources/MangaDex/Api/CoverArtAttributes.cs
Normal file
12
MangaReader.Core/Sources/MangaDex/Api/CoverArtAttributes.cs
Normal 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; }
|
||||
}
|
||||
6
MangaReader.Core/Sources/MangaDex/Api/CoverArtEntity.cs
Normal file
6
MangaReader.Core/Sources/MangaDex/Api/CoverArtEntity.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace MangaReader.Core.Sources.MangaDex.Api;
|
||||
|
||||
public class CoverArtEntity : MangaDexEntity
|
||||
{
|
||||
public required CoverArtAttributes Attributes { get; set; }
|
||||
}
|
||||
6
MangaReader.Core/Sources/MangaDex/Api/CreatorEntity.cs
Normal file
6
MangaReader.Core/Sources/MangaDex/Api/CreatorEntity.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace MangaReader.Core.Sources.MangaDex.Api;
|
||||
|
||||
public class CreatorEntity : MangaDexEntity
|
||||
{
|
||||
|
||||
}
|
||||
7
MangaReader.Core/Sources/MangaDex/Api/IMangaDexClient.cs
Normal file
7
MangaReader.Core/Sources/MangaDex/Api/IMangaDexClient.cs
Normal 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);
|
||||
}
|
||||
9
MangaReader.Core/Sources/MangaDex/Api/MangaAttributes.cs
Normal file
9
MangaReader.Core/Sources/MangaDex/Api/MangaAttributes.cs
Normal 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; } = [];
|
||||
}
|
||||
39
MangaReader.Core/Sources/MangaDex/Api/MangaDexClient.cs
Normal file
39
MangaReader.Core/Sources/MangaDex/Api/MangaDexClient.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace MangaReader.Core.Sources.MangaDex.Api;
|
||||
|
||||
public class MangaDexCollectionResponse : MangaDexResponse
|
||||
{
|
||||
public List<MangaDexEntity> Data { get; set; } = [];
|
||||
}
|
||||
8
MangaReader.Core/Sources/MangaDex/Api/MangaDexEntity.cs
Normal file
8
MangaReader.Core/Sources/MangaDex/Api/MangaDexEntity.cs
Normal 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; } = [];
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace MangaReader.Core.Sources.MangaDex.Api;
|
||||
|
||||
public class MangaDexEntityResponse : MangaDexResponse
|
||||
{
|
||||
public MangaDexEntity? Data { get; set; }
|
||||
}
|
||||
@@ -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; }
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
6
MangaReader.Core/Sources/MangaDex/Api/MangaEntity.cs
Normal file
6
MangaReader.Core/Sources/MangaDex/Api/MangaEntity.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace MangaReader.Core.Sources.MangaDex.Api;
|
||||
|
||||
public class MangaEntity : MangaDexEntity
|
||||
{
|
||||
public required MangaAttributes Attributes { get; set; }
|
||||
}
|
||||
24
MangaReader.Core/Sources/MangaDex/Api/PersonAttributes.cs
Normal file
24
MangaReader.Core/Sources/MangaDex/Api/PersonAttributes.cs
Normal 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; }
|
||||
}
|
||||
@@ -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...
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace MangaReader.Core.Sources.MangaDex.Api;
|
||||
|
||||
public class ScanlationGroupEntity : MangaDexEntity
|
||||
{
|
||||
public required ScanlationGroupAttributes Attributes { get; set; }
|
||||
}
|
||||
9
MangaReader.Core/Sources/MangaDex/Api/TagAttributes.cs
Normal file
9
MangaReader.Core/Sources/MangaDex/Api/TagAttributes.cs
Normal 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; }
|
||||
}
|
||||
6
MangaReader.Core/Sources/MangaDex/Api/TagEntity.cs
Normal file
6
MangaReader.Core/Sources/MangaDex/Api/TagEntity.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace MangaReader.Core.Sources.MangaDex.Api;
|
||||
|
||||
public class TagEntity : MangaDexEntity
|
||||
{
|
||||
public required TagAttributes Attributes { get; set; }
|
||||
}
|
||||
@@ -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");
|
||||
// }
|
||||
//}
|
||||
@@ -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}";
|
||||
}
|
||||
}
|
||||
@@ -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; } = [];
|
||||
}
|
||||
@@ -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; } = [];
|
||||
}
|
||||
@@ -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; } = [];
|
||||
}
|
||||
@@ -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; } = [];
|
||||
}
|
||||
Reference in New Issue
Block a user