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(httpService) { protected override string GetSearchUrl(string 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) { IEnumerable mangaSearchResults = searchResult.Select(searchResult => new MangaSearchResult() { Source = "NatoManga", Title = searchResult.Name, Thumbnail = searchResult.Thumb, Url = searchResult.Url }); return [.. mangaSearchResults]; } }