2020-03-16 03:10:52 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const config = require('config');
|
|
|
|
|
2020-05-17 02:59:09 +00:00
|
|
|
const argv = require('./argv');
|
2020-03-21 01:48:24 +00:00
|
|
|
const logger = require('./logger')(__filename);
|
2020-03-16 03:10:52 +00:00
|
|
|
const knex = require('./knex');
|
|
|
|
const slugify = require('./utils/slugify');
|
2020-07-08 00:53:46 +00:00
|
|
|
const { formatDate } = require('./utils/qu');
|
2020-05-16 02:36:45 +00:00
|
|
|
const { associateActors, scrapeActors } = require('./actors');
|
2020-03-29 02:00:46 +00:00
|
|
|
const { associateReleaseTags } = require('./tags');
|
2020-06-25 00:26:25 +00:00
|
|
|
const { curateEntity } = require('./entities');
|
2020-03-29 02:00:46 +00:00
|
|
|
const { associateReleaseMedia } = require('./media');
|
2020-03-16 03:10:52 +00:00
|
|
|
|
2020-08-01 13:11:07 +00:00
|
|
|
function curateReleaseEntry(release, batchId, existingRelease, type = 'scene') {
|
2020-07-03 02:12:56 +00:00
|
|
|
const slugBase = release.title
|
|
|
|
|| (release.actors?.length && `${release.entity.slug} ${release.actors.map(actor => actor.name).join(' ')}`)
|
2020-07-08 00:53:46 +00:00
|
|
|
|| (release.date && `${release.entity.slug} ${formatDate(release.date, 'YYYY MM DD')}`)
|
2020-07-03 02:12:56 +00:00
|
|
|
|| null;
|
|
|
|
|
|
|
|
const slug = slugify(slugBase, '-', {
|
2020-05-14 02:26:05 +00:00
|
|
|
encode: true,
|
|
|
|
limit: config.titleSlugLength,
|
|
|
|
});
|
|
|
|
|
|
|
|
const curatedRelease = {
|
|
|
|
title: release.title,
|
|
|
|
entry_id: release.entryId || null,
|
2020-06-25 00:26:25 +00:00
|
|
|
entity_id: release.entity.id,
|
2020-05-14 02:26:05 +00:00
|
|
|
studio_id: release.studio?.id || null,
|
|
|
|
url: release.url,
|
2020-05-26 02:11:29 +00:00
|
|
|
date: Number(release.date) ? release.date : null,
|
2020-07-03 02:12:56 +00:00
|
|
|
date_precision: release.datePrecision,
|
2020-05-14 02:26:05 +00:00
|
|
|
slug,
|
|
|
|
description: release.description,
|
2020-07-08 00:53:46 +00:00
|
|
|
comment: release.comment,
|
2020-05-14 02:26:05 +00:00
|
|
|
// director: release.director,
|
|
|
|
// likes: release.rating && release.rating.likes,
|
|
|
|
// dislikes: release.rating && release.rating.dislikes,
|
|
|
|
// rating: release.rating && release.rating.stars && Math.floor(release.rating.stars),
|
|
|
|
deep: typeof release.deep === 'boolean' ? release.deep : false,
|
|
|
|
deep_url: release.deepUrl,
|
|
|
|
updated_batch_id: batchId,
|
|
|
|
};
|
|
|
|
|
2020-08-01 13:11:07 +00:00
|
|
|
if (type === 'scene') {
|
|
|
|
curatedRelease.shoot_id = release.shootId || null;
|
2020-08-02 01:44:14 +00:00
|
|
|
curatedRelease.production_date = Number(release.productionDate) ? release.productionDate : null;
|
2020-08-01 13:11:07 +00:00
|
|
|
curatedRelease.duration = release.duration;
|
|
|
|
}
|
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
if (!existingRelease && !release.id) {
|
|
|
|
curatedRelease.created_batch_id = batchId;
|
|
|
|
}
|
|
|
|
|
|
|
|
return curatedRelease;
|
2020-03-16 03:10:52 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 00:26:25 +00:00
|
|
|
async function attachChannelEntities(releases) {
|
2020-07-01 02:47:05 +00:00
|
|
|
const releasesWithoutEntity = releases.filter(release => release.channel && (!release.entity || release.entity.type === 'network'));
|
2020-03-16 03:10:52 +00:00
|
|
|
|
2020-06-25 00:26:25 +00:00
|
|
|
const channelEntities = await knex('entities')
|
|
|
|
.select(knex.raw('entities.*, row_to_json(parents) as parent'))
|
|
|
|
.whereIn('entities.slug', releasesWithoutEntity.map(release => release.channel))
|
2020-06-30 22:25:27 +00:00
|
|
|
.where('entities.type', 'channel')
|
2020-06-25 00:26:25 +00:00
|
|
|
.leftJoin('entities AS parents', 'parents.id', 'entities.parent_id');
|
2020-03-30 01:01:08 +00:00
|
|
|
|
2020-06-25 00:26:25 +00:00
|
|
|
const channelEntitiesBySlug = channelEntities.reduce((acc, entity) => ({ ...acc, [entity.slug]: entity }), {});
|
2020-03-16 23:58:03 +00:00
|
|
|
|
2020-06-25 00:26:25 +00:00
|
|
|
const releasesWithChannelEntity = await Promise.all(releases
|
2020-05-14 02:26:05 +00:00
|
|
|
.map(async (release) => {
|
2020-06-25 00:26:25 +00:00
|
|
|
if (release.channel && channelEntitiesBySlug[release.channel]) {
|
|
|
|
const curatedEntity = await curateEntity(channelEntitiesBySlug[release.channel]);
|
2020-03-30 01:01:08 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
return {
|
|
|
|
...release,
|
2020-06-25 00:26:25 +00:00
|
|
|
entity: curatedEntity,
|
2020-05-14 02:26:05 +00:00
|
|
|
};
|
|
|
|
}
|
2020-03-16 23:58:03 +00:00
|
|
|
|
2020-06-25 00:26:25 +00:00
|
|
|
if (release.entity) {
|
2020-05-26 02:11:29 +00:00
|
|
|
return release;
|
|
|
|
}
|
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
logger.error(`Unable to match channel '${release.channel?.slug || release.channel}' from generic URL ${release.url}`);
|
2020-03-16 23:58:03 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
return null;
|
|
|
|
}));
|
|
|
|
|
2020-06-25 00:26:25 +00:00
|
|
|
return releasesWithChannelEntity.filter(Boolean);
|
2020-03-16 23:58:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function attachStudios(releases) {
|
2020-05-14 02:26:05 +00:00
|
|
|
const studioSlugs = releases.map(release => release.studio).filter(Boolean);
|
2020-03-16 23:58:03 +00:00
|
|
|
|
2020-06-25 00:26:25 +00:00
|
|
|
const studios = await knex('entities')
|
|
|
|
.whereIn('slug', studioSlugs)
|
2020-06-30 22:25:27 +00:00
|
|
|
.where('type', 'studio');
|
2020-06-25 00:26:25 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
const studioBySlug = studios.reduce((acc, studio) => ({ ...acc, [studio.slug]: studio }), {});
|
2020-03-16 23:58:03 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
const releasesWithStudio = releases.map((release) => {
|
|
|
|
if (release.studio && studioBySlug[release.studio]) {
|
|
|
|
return {
|
|
|
|
...release,
|
|
|
|
studio: studioBySlug[release.studio],
|
|
|
|
};
|
|
|
|
}
|
2020-03-16 23:58:03 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
if (release.studio) {
|
|
|
|
logger.warn(`Unable to match studio '${release.studio}' for ${release.url}`);
|
|
|
|
}
|
2020-03-16 23:58:03 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
return release;
|
|
|
|
});
|
2020-03-16 23:58:03 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
return releasesWithStudio;
|
2020-03-16 03:10:52 +00:00
|
|
|
}
|
|
|
|
|
2020-03-22 02:50:24 +00:00
|
|
|
function attachReleaseIds(releases, storedReleases) {
|
2020-06-25 00:26:25 +00:00
|
|
|
const storedReleaseIdsByEntityIdAndEntryId = storedReleases.reduce((acc, release) => {
|
2020-06-17 02:07:24 +00:00
|
|
|
if (!acc[release.entity_id]) acc[release.entity_id] = {};
|
|
|
|
acc[release.entity_id][release.entry_id] = release.id;
|
2020-03-22 02:50:24 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
return acc;
|
|
|
|
}, {});
|
2020-03-22 02:50:24 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
const releasesWithId = releases.map(release => ({
|
|
|
|
...release,
|
2020-06-25 00:26:25 +00:00
|
|
|
id: storedReleaseIdsByEntityIdAndEntryId[release.entity.id][release.entryId],
|
2020-05-14 02:26:05 +00:00
|
|
|
}));
|
2020-03-22 02:50:24 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
return releasesWithId;
|
2020-03-22 02:50:24 +00:00
|
|
|
}
|
|
|
|
|
2020-03-28 03:37:04 +00:00
|
|
|
function filterInternalDuplicateReleases(releases) {
|
2020-06-25 00:26:25 +00:00
|
|
|
const releasesByEntityIdAndEntryId = releases.reduce((acc, release) => {
|
|
|
|
if (!release.entity) {
|
2020-05-26 02:11:29 +00:00
|
|
|
return acc;
|
|
|
|
}
|
|
|
|
|
2020-06-25 00:26:25 +00:00
|
|
|
if (!acc[release.entity.id]) {
|
|
|
|
acc[release.entity.id] = {};
|
2020-05-14 02:26:05 +00:00
|
|
|
}
|
2020-03-28 03:37:04 +00:00
|
|
|
|
2020-06-25 00:26:25 +00:00
|
|
|
acc[release.entity.id][release.entryId] = release;
|
2020-03-28 03:37:04 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
return acc;
|
|
|
|
}, {});
|
2020-03-28 03:37:04 +00:00
|
|
|
|
2020-06-25 00:26:25 +00:00
|
|
|
return Object.values(releasesByEntityIdAndEntryId)
|
|
|
|
.map(entityReleases => Object.values(entityReleases))
|
2020-05-14 02:26:05 +00:00
|
|
|
.flat();
|
2020-03-28 03:37:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function filterDuplicateReleases(releases) {
|
2020-05-14 02:26:05 +00:00
|
|
|
const internalUniqueReleases = filterInternalDuplicateReleases(releases);
|
2020-03-28 03:37:04 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
const duplicateReleaseEntries = await knex('releases')
|
2020-06-25 00:26:25 +00:00
|
|
|
.whereIn(['entry_id', 'entity_id'], internalUniqueReleases.map(release => [release.entryId, release.entity.id]));
|
2020-03-16 03:10:52 +00:00
|
|
|
|
2020-06-25 00:26:25 +00:00
|
|
|
const duplicateReleasesByEntityIdAndEntryId = duplicateReleaseEntries.reduce((acc, release) => {
|
2020-06-17 02:07:24 +00:00
|
|
|
if (!acc[release.entity_id]) acc[release.entity_id] = {};
|
|
|
|
acc[release.entity_id][release.entry_id] = true;
|
2020-03-16 03:10:52 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
return acc;
|
|
|
|
}, {});
|
2020-03-22 02:50:24 +00:00
|
|
|
|
2020-06-25 00:26:25 +00:00
|
|
|
const duplicateReleases = internalUniqueReleases.filter(release => duplicateReleasesByEntityIdAndEntryId[release.entity.id]?.[release.entryId]);
|
|
|
|
const uniqueReleases = internalUniqueReleases.filter(release => !duplicateReleasesByEntityIdAndEntryId[release.entity.id]?.[release.entryId]);
|
2020-03-22 02:50:24 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
return {
|
|
|
|
uniqueReleases,
|
|
|
|
duplicateReleases,
|
|
|
|
duplicateReleaseEntries,
|
|
|
|
};
|
2020-03-16 03:10:52 +00:00
|
|
|
}
|
|
|
|
|
2020-03-29 02:00:46 +00:00
|
|
|
async function updateReleasesSearch(releaseIds) {
|
2020-05-14 02:26:05 +00:00
|
|
|
logger.info(`Updating search documents for ${releaseIds ? releaseIds.length : 'all' } releases`);
|
2020-03-29 02:00:46 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
const documents = await knex.raw(`
|
2020-03-29 02:00:46 +00:00
|
|
|
SELECT
|
|
|
|
releases.id AS release_id,
|
|
|
|
TO_TSVECTOR(
|
|
|
|
'traxxx',
|
2020-03-29 21:42:41 +00:00
|
|
|
COALESCE(releases.title, '') || ' ' ||
|
2020-06-17 02:07:24 +00:00
|
|
|
entities.name || ' ' ||
|
|
|
|
entities.slug || ' ' ||
|
2020-07-12 20:36:53 +00:00
|
|
|
COALESCE(array_to_string(entities.alias, ' '), '') || ' ' ||
|
2020-06-29 23:52:17 +00:00
|
|
|
COALESCE(parents.name, '') || ' ' ||
|
|
|
|
COALESCE(parents.slug, '') || ' ' ||
|
2020-07-12 20:36:53 +00:00
|
|
|
COALESCE(array_to_string(parents.alias, ' '), '') || ' ' ||
|
2020-03-29 02:00:46 +00:00
|
|
|
COALESCE(releases.shoot_id, '') || ' ' ||
|
|
|
|
COALESCE(TO_CHAR(releases.date, 'YYYY YY MM FMMM FMmonth mon DD FMDD'), '') || ' ' ||
|
|
|
|
STRING_AGG(COALESCE(actors.name, ''), ' ') || ' ' ||
|
|
|
|
STRING_AGG(COALESCE(tags.name, ''), ' ') || ' ' ||
|
|
|
|
STRING_AGG(COALESCE(tags_aliases.name, ''), ' ')
|
|
|
|
) as document
|
|
|
|
FROM releases
|
2020-06-17 02:07:24 +00:00
|
|
|
LEFT JOIN entities ON releases.entity_id = entities.id
|
|
|
|
LEFT JOIN entities AS parents ON parents.id = entities.parent_id
|
2020-03-29 02:00:46 +00:00
|
|
|
LEFT JOIN releases_actors AS local_actors ON local_actors.release_id = releases.id
|
|
|
|
LEFT JOIN releases_tags AS local_tags ON local_tags.release_id = releases.id
|
|
|
|
LEFT JOIN actors ON local_actors.actor_id = actors.id
|
|
|
|
LEFT JOIN tags ON local_tags.tag_id = tags.id AND tags.priority >= 7
|
|
|
|
LEFT JOIN tags as tags_aliases ON local_tags.tag_id = tags_aliases.alias_for AND tags_aliases.secondary = true
|
|
|
|
${releaseIds ? 'WHERE releases.id = ANY(?)' : ''}
|
2020-06-29 23:52:17 +00:00
|
|
|
GROUP BY releases.id, entities.name, entities.slug, entities.alias, parents.name, parents.slug, parents.alias;
|
2020-03-29 02:00:46 +00:00
|
|
|
`, releaseIds && [releaseIds]);
|
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
if (documents.rows?.length > 0) {
|
|
|
|
const query = knex('releases_search').insert(documents.rows).toString();
|
|
|
|
await knex.raw(`${query} ON CONFLICT (release_id) DO UPDATE SET document = EXCLUDED.document`);
|
|
|
|
}
|
2020-03-29 02:00:46 +00:00
|
|
|
}
|
|
|
|
|
2020-08-08 16:10:59 +00:00
|
|
|
async function storeScenes(releases) {
|
2020-05-14 02:26:05 +00:00
|
|
|
if (releases.length === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const [batchId] = await knex('batches').insert({ comment: null }).returning('id');
|
2020-03-16 03:10:52 +00:00
|
|
|
|
2020-06-25 00:26:25 +00:00
|
|
|
const releasesWithChannels = await attachChannelEntities(releases);
|
|
|
|
const releasesWithStudios = await attachStudios(releasesWithChannels);
|
2020-03-16 23:58:03 +00:00
|
|
|
|
2020-06-25 00:26:25 +00:00
|
|
|
// uniqueness is entity ID + entry ID, filter uniques after adding entities
|
2020-05-14 02:26:05 +00:00
|
|
|
const { uniqueReleases, duplicateReleases, duplicateReleaseEntries } = await filterDuplicateReleases(releasesWithStudios);
|
2020-03-16 03:10:52 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
const curatedNewReleaseEntries = uniqueReleases.map(release => curateReleaseEntry(release, batchId));
|
2020-03-21 01:48:24 +00:00
|
|
|
|
2020-05-26 02:11:29 +00:00
|
|
|
const storedReleases = await knex.batchInsert('releases', curatedNewReleaseEntries).returning('*');
|
2020-05-14 02:26:05 +00:00
|
|
|
// TODO: update duplicate releases
|
2020-03-16 03:10:52 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
const storedReleaseEntries = Array.isArray(storedReleases) ? storedReleases : [];
|
|
|
|
const releasesWithId = attachReleaseIds([].concat(uniqueReleases, duplicateReleases), [].concat(storedReleaseEntries, duplicateReleaseEntries));
|
2020-03-22 02:50:24 +00:00
|
|
|
|
2020-05-16 02:36:45 +00:00
|
|
|
const [actors] = await Promise.all([
|
2020-05-14 02:26:05 +00:00
|
|
|
associateActors(releasesWithId, batchId),
|
|
|
|
associateReleaseTags(releasesWithId),
|
|
|
|
]);
|
2020-03-21 01:48:24 +00:00
|
|
|
|
2020-05-16 02:36:45 +00:00
|
|
|
await updateReleasesSearch(releasesWithId.map(release => release.id));
|
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
// media is more error-prone, associate separately
|
|
|
|
await associateReleaseMedia(releasesWithId);
|
2020-05-17 02:59:09 +00:00
|
|
|
|
2020-06-28 20:29:18 +00:00
|
|
|
if (argv.sceneActors) {
|
2020-05-17 02:59:09 +00:00
|
|
|
await scrapeActors(actors.map(actor => actor.name));
|
|
|
|
}
|
2020-05-13 00:56:20 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
logger.info(`Stored ${storedReleaseEntries.length} releases`);
|
2020-03-28 03:37:04 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
return releasesWithId;
|
2020-03-16 03:10:52 +00:00
|
|
|
}
|
|
|
|
|
2020-08-10 19:39:55 +00:00
|
|
|
async function associateMovieScenes(movies, movieScenes) {
|
|
|
|
const movieScenesByEntityIdAndEntryId = movieScenes.reduce((acc, scene) => ({
|
|
|
|
...acc,
|
|
|
|
[scene.entity.id]: {
|
|
|
|
...acc[scene.entity.id],
|
|
|
|
[scene.entryId]: scene,
|
|
|
|
},
|
|
|
|
}), {});
|
|
|
|
|
|
|
|
const associations = movies.map((movie) => {
|
|
|
|
if (!movie.scenes) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return movie.scenes.map((scene) => {
|
|
|
|
const movieScene = movieScenesByEntityIdAndEntryId[movie.entity.id]?.[scene.entryId];
|
|
|
|
|
|
|
|
if (movieScene) {
|
|
|
|
return {
|
|
|
|
movie_id: movie.id,
|
|
|
|
scene_id: movieScene.id,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
}).flat().filter(Boolean);
|
|
|
|
|
|
|
|
await knex.batchInsert('movies_scenes', associations);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function storeMovies(movies, movieScenes) {
|
2020-08-08 16:10:59 +00:00
|
|
|
const { uniqueReleases } = await filterDuplicateReleases(movies);
|
2020-08-01 13:11:07 +00:00
|
|
|
const [batchId] = await knex('batches').insert({ comment: null }).returning('id');
|
|
|
|
|
2020-08-08 16:10:59 +00:00
|
|
|
const curatedMovieEntries = uniqueReleases.map(release => curateReleaseEntry(release, batchId, null, 'movie'));
|
2020-08-01 13:11:07 +00:00
|
|
|
const storedMovies = await knex.batchInsert('movies', curatedMovieEntries).returning('*');
|
|
|
|
|
|
|
|
const moviesWithId = attachReleaseIds(movies, storedMovies);
|
|
|
|
|
2020-08-10 19:39:55 +00:00
|
|
|
await associateMovieScenes(moviesWithId, movieScenes);
|
2020-08-01 13:11:07 +00:00
|
|
|
await associateReleaseMedia(moviesWithId, 'movies');
|
|
|
|
|
|
|
|
return storedMovies;
|
|
|
|
}
|
|
|
|
|
2020-03-16 03:10:52 +00:00
|
|
|
module.exports = {
|
2020-08-08 16:10:59 +00:00
|
|
|
storeScenes,
|
2020-08-01 13:11:07 +00:00
|
|
|
storeMovies,
|
2020-05-14 02:26:05 +00:00
|
|
|
updateReleasesSearch,
|
2020-03-16 03:10:52 +00:00
|
|
|
};
|