Added MangaDex Api. Updated project structure.
This commit is contained in:
@@ -2,5 +2,5 @@
|
||||
|
||||
public interface IMangaSearchCoordinator
|
||||
{
|
||||
Task<MangaSearchResult[]> SearchAsync(string keyword, CancellationToken cancellationToken);
|
||||
Task<Dictionary<string, MangaSearchResult[]>> SearchAsync(string keyword, CancellationToken cancellationToken);
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
using MangaReader.Core.HttpService;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace MangaReader.Core.Search.MangaDex;
|
||||
|
||||
public partial class MangaDexSearchProvider(IHttpService httpService) : MangaSearchProviderBase<MangaDexSearchResult>(httpService)
|
||||
{
|
||||
[GeneratedRegex(@"[^a-z0-9\s-]")]
|
||||
private static partial Regex InvalidSlugCharactersRegex();
|
||||
|
||||
[GeneratedRegex(@"\s+")]
|
||||
private static partial Regex WhitespaceRegex();
|
||||
|
||||
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 = 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}";
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
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; } = [];
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
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; } = [];
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
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; } = [];
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
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; } = [];
|
||||
}
|
||||
@@ -1,12 +1,25 @@
|
||||
namespace MangaReader.Core.Search;
|
||||
using MangaReader.Core.Sources;
|
||||
|
||||
namespace MangaReader.Core.Search;
|
||||
|
||||
public class MangaSearchCoordinator(IEnumerable<IMangaSearchProvider> searchProviders) : IMangaSearchCoordinator
|
||||
{
|
||||
public async Task<MangaSearchResult[]> SearchAsync(string keyword, CancellationToken cancellationToken)
|
||||
public async Task<Dictionary<string, MangaSearchResult[]>> SearchAsync(string keyword, CancellationToken cancellationToken)
|
||||
{
|
||||
var searchTasks = searchProviders.Select(searchProvider => searchProvider.SearchAsync(keyword, cancellationToken));
|
||||
var searchTasks = searchProviders.Select(searchProvider =>
|
||||
GetMangaSearchResultsAsync(searchProvider, keyword, cancellationToken));
|
||||
|
||||
var allResults = await Task.WhenAll(searchTasks);
|
||||
|
||||
return [.. allResults.SelectMany(mangaSearchResults => mangaSearchResults)];
|
||||
return allResults.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
|
||||
}
|
||||
|
||||
private static async Task<KeyValuePair<string, MangaSearchResult[]>> GetMangaSearchResultsAsync(
|
||||
IMangaSearchProvider searchProvider, string keyword, CancellationToken cancellationToken)
|
||||
{
|
||||
string sourceName = searchProvider is IMangaSourceComponent sourceComponent ? sourceComponent.SourceId : "Unknown";
|
||||
MangaSearchResult[] results = await searchProvider.SearchAsync(keyword, cancellationToken);
|
||||
|
||||
return new KeyValuePair<string, MangaSearchResult[]>(sourceName, results);
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,7 @@ public abstract class MangaSearchProviderBase<T>(IHttpService httpService) : IMa
|
||||
|
||||
public async Task<MangaSearchResult[]> SearchAsync(string keyword, CancellationToken cancellationToken)
|
||||
{
|
||||
T? searchResult = await GetSearchResultAsync(keyword);
|
||||
T? searchResult = await GetSearchResultAsync(keyword, cancellationToken);
|
||||
|
||||
if (searchResult == null)
|
||||
return [];
|
||||
@@ -20,10 +20,10 @@ public abstract class MangaSearchProviderBase<T>(IHttpService httpService) : IMa
|
||||
return GetSearchResult(searchResult);
|
||||
}
|
||||
|
||||
private async Task<T?> GetSearchResultAsync(string keyword)
|
||||
private async Task<T?> GetSearchResultAsync(string keyword, CancellationToken cancellationToken)
|
||||
{
|
||||
string url = GetSearchUrl(keyword);
|
||||
string response = await httpService.GetStringAsync(url);
|
||||
string response = await httpService.GetStringAsync(url, cancellationToken);
|
||||
|
||||
return JsonSerializer.Deserialize<T>(response, _jsonSerializerOptions);
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
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; }
|
||||
|
||||
@@ -1,57 +0,0 @@
|
||||
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)
|
||||
{
|
||||
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<MangaSearchResult> mangaSearchResults = searchResult.Select(searchResult =>
|
||||
new MangaSearchResult()
|
||||
{
|
||||
Source = "NatoManga",
|
||||
Title = searchResult.Name,
|
||||
Thumbnail = searchResult.Thumb,
|
||||
Url = searchResult.Url
|
||||
});
|
||||
|
||||
return [.. mangaSearchResults];
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
namespace MangaReader.Core.Search.NatoManga;
|
||||
|
||||
public record NatoMangaSearchResult
|
||||
{
|
||||
public int Id { get; init; }
|
||||
public string? Author { get; init; }
|
||||
public required string Name { get; init; }
|
||||
public string? ChapterLatest { get; init; }
|
||||
public required string Url { get; init; }
|
||||
public string? Thumb { get; init; }
|
||||
public string? Slug { get; init; }
|
||||
}
|
||||
Reference in New Issue
Block a user