More updates.

This commit is contained in:
2025-06-25 10:40:03 -04:00
parent a82eab0ecb
commit 33e521e8bb
28 changed files with 334 additions and 63 deletions

View File

@@ -13,7 +13,7 @@ public partial class MangaPipeline(MangaContext context) : IMangaPipeline
Secondary
}
public async Task RunMetadataAsync(MangaMetadataPipelineRequest request)
public async Task RunMetadataAsync(MangaMetadataPipelineRequest request, CancellationToken cancellationToken)
{
string sourceName = request.SourceName;
string sourceUrl = request.SourceUrl;
@@ -24,7 +24,7 @@ public partial class MangaPipeline(MangaContext context) : IMangaPipeline
MangaSource mangaSource = await AddMangaSourceAsync(sourceUrl, manga, source);
await AddTitleAsync(manga, sourceManga.Title, TitleType.Primary);
await AddDescriptionAsync(manga, sourceManga.Description);
await AddDescriptionAsync(mangaSource, sourceManga.Description);
foreach (SourceMangaTitle alternateTitle in sourceManga.AlternateTitles)
{
@@ -36,6 +36,11 @@ public partial class MangaPipeline(MangaContext context) : IMangaPipeline
await LinkGenreAsync(manga, genre);
}
foreach (SourceMangaContributor contributor in sourceManga.Contributors)
{
await LinkMangaContributorAsync(manga, contributor);
}
foreach (SourceMangaChapter chapter in sourceManga.Chapters)
{
await AddChapterAsync(mangaSource, chapter);
@@ -133,20 +138,23 @@ public partial class MangaPipeline(MangaContext context) : IMangaPipeline
context.MangaTitles.Add(mangaTitle);
}
private async Task AddDescriptionAsync(Manga manga, SourceMangaDescription? sourceMangaDescription)
private async Task AddDescriptionAsync(MangaSource mangaSource, SourceMangaDescription? sourceMangaDescription)
{
if (sourceMangaDescription == null)
return;
MangaDescription? mangaDescription = await context.MangaDescriptions.FirstOrDefaultAsync(md =>
md.Manga == manga && md.Name == sourceMangaDescription.Name);
md.MangaSource == mangaSource && md.Language == sourceMangaDescription.Language);
if (mangaDescription != null)
{
mangaDescription.Name = sourceMangaDescription.Name;
return;
}
mangaDescription = new()
{
Manga = manga,
MangaSource = mangaSource,
Name = sourceMangaDescription.Name,
Language = sourceMangaDescription.Language
};
@@ -189,6 +197,51 @@ public partial class MangaPipeline(MangaContext context) : IMangaPipeline
return genre;
}
private async Task LinkMangaContributorAsync(Manga manga, SourceMangaContributor sourceMangaContributor)
{
Contributor contributor = await GetOrAddContributorAsync(sourceMangaContributor.Name);
MangaContributor? mangaContributor = await context.MangaContributors.FirstOrDefaultAsync(x =>
x.Manga == manga && x.Contributor == contributor && x.Role == sourceMangaContributor.Role);
if (mangaContributor != null)
return;
mangaContributor = new()
{
Manga = manga,
Contributor = contributor,
Role = sourceMangaContributor.Role
};
context.MangaContributors.Add(mangaContributor);
}
private async Task<Contributor> GetOrAddContributorAsync(string contributorName)
{
Contributor? trackedContributor = context.ChangeTracker
.Entries<Contributor>()
.Select(e => e.Entity)
.FirstOrDefault(c => c.Name == contributorName);
if (trackedContributor is not null)
return trackedContributor;
Contributor? contributor = await context.Contributors.FirstOrDefaultAsync(x => x.Name == contributorName);
if (contributor == null)
{
contributor = new()
{
Name = contributorName,
};
await context.Contributors.AddAsync(contributor);
}
return contributor;
}
private async Task AddChapterAsync(MangaSource mangaSource, SourceMangaChapter sourceMangaChapter)
{
SourceChapter sourceChapter = await GetSourceChapter(mangaSource, sourceMangaChapter)
@@ -225,9 +278,9 @@ public partial class MangaPipeline(MangaContext context) : IMangaPipeline
return sourceChapter;
}
public async Task RunPagesAsync(MangaPagePipelineRequest request)
public async Task RunPagesAsync(MangaPagePipelineRequest request, CancellationToken cancellationToken)
{
SourceChapter? sourceChapter = await context.SourceChapters.FirstOrDefaultAsync(x => x.SourceChapterId == request.SourceChapterId);
SourceChapter? sourceChapter = await context.SourceChapters.FirstOrDefaultAsync(x => x.SourceChapterId == request.SourceChapterId, cancellationToken);
if (sourceChapter == null)
return;
@@ -236,14 +289,14 @@ public partial class MangaPipeline(MangaContext context) : IMangaPipeline
foreach (string pageImageUrl in request.PageImageUrls)
{
await AddOrUpdateSourcePageAsync(sourceChapter, currentPageNumber++, pageImageUrl);
await AddOrUpdateSourcePageAsync(sourceChapter, currentPageNumber++, pageImageUrl, cancellationToken);
}
}
private async Task AddOrUpdateSourcePageAsync(SourceChapter sourceChapter, int pageNumber, string pageImageUrl)
private async Task AddOrUpdateSourcePageAsync(SourceChapter sourceChapter, int pageNumber, string pageImageUrl, CancellationToken cancellationToken)
{
SourcePage? sourcePage = await context.SourcePages.FirstOrDefaultAsync(x =>
x.Chapter == sourceChapter && x.PageNumber == pageNumber);
x.Chapter == sourceChapter && x.PageNumber == pageNumber, cancellationToken);
if (sourcePage == null)
{
@@ -254,11 +307,11 @@ public partial class MangaPipeline(MangaContext context) : IMangaPipeline
Url = pageImageUrl
};
context.SourcePages.Add(sourcePage);
await context.SourcePages.AddAsync(sourcePage, cancellationToken);
}
else
{
sourcePage.Url = pageImageUrl;
}
}
}
}