From c51cd080fa60d605901e54082be3816d57a4443a Mon Sep 17 00:00:00 2001 From: DebaucheryLibrarian Date: Wed, 17 Feb 2021 00:40:20 +0100 Subject: [PATCH] Improved actor mapping in release associations. Storing alias ID in actor release association. --- migrations/20190325001339_releases.js | 5 +++++ src/actors.js | 18 +++++++++++++++++- src/scrapers/traxxx.js | 9 ++++++--- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/migrations/20190325001339_releases.js b/migrations/20190325001339_releases.js index 405c2887..ab5d53a9 100644 --- a/migrations/20190325001339_releases.js +++ b/migrations/20190325001339_releases.js @@ -682,6 +682,11 @@ exports.up = knex => Promise.resolve() .inTable('actors') .onDelete('cascade'); + table.integer('alias_id', 12) + .references('id') + .inTable('actors') + .onDelete('cascade'); + table.unique(['release_id', 'actor_id']); table.datetime('created_at') diff --git a/src/actors.js b/src/actors.js index c38394e1..3f5146cc 100644 --- a/src/actors.js +++ b/src/actors.js @@ -912,16 +912,32 @@ async function associateActors(releases, batchId) { const actors = await getOrCreateActors(uniqueBaseActors, batchId); + /* const actorIdsBySlug = actors.reduce((acc, actor) => ({ ...acc, [actor.slug]: actor.alias_for || actor.id, }), {}); + */ + + 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) .map(([releaseId, releaseActors]) => releaseActors .map(releaseActor => ({ release_id: releaseId, - actor_id: actorIdsBySlug[releaseActor.slug], + ...(actorIdsByEntityIdEntryIdAndSlug[releaseActor.entity?.id]?.[releaseActor.entryId]?.[releaseActor.slug] || actorIdsByEntityIdEntryIdAndSlug.null.null[releaseActor.slug]), }))) .flat(); diff --git a/src/scrapers/traxxx.js b/src/scrapers/traxxx.js index 60270ed6..3016f1ac 100644 --- a/src/scrapers/traxxx.js +++ b/src/scrapers/traxxx.js @@ -222,7 +222,11 @@ function actors(release) { : Math.floor(Math.random() * 3) + 2; return Array.from({ length }, () => ({ - name: faker.name.findName(), + name: faker.name + .findName() + .split(' ') + .slice(0, Math.random() < 0.2 ? 1 : 2) // sometimes only use the first name + .join(' '), gender: gender(), })); } @@ -258,8 +262,7 @@ async function fetchLatest(entity, page, options) { .limit(faker.random.number({ min: 2, max: 15 })) .pluck('name'); - // release.actors = [...actors(release), null]; // include empty actor to ensure proper handling - release.actors = ['Amber']; + release.actors = [...actors(release), null]; // include empty actor to ensure proper handling release.title = title(release); return release;