Catching actor association errors so it does not inhibit media association.

This commit is contained in:
DebaucheryLibrarian 2021-02-23 04:09:33 +01:00
parent f44cb8bf4c
commit fae288633c
1 changed files with 45 additions and 39 deletions

View File

@ -891,55 +891,61 @@ async function getOrCreateActors(baseActors, batchId) {
} }
async function associateActors(releases, batchId) { async function associateActors(releases, batchId) {
const baseActorsByReleaseId = releases.reduce((acc, release) => { try {
if (release.actors) { const baseActorsByReleaseId = releases.reduce((acc, release) => {
acc[release.id] = toBaseActors(release.actors, release); if (release.actors) {
acc[release.id] = toBaseActors(release.actors, release);
}
return acc;
}, {});
const baseActors = Object.values(baseActorsByReleaseId).flat();
if (baseActors.length === 0) {
return [];
} }
return acc; const baseActorsBySlug = baseActors.reduce((acc, baseActor) => ({
}, {}); ...acc,
[baseActor.slug]: baseActor,
}), {});
const baseActors = Object.values(baseActorsByReleaseId).flat(); const uniqueBaseActors = Object.values(baseActorsBySlug);
const actors = await getOrCreateActors(uniqueBaseActors, batchId);
if (baseActors.length === 0) { const actorIdsByEntityIdEntryIdAndSlug = actors.reduce((acc, actor) => ({
return []; ...acc,
} [actor.entity_id]: {
...acc[actor.entity_id],
const baseActorsBySlug = baseActors.reduce((acc, baseActor) => ({ [actor.entry_id]: {
...acc, ...acc[actor.entity_id]?.[actor.entry_id],
[baseActor.slug]: baseActor, [actor.slug]: {
}), {}); actor_id: actor.alias_for || actor.id,
alias_id: actor.alias_for ? actor.id : null,
const uniqueBaseActors = Object.values(baseActorsBySlug); },
const actors = await getOrCreateActors(uniqueBaseActors, batchId);
const actorIdsByEntityIdEntryIdAndSlug = actors.reduce((acc, actor) => ({
...acc,
[actor.entity_id]: {
...acc[actor.entity_id],
[actor.entry_id]: {
...acc[actor.entity_id]?.[actor.entry_id],
[actor.slug]: {
actor_id: actor.alias_for || actor.id,
alias_id: actor.alias_for ? actor.id : null,
}, },
}, },
}, }), {});
}), {});
const releaseActorAssociations = Object.entries(baseActorsByReleaseId) const releaseActorAssociations = Object.entries(baseActorsByReleaseId)
.map(([releaseId, releaseActors]) => releaseActors .map(([releaseId, releaseActors]) => releaseActors
.map(releaseActor => ({ .map(releaseActor => ({
release_id: releaseId, release_id: releaseId,
...(actorIdsByEntityIdEntryIdAndSlug[releaseActor.entity?.id]?.[releaseActor.entryId]?.[releaseActor.slug] || actorIdsByEntityIdEntryIdAndSlug.null.null[releaseActor.slug]), ...(actorIdsByEntityIdEntryIdAndSlug[releaseActor.entity?.id]?.[releaseActor.entryId]?.[releaseActor.slug] || actorIdsByEntityIdEntryIdAndSlug.null.null[releaseActor.slug]),
}))) })))
.flat(); .flat();
await bulkInsert('releases_actors', releaseActorAssociations, false); await bulkInsert('releases_actors', releaseActorAssociations, false);
logger.verbose(`Associated ${releaseActorAssociations.length} actors to ${releases.length} scenes`); logger.verbose(`Associated ${releaseActorAssociations.length} actors to ${releases.length} scenes`);
return actors; return actors;
} catch (error) {
logger.error(`Failed to associate actors: ${error.message}`);
return [];
}
} }
async function fetchActor(actorId) { async function fetchActor(actorId) {