Added contributor classes for manga. Implemented MangaDex search.

This commit is contained in:
2025-05-24 15:56:44 -04:00
parent f760cff21f
commit 1a752bb57e
20 changed files with 706 additions and 24 deletions

View File

@@ -0,0 +1,9 @@
namespace MangaReader.Core.Data;
public class Contributor
{
public int ContributorId { get; set; }
public required string Name { get; set; }
public ICollection<MangaContributor> MangaContributions { get; set; } = [];
}

View File

@@ -7,18 +7,10 @@ public class Manga
public required string Title { get; set; }
public string? Description { get; set; }
public virtual ICollection<MangaCover> Covers { get; set; }
public virtual ICollection<MangaTitle> Titles { get; set; }
public virtual ICollection<MangaSource> Sources { get; set; }
public virtual ICollection<MangaGenre> Genres { get; set; }
public virtual ICollection<MangaChapter> Chapters { get; set; }
public Manga()
{
Covers = new HashSet<MangaCover>();
Titles = new HashSet<MangaTitle>();
Sources = new HashSet<MangaSource>();
Genres = new HashSet<MangaGenre>();
Chapters = new HashSet<MangaChapter>();
}
public virtual ICollection<MangaCover> Covers { get; set; } = [];
public virtual ICollection<MangaTitle> Titles { get; set; } = [];
public virtual ICollection<MangaSource> Sources { get; set; } = [];
public virtual ICollection<MangaContributor> Contributors { get; set; } = [];
public virtual ICollection<MangaGenre> Genres { get; set; } = [];
public virtual ICollection<MangaChapter> Chapters { get; set; } = [];
}

View File

@@ -9,6 +9,8 @@ public class MangaContext(DbContextOptions options) : DbContext(options)
public DbSet<MangaTitle> MangaTitles { get; set; }
public DbSet<Source> Sources { get; set; }
public DbSet<MangaSource> MangaSources { get; set; }
public DbSet<Contributor> Contributors { get; set; }
public DbSet<MangaContributor> MangaContributors { get; set; }
public DbSet<Genre> Genres { get; set; }
public DbSet<MangaGenre> MangaGenres { get; set; }
public DbSet<MangaChapter> MangaChapters { get; set; }
@@ -24,6 +26,8 @@ public class MangaContext(DbContextOptions options) : DbContext(options)
ConfigureMangaTitle(modelBuilder);
ConfigureSource(modelBuilder);
ConfigureMangaSource(modelBuilder);
ConfigureContributor(modelBuilder);
ConfigureMangaContributor(modelBuilder);
ConfigureGenre(modelBuilder);
ConfigureMangaGenre(modelBuilder);
ConfigureMangaChapter(modelBuilder);
@@ -37,6 +41,10 @@ public class MangaContext(DbContextOptions options) : DbContext(options)
.Entity<Manga>()
.HasKey(x => x.MangaId);
modelBuilder.Entity<Manga>()
.HasIndex(x => x.Title)
.IsUnique();
modelBuilder.Entity<Manga>()
.HasIndex(x => x.Slug)
.IsUnique();
@@ -118,6 +126,32 @@ public class MangaContext(DbContextOptions options) : DbContext(options)
.OnDelete(DeleteBehavior.Cascade);
}
private static void ConfigureContributor(ModelBuilder modelBuilder)
{
modelBuilder
.Entity<Contributor>()
.HasKey(x => x.ContributorId);
modelBuilder
.Entity<Contributor>()
.HasIndex(x => x.Name)
.IsUnique(true);
}
private static void ConfigureMangaContributor(ModelBuilder modelBuilder)
{
modelBuilder
.Entity<MangaContributor>()
.HasKey(mc => new { mc.MangaId, mc.ContributorId, mc.Role });
modelBuilder
.Entity<MangaContributor>()
.HasOne(x => x.Manga)
.WithMany(x => x.Contributors)
.HasForeignKey(x => x.MangaId)
.OnDelete(DeleteBehavior.Cascade);
}
private static void ConfigureGenre(ModelBuilder modelBuilder)
{
modelBuilder

View File

@@ -0,0 +1,12 @@
namespace MangaReader.Core.Data;
public class MangaContributor
{
public int MangaId { get; set; }
public required Manga Manga { get; set; }
public int ContributorId { get; set; }
public required Contributor Contributor { get; set; }
public MangaContributorRole Role { get; set; }
}

View File

@@ -0,0 +1,7 @@
namespace MangaReader.Core.Data;
public enum MangaContributorRole
{
Author,
Artist
}

View File

@@ -12,7 +12,7 @@
</ItemGroup>
<ItemGroup>
<Folder Include="Search\MangaDex\" />
<Folder Include="Utilities\" />
</ItemGroup>
</Project>

View 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}";
}
}

View 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; } = [];
}

View File

@@ -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; } = [];
}

View File

@@ -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; } = [];
}

View File

@@ -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; } = [];
}

View File

@@ -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
};

View File

@@ -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; }
}

View File

@@ -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
});

View File

@@ -6,6 +6,7 @@ public class SourceManga
public string? Description { get; set; }
public List<string> AlternateTitles { get; set; } = [];
public List<string> Authors { get; set; } = [];
public List<string> Artists { get; set; } = [];
public MangaStatus Status { get; set; } = MangaStatus.Unknown;
public List<string> Genres { get; set; } = [];
public DateTime? UpdateDate { get; set; }