66 lines
2.3 KiB
C#
66 lines
2.3 KiB
C#
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}";
|
|
}
|
|
} |