Added contributor classes for manga. Implemented MangaDex search.
This commit is contained in:
66
MangaReader.Core/Search/MangaDex/MangaDexSearchProvider.cs
Normal file
66
MangaReader.Core/Search/MangaDex/MangaDexSearchProvider.cs
Normal 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}";
|
||||
}
|
||||
}
|
||||
8
MangaReader.Core/Search/MangaDex/MangaDexSearchResult.cs
Normal file
8
MangaReader.Core/Search/MangaDex/MangaDexSearchResult.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace MangaReader.Core.Search.MangaDex;
|
||||
|
||||
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.Search.MangaDex;
|
||||
|
||||
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.Search.MangaDex;
|
||||
|
||||
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.Search.MangaDex;
|
||||
|
||||
public class MangaDexSearchResultDataRelationship
|
||||
{
|
||||
public required Guid Id { get; set; }
|
||||
public required string Type { get; set; }
|
||||
public Dictionary<string, object> Attributes { get; set; } = [];
|
||||
}
|
||||
@@ -5,7 +5,7 @@ namespace MangaReader.Core.Search;
|
||||
|
||||
public abstract class MangaSearchProviderBase<T>(IHttpService httpService) : IMangaSearchProvider<T>
|
||||
{
|
||||
private static JsonSerializerOptions _jsonSerializerOptions = new()
|
||||
private static readonly JsonSerializerOptions _jsonSerializerOptions = new()
|
||||
{
|
||||
PropertyNameCaseInsensitive = true
|
||||
};
|
||||
|
||||
@@ -5,6 +5,7 @@ public record MangaSearchResult
|
||||
public required string Source { get; init; }
|
||||
public required string Url { get; init; }
|
||||
public required string Title { get; init; }
|
||||
public string? Thumbnail { get; init; }
|
||||
public string? Author { get; init; }
|
||||
public string? Description { get; init; }
|
||||
}
|
||||
@@ -1,14 +1,44 @@
|
||||
using MangaReader.Core.HttpService;
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace MangaReader.Core.Search.NatoManga;
|
||||
|
||||
public class NatoMangaSearchProvider(IHttpService httpService) : MangaSearchProviderBase<NatoMangaSearchResult[]>(httpService)
|
||||
{
|
||||
// https://www.natomanga.com/home/search/json?searchword=gal_can_t_be_kind
|
||||
|
||||
protected override string GetSearchUrl(string keyword)
|
||||
{
|
||||
return $"https://www.natomanga.com/home/search/json?searchword={keyword}";
|
||||
string formattedSeachWord = GetFormattedSearchWord(keyword);
|
||||
|
||||
return $"https://www.natomanga.com/home/search/json?searchword={formattedSeachWord}";
|
||||
}
|
||||
|
||||
private static string GetFormattedSearchWord(string input)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(input))
|
||||
return string.Empty;
|
||||
|
||||
// Convert to lowercase and normalize to decompose accents
|
||||
string normalized = input.ToLowerInvariant()
|
||||
.Normalize(NormalizationForm.FormD);
|
||||
|
||||
// Remove diacritics
|
||||
var sb = new StringBuilder();
|
||||
foreach (var c in normalized)
|
||||
{
|
||||
var unicodeCategory = CharUnicodeInfo.GetUnicodeCategory(c);
|
||||
if (unicodeCategory != UnicodeCategory.NonSpacingMark)
|
||||
sb.Append(c);
|
||||
}
|
||||
|
||||
// Replace non-alphanumeric characters with underscores
|
||||
string cleaned = Regex.Replace(sb.ToString(), @"[^a-z0-9]+", "_");
|
||||
|
||||
// Trim and collapse underscores
|
||||
cleaned = Regex.Replace(cleaned, "_{2,}", "_").Trim('_');
|
||||
|
||||
return cleaned;
|
||||
}
|
||||
|
||||
protected override MangaSearchResult[] GetSearchResult(NatoMangaSearchResult[] searchResult)
|
||||
@@ -18,6 +48,7 @@ public class NatoMangaSearchProvider(IHttpService httpService) : MangaSearchProv
|
||||
{
|
||||
Source = "NatoManga",
|
||||
Title = searchResult.Name,
|
||||
Thumbnail = searchResult.Thumb,
|
||||
Url = searchResult.Url
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user