Files
jsmr/JSMR.Application/Scanning/ScanVoiceWorksHandler.cs
Brian Bicknell 62c2efab01
All checks were successful
ci / build-test (push) Successful in 2m18s
ci / publish-image (push) Has been skipped
Updated scanner to infer when it has reached the end of results.
2026-03-07 01:26:04 -05:00

64 lines
2.5 KiB
C#

using JSMR.Application.Common.Caching;
using JSMR.Application.Integrations.DLSite.Models;
using JSMR.Application.Integrations.Ports;
using JSMR.Application.Scanning.Contracts;
using JSMR.Application.Scanning.Ports;
namespace JSMR.Application.Scanning;
public sealed class ScanVoiceWorksHandler(
IVoiceWorkScannerRepository scannerRepository,
IVoiceWorkUpdaterRepository updaterRepository,
IDLSiteClient dlsiteClient,
ISpamCircleCache spamCircleCache,
IVoiceWorkSearchUpdater searchUpdater)
{
public async Task<ScanVoiceWorksResponse> HandleAsync(ScanVoiceWorksRequest request, CancellationToken cancellationToken)
{
IVoiceWorksScanner? scanner = scannerRepository.GetScanner(request.Locale);
IVoiceWorkUpdater? updater = updaterRepository.GetUpdater(request.Locale);
if (scanner is null)
throw new InvalidOperationException($"No scanner registered for locale {request.Locale}.");
if (updater is null)
throw new InvalidOperationException($"No updater registered for locale {request.Locale}.");
VoiceWorkScanOptions options = new(
PageNumber: request.PageNumber,
PageSize: request.PageSize,
ExcludedMakerIds: await spamCircleCache.GetAsync(cancellationToken),
ExcludePartiallyAIGeneratedWorks: true,
ExcludeAIGeneratedWorks: true
);
VoiceWorkScanResult scanResult = await scanner.ScanPageAsync(options, cancellationToken);
if (scanResult.EndOfResults)
{
return new ScanVoiceWorksResponse(
Results: [],
EndOfResults: true
);
}
string[] productIds = [.. scanResult.Works.Where(x => !string.IsNullOrWhiteSpace(x.ProductId)).Select(x => x.ProductId!)];
VoiceWorkDetailCollection voiceWorkDetails = await dlsiteClient.GetVoiceWorkDetailsAsync(productIds, cancellationToken);
VoiceWorkIngest[] ingests = [.. scanResult.Works.Select(work =>
{
voiceWorkDetails.TryGetValue(work.ProductId!, out VoiceWorkDetails? value);
return VoiceWorkIngest.From(work, value);
})];
VoiceWorkUpsertResult[] upsertResults = await updater.UpsertAsync(ingests, cancellationToken);
int[] voiceWorkIds = [.. upsertResults.Where(x => x.VoiceWorkId.HasValue).Select(x => x.VoiceWorkId!.Value)];
await searchUpdater.UpdateAsync(voiceWorkIds, cancellationToken);
return new ScanVoiceWorksResponse(
Results: upsertResults,
EndOfResults: false
);
}
}