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}";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user