Added MangaDex Api. Updated project structure.
This commit is contained in:
@@ -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