Project restructuring.
This commit is contained in:
@@ -1,12 +1,10 @@
|
||||
using MangaReader.Core.HttpService;
|
||||
using MangaReader.Core.Sources;
|
||||
using MangaReader.Core.Sources.MangaDex.Search;
|
||||
using System.Text;
|
||||
using MangaReader.Core.Search;
|
||||
using MangaReader.Core.Sources.MangaDex.Api;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace MangaReader.Core.Search.MangaDex;
|
||||
namespace MangaReader.Core.Sources.MangaDex.Search;
|
||||
|
||||
public partial class MangaDexSearchProvider(IHttpService httpService) : MangaSearchProviderBase<MangaDexSearchResult>(httpService), IMangaSourceComponent
|
||||
public partial class MangaDexSearchProvider(IMangaDexClient mangaDexClient) : IMangaSearchProvider, IMangaSourceComponent
|
||||
{
|
||||
[GeneratedRegex(@"[^a-z0-9\s-]")]
|
||||
private static partial Regex InvalidSlugCharactersRegex();
|
||||
@@ -16,35 +14,50 @@ public partial class MangaDexSearchProvider(IHttpService httpService) : MangaSea
|
||||
|
||||
public string SourceId => "MangaDex";
|
||||
|
||||
protected override string GetSearchUrl(string keyword)
|
||||
public async Task<MangaSearchResult[]> SearchAsync(string keyword, CancellationToken cancellationToken)
|
||||
{
|
||||
string normalizedKeyword = keyword.ToLowerInvariant().Normalize(NormalizationForm.FormD);
|
||||
MangaDexResponse? response = await mangaDexClient.SearchMangaByTitleAsync(keyword, cancellationToken);
|
||||
|
||||
return $"https://api.mangadex.org/manga?title={normalizedKeyword}&limit=5";
|
||||
}
|
||||
if (response == null || (response is not MangaDexCollectionResponse collectionResponse))
|
||||
return [];
|
||||
|
||||
protected override MangaSearchResult[] GetSearchResult(MangaDexSearchResult searchResult)
|
||||
{
|
||||
List<MangaSearchResult> mangaSearchResults = [];
|
||||
|
||||
foreach (MangaDexSearchResultData searchResultData in searchResult.Data)
|
||||
foreach (MangaDexEntity entity in collectionResponse.Data)
|
||||
{
|
||||
string title = searchResultData.Attributes.Title.FirstOrDefault().Value;
|
||||
string slug = GenerateSlug(title);
|
||||
MangaSearchResult? mangaSearchResult = GetMangaSearchResult(entity);
|
||||
|
||||
MangaSearchResult mangaSearchResult = new()
|
||||
{
|
||||
Title = title,
|
||||
Url = $"https://mangadex.org/title/{searchResultData.Id}/{slug}",
|
||||
Thumbnail = GetThumbnail(searchResultData)
|
||||
};
|
||||
if (mangaSearchResult == null)
|
||||
continue;
|
||||
|
||||
mangaSearchResults.Add(mangaSearchResult);
|
||||
|
||||
}
|
||||
|
||||
return [.. mangaSearchResults];
|
||||
}
|
||||
|
||||
private static MangaSearchResult? GetMangaSearchResult(MangaDexEntity entity)
|
||||
{
|
||||
if (entity is not MangaEntity mangaEntity)
|
||||
return null;
|
||||
|
||||
if (mangaEntity.Attributes == null)
|
||||
return null;
|
||||
|
||||
string title = mangaEntity.Attributes.Title.FirstOrDefault().Value;
|
||||
string slug = GenerateSlug(title);
|
||||
|
||||
MangaSearchResult mangaSearchResult = new()
|
||||
{
|
||||
Title = title,
|
||||
Url = $"https://mangadex.org/title/{mangaEntity.Id}/{slug}",
|
||||
Thumbnail = GetThumbnail(mangaEntity)
|
||||
};
|
||||
|
||||
return mangaSearchResult;
|
||||
}
|
||||
|
||||
public static string GenerateSlug(string title)
|
||||
{
|
||||
// title.ToLowerInvariant().Normalize(NormalizationForm.FormD);
|
||||
@@ -57,19 +70,19 @@ public partial class MangaDexSearchProvider(IHttpService httpService) : MangaSea
|
||||
return title.Trim('-');
|
||||
}
|
||||
|
||||
private static string? GetThumbnail(MangaDexSearchResultData searchResultData)
|
||||
private static string? GetThumbnail(MangaDexEntity mangaDexEntity)
|
||||
{
|
||||
var coverArtRelationship = searchResultData.Relationships.FirstOrDefault(x => x.Type == "cover_art");
|
||||
CoverArtEntity? coverArtEntity = (CoverArtEntity?)mangaDexEntity.Relationships.FirstOrDefault(entity =>
|
||||
entity is CoverArtEntity);
|
||||
|
||||
if (coverArtRelationship == null)
|
||||
if (coverArtEntity == null || string.IsNullOrWhiteSpace(coverArtEntity.Attributes?.FileName))
|
||||
return null;
|
||||
|
||||
if (coverArtRelationship.Attributes.TryGetValue("fileName", out object? fileNameValue) == false)
|
||||
string? fileName = coverArtEntity.Attributes?.FileName;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(coverArtEntity.Attributes?.FileName))
|
||||
return null;
|
||||
|
||||
if (fileNameValue == null)
|
||||
return null;
|
||||
|
||||
return $"https://mangadex.org/covers/{searchResultData.Id}/{fileNameValue}";
|
||||
return $"https://mangadex.org/covers/{mangaDexEntity.Id}/{fileName}";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user