Added contributor classes for manga. Implemented MangaDex search.

This commit is contained in:
2025-05-24 15:56:44 -04:00
parent f760cff21f
commit 1a752bb57e
20 changed files with 706 additions and 24 deletions

View File

@@ -0,0 +1,66 @@
using MangaReader.Core.HttpService;
using System.Text;
using System.Text.RegularExpressions;
namespace MangaReader.Core.Search.MangaDex;
public class MangaDexSearchProvider(IHttpService httpService) : MangaSearchProviderBase<MangaDexSearchResult>(httpService)
{
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()
{
Source = "MangaDex",
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 = Regex.Replace(title, @"[^a-z0-9\s-]", ""); // remove invalid chars
title = Regex.Replace(title, @"[^a-z0-9\s-]", "-"); // replace invalid chars with dash
title = Regex.Replace(title, @"\s+", "-"); // 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}";
}
}