2020-03-16 03:10:52 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const config = require('config');
|
2022-01-10 01:17:17 +00:00
|
|
|
const Promise = require('bluebird');
|
2023-06-15 14:56:27 +00:00
|
|
|
const { decode } = require('html-entities');
|
2020-03-16 03:10:52 +00:00
|
|
|
|
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-08-14 21:05:25 +00:00
|
|
|
const bulkInsert = require('./utils/bulk-insert');
|
2020-08-20 02:57:38 +00:00
|
|
|
const resolvePlace = require('./utils/resolve-place');
|
2022-01-10 01:17:17 +00:00
|
|
|
const chunk = require('./utils/chunk');
|
2020-07-08 00:53:46 +00:00
|
|
|
const { formatDate } = require('./utils/qu');
|
2021-03-06 23:01:02 +00:00
|
|
|
const { associateActors, associateDirectors, scrapeActors, toBaseActors } = 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');
|
2023-07-25 01:03:41 +00:00
|
|
|
const { updateSceneSearch, updateMovieSearch } = require('./update-search');
|
2021-04-25 01:08:50 +00:00
|
|
|
const { notify } = require('./alerts');
|
2020-03-16 03:10:52 +00:00
|
|
|
|
2020-08-20 02:57:38 +00:00
|
|
|
async function curateReleaseEntry(release, batchId, existingRelease, type = 'scene') {
|
2020-07-03 02:12:56 +00:00
|
|
|
const slugBase = release.title
|
2021-11-20 22:59:15 +00:00
|
|
|
|| (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 = {
|
2023-06-15 14:56:27 +00:00
|
|
|
title: decode(release.title),
|
2023-08-02 00:14:41 +00:00
|
|
|
alt_titles: release.altTitles?.map((title) => decode(title)),
|
2020-05-14 02:26:05 +00:00
|
|
|
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,
|
2023-06-15 14:56:27 +00:00
|
|
|
description: decode(release.description),
|
2020-07-08 00:53:46 +00:00
|
|
|
comment: release.comment,
|
2020-05-14 02:26:05 +00:00
|
|
|
deep: typeof release.deep === 'boolean' ? release.deep : false,
|
|
|
|
deep_url: release.deepUrl,
|
|
|
|
updated_batch_id: batchId,
|
|
|
|
};
|
|
|
|
|
2023-06-16 00:29:01 +00:00
|
|
|
if (release.id) {
|
|
|
|
// release is updated
|
|
|
|
curatedRelease.id = release.id;
|
|
|
|
}
|
|
|
|
|
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;
|
2023-06-08 02:19:37 +00:00
|
|
|
curatedRelease.duration = Number(release.duration) || null;
|
2022-05-29 19:10:44 +00:00
|
|
|
curatedRelease.qualities = Array.from(new Set(release.qualities?.map(Number).filter(Boolean))).sort((qualityA, qualityB) => qualityB - qualityA);
|
2020-08-01 13:11:07 +00:00
|
|
|
}
|
|
|
|
|
2020-08-20 02:57:38 +00:00
|
|
|
if (release.productionLocation) {
|
2023-06-15 14:56:27 +00:00
|
|
|
curatedRelease.production_location = decode(release.productionLocation);
|
2020-08-20 02:57:38 +00:00
|
|
|
|
|
|
|
if (argv.resolvePlace) {
|
2023-06-15 14:56:27 +00:00
|
|
|
const productionLocation = await resolvePlace(decode(release.productionLocation));
|
2020-08-20 02:57:38 +00:00
|
|
|
|
|
|
|
if (productionLocation) {
|
|
|
|
curatedRelease.production_city = productionLocation.city;
|
|
|
|
curatedRelease.production_state = productionLocation.state;
|
|
|
|
curatedRelease.production_country_alpha2 = productionLocation.country;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2021-11-20 22:59:15 +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'))
|
2021-11-20 22:59:15 +00:00
|
|
|
.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) {
|
2021-11-20 22:59:15 +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
|
|
|
}
|
|
|
|
|
2022-01-19 23:54:10 +00:00
|
|
|
function attachReleaseIds(releases, storedReleases, batchId) {
|
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-08-14 21:05:25 +00:00
|
|
|
const releasesWithId = releases.map((release) => {
|
|
|
|
if (!release.entity) {
|
2022-03-26 16:40:20 +00:00
|
|
|
logger.error(`No entity available for ${release.url}`);
|
2020-08-14 21:05:25 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-06-13 14:49:27 +00:00
|
|
|
const id = storedReleaseIdsByEntityIdAndEntryId[release.entity.id]?.[release.entryId]
|
|
|
|
|| storedReleaseIdsByEntityIdAndEntryId[release.entity.parent?.id]?.[release.entryId];
|
2020-08-14 21:05:25 +00:00
|
|
|
|
|
|
|
if (id) {
|
|
|
|
return {
|
|
|
|
...release,
|
|
|
|
id,
|
2022-01-19 23:54:10 +00:00
|
|
|
batchId,
|
2020-08-14 21:05:25 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}).filter(Boolean);
|
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-12-30 02:19:09 +00:00
|
|
|
if (!release.entryId) {
|
|
|
|
logger.warn(`No entry ID supplied for "${release.title}" from '${release.entity.name}'`);
|
|
|
|
|
|
|
|
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)
|
2021-11-20 22:59:15 +00:00
|
|
|
.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);
|
2022-01-10 01:17:17 +00:00
|
|
|
const internalUniqueReleaseChunks = chunk(internalUniqueReleases);
|
2020-03-28 03:37:04 +00:00
|
|
|
|
2022-01-10 01:17:17 +00:00
|
|
|
const duplicateReleaseEntryChunks = await Promise.map(internalUniqueReleaseChunks, async (internalUniqueReleasesChunk) => knex('releases')
|
|
|
|
.whereIn(['entry_id', 'entity_id'], internalUniqueReleasesChunk.map((release) => [release.entryId, release.entity.id]))
|
|
|
|
.orWhereIn(['entry_id', 'entity_id'], internalUniqueReleasesChunk
|
2021-06-13 14:49:27 +00:00
|
|
|
// scene IDs shared across network, mark as duplicate so scene can be updated with channel if only available on release day (i.e. Perv City)
|
2021-11-20 22:59:15 +00:00
|
|
|
.filter((release) => release.entity.parent?.parameters?.networkEntryIds)
|
2022-01-10 01:17:17 +00:00
|
|
|
.map((release) => [release.entryId, release.entity.parent.id])), { concurrency: 10 });
|
|
|
|
|
|
|
|
const duplicateReleaseEntries = duplicateReleaseEntryChunks.flat();
|
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
|
|
|
|
2021-11-20 22:59:15 +00:00
|
|
|
const duplicateReleases = internalUniqueReleases.filter((release) => duplicateReleasesByEntityIdAndEntryId[release.entity.id]?.[release.entryId]
|
2021-06-13 14:49:27 +00:00
|
|
|
|| duplicateReleasesByEntityIdAndEntryId[release.entity.parent?.id]?.[release.entryId]);
|
|
|
|
|
2021-11-20 22:59:15 +00:00
|
|
|
const uniqueReleases = internalUniqueReleases.filter((release) => !duplicateReleasesByEntityIdAndEntryId[release.entity.id]?.[release.entryId]
|
2021-06-13 14:49:27 +00:00
|
|
|
&& !duplicateReleasesByEntityIdAndEntryId[release.entity.parent?.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
|
|
|
}
|
|
|
|
|
2021-02-26 23:37:22 +00:00
|
|
|
async function storeChapters(releases) {
|
2021-02-27 17:05:06 +00:00
|
|
|
const chapters = releases
|
2021-11-20 22:59:15 +00:00
|
|
|
.map((release) => release.chapters?.map((chapter, index) => ({
|
2021-02-27 17:05:06 +00:00
|
|
|
releaseId: release.id,
|
|
|
|
index: index + 1,
|
|
|
|
time: chapter.time,
|
|
|
|
duration: chapter.duration,
|
|
|
|
title: chapter.title,
|
|
|
|
description: chapter.description,
|
|
|
|
poster: chapter.poster,
|
|
|
|
photos: chapter.photos,
|
|
|
|
tags: chapter.tags,
|
|
|
|
})))
|
|
|
|
.flat()
|
|
|
|
.filter(Boolean)
|
|
|
|
.sort((chapterA, chapterB) => chapterA.time - chapterB.time);
|
2020-08-20 02:57:38 +00:00
|
|
|
|
2021-11-20 22:59:15 +00:00
|
|
|
const curatedChapterEntries = chapters.map((chapter) => ({
|
2021-02-26 23:37:22 +00:00
|
|
|
index: chapter.index,
|
|
|
|
time: chapter.time,
|
|
|
|
duration: chapter.duration,
|
|
|
|
title: chapter.title,
|
|
|
|
description: chapter.description,
|
|
|
|
release_id: chapter.releaseId,
|
2020-08-20 02:57:38 +00:00
|
|
|
}));
|
|
|
|
|
2021-02-26 23:37:22 +00:00
|
|
|
const storedChapters = await bulkInsert('chapters', curatedChapterEntries, ['release_id', 'index']);
|
|
|
|
|
|
|
|
const chapterIdsByReleaseIdAndChapter = storedChapters.reduce((acc, chapter) => ({
|
2020-08-20 02:57:38 +00:00
|
|
|
...acc,
|
2021-02-26 23:37:22 +00:00
|
|
|
[chapter.release_id]: {
|
|
|
|
...acc[chapter.release_id],
|
|
|
|
[chapter.index]: chapter.id,
|
2020-08-20 02:57:38 +00:00
|
|
|
},
|
|
|
|
}), {});
|
|
|
|
|
2021-11-20 22:59:15 +00:00
|
|
|
const chaptersWithId = chapters.map((chapter) => ({
|
2021-02-26 23:37:22 +00:00
|
|
|
...chapter,
|
|
|
|
id: chapterIdsByReleaseIdAndChapter[chapter.releaseId][chapter.index],
|
2020-08-20 02:57:38 +00:00
|
|
|
}));
|
|
|
|
|
2021-02-26 23:37:22 +00:00
|
|
|
await associateReleaseTags(chaptersWithId, 'chapter');
|
2020-08-20 02:57:38 +00:00
|
|
|
|
|
|
|
// media is more error-prone, associate separately
|
2021-02-26 23:37:22 +00:00
|
|
|
await associateReleaseMedia(chaptersWithId, 'chapter');
|
2020-08-20 02:57:38 +00:00
|
|
|
}
|
|
|
|
|
2020-08-10 19:39:55 +00:00
|
|
|
async function associateMovieScenes(movies, movieScenes) {
|
2021-01-29 01:38:05 +00:00
|
|
|
const moviesByEntityIdAndEntryId = movies.reduce((acc, movie) => ({
|
2020-08-10 19:39:55 +00:00
|
|
|
...acc,
|
2021-01-29 01:38:05 +00:00
|
|
|
[movie.entity.id]: {
|
|
|
|
...acc[movie.entity.id],
|
|
|
|
[movie.entryId]: movie,
|
2020-08-10 19:39:55 +00:00
|
|
|
},
|
|
|
|
}), {});
|
|
|
|
|
2021-01-29 01:38:05 +00:00
|
|
|
const associations = movieScenes.map((scene) => {
|
|
|
|
if (!scene.movie) {
|
2020-08-10 19:39:55 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-03-04 22:31:59 +00:00
|
|
|
const sceneMovie = moviesByEntityIdAndEntryId[scene.entity.id]?.[scene.movie.entryId]
|
|
|
|
|| moviesByEntityIdAndEntryId[scene.entity.parent?.id]?.[scene.movie.entryId];
|
2020-08-10 19:39:55 +00:00
|
|
|
|
2021-01-29 01:38:05 +00:00
|
|
|
if (sceneMovie?.id) {
|
|
|
|
return {
|
|
|
|
movie_id: sceneMovie.id,
|
|
|
|
scene_id: scene.id,
|
|
|
|
};
|
|
|
|
}
|
2020-08-10 19:39:55 +00:00
|
|
|
|
2021-01-29 01:38:05 +00:00
|
|
|
return null;
|
|
|
|
}).filter(Boolean);
|
2020-08-10 19:39:55 +00:00
|
|
|
|
2020-08-14 21:21:53 +00:00
|
|
|
await bulkInsert('movies_scenes', associations, false);
|
2020-08-10 19:39:55 +00:00
|
|
|
}
|
|
|
|
|
2022-03-26 16:40:20 +00:00
|
|
|
async function associateSerieScenes(series, serieScenes) {
|
|
|
|
const seriesByEntityIdAndEntryId = series.reduce((acc, serie) => ({
|
|
|
|
...acc,
|
|
|
|
[serie.entity.id]: {
|
|
|
|
...acc[serie.entity.id],
|
|
|
|
[serie.entryId]: serie,
|
|
|
|
},
|
|
|
|
}), {});
|
|
|
|
|
|
|
|
const associations = serieScenes.map((scene) => {
|
|
|
|
if (!scene.serie) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const sceneSerie = seriesByEntityIdAndEntryId[scene.entity.id]?.[scene.serie.entryId]
|
|
|
|
|| seriesByEntityIdAndEntryId[scene.entity.parent?.id]?.[scene.serie.entryId];
|
|
|
|
|
|
|
|
if (sceneSerie?.id) {
|
|
|
|
return {
|
|
|
|
serie_id: sceneSerie.id,
|
|
|
|
scene_id: scene.id,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}).filter(Boolean);
|
|
|
|
|
|
|
|
await bulkInsert('series_scenes', associations, false);
|
|
|
|
}
|
|
|
|
|
2022-01-19 23:54:10 +00:00
|
|
|
async function storeMovies(movies, useBatchId) {
|
2021-01-29 01:38:05 +00:00
|
|
|
if (!movies || movies.length === 0) {
|
2020-08-23 01:41:29 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2020-08-08 16:10:59 +00:00
|
|
|
const { uniqueReleases } = await filterDuplicateReleases(movies);
|
2023-07-25 01:22:24 +00:00
|
|
|
const [batchId] = useBatchId ? [useBatchId] : await knex('batches').insert({ showcased: argv.showcased, comment: null }).returning('id');
|
2020-08-01 13:11:07 +00:00
|
|
|
|
2021-11-20 22:59:15 +00:00
|
|
|
const curatedMovieEntries = await Promise.all(uniqueReleases.map((release) => curateReleaseEntry(release, batchId, null, 'movie')));
|
2020-08-01 13:11:07 +00:00
|
|
|
|
2020-08-14 21:05:25 +00:00
|
|
|
const storedMovies = await bulkInsert('movies', curatedMovieEntries, ['entity_id', 'entry_id'], true);
|
2020-08-01 13:11:07 +00:00
|
|
|
const moviesWithId = attachReleaseIds(movies, storedMovies);
|
|
|
|
|
2021-11-20 22:59:15 +00:00
|
|
|
await updateMovieSearch(moviesWithId.map((movie) => movie.id));
|
2020-08-20 02:57:38 +00:00
|
|
|
await associateReleaseMedia(moviesWithId, 'movie');
|
2020-08-01 13:11:07 +00:00
|
|
|
|
2021-01-29 01:38:05 +00:00
|
|
|
return moviesWithId;
|
2020-08-01 13:11:07 +00:00
|
|
|
}
|
|
|
|
|
2022-03-26 16:40:20 +00:00
|
|
|
async function storeSeries(series, useBatchId) {
|
|
|
|
if (!series || series.length === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const { uniqueReleases } = await filterDuplicateReleases(series);
|
2023-07-25 01:22:24 +00:00
|
|
|
const [batchId] = useBatchId ? [useBatchId] : await knex('batches').insert({ showcased: argv.showcased, comment: null }).returning('id');
|
2022-03-26 16:40:20 +00:00
|
|
|
|
|
|
|
const curatedSerieEntries = await Promise.all(uniqueReleases.map((release) => curateReleaseEntry(release, batchId, null, 'serie')));
|
|
|
|
|
|
|
|
const storedSeries = await bulkInsert('series', curatedSerieEntries, ['entity_id', 'entry_id'], true);
|
|
|
|
const seriesWithId = attachReleaseIds(series, storedSeries);
|
|
|
|
|
|
|
|
await updateMovieSearch(seriesWithId.map((serie) => serie.id), 'serie');
|
|
|
|
await associateReleaseMedia(seriesWithId, 'serie');
|
|
|
|
|
|
|
|
return seriesWithId;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function storeScenes(releases, useBatchId) {
|
|
|
|
if (!releases || releases.length === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2023-07-25 01:22:24 +00:00
|
|
|
const [batchId] = useBatchId ? [useBatchId] : await knex('batches').insert({ showcased: argv.showcased, comment: null }).returning('id');
|
2022-03-26 16:40:20 +00:00
|
|
|
|
|
|
|
const releasesWithChannels = await attachChannelEntities(releases);
|
|
|
|
const releasesWithBaseActors = releasesWithChannels.map((release) => ({ ...release, actors: toBaseActors(release.actors) }));
|
|
|
|
const releasesWithStudios = await attachStudios(releasesWithBaseActors);
|
|
|
|
|
|
|
|
// uniqueness is entity ID + entry ID, filter uniques after adding entities
|
|
|
|
const { uniqueReleases, duplicateReleases, duplicateReleaseEntries } = await filterDuplicateReleases(releasesWithStudios);
|
|
|
|
|
|
|
|
const curatedNewReleaseEntries = await Promise.all(uniqueReleases.map((release) => curateReleaseEntry(release, batchId)));
|
|
|
|
const storedReleases = await bulkInsert('releases', curatedNewReleaseEntries);
|
|
|
|
|
|
|
|
const storedReleaseEntries = Array.isArray(storedReleases) ? storedReleases : [];
|
|
|
|
|
|
|
|
const uniqueReleasesWithId = attachReleaseIds(uniqueReleases, storedReleaseEntries, batchId);
|
|
|
|
const duplicateReleasesWithId = attachReleaseIds(duplicateReleases, duplicateReleaseEntries, batchId);
|
2023-06-15 14:56:27 +00:00
|
|
|
const curatedDuplicateReleases = await Promise.all(duplicateReleasesWithId.map((release) => curateReleaseEntry(release, batchId)));
|
2022-03-26 16:40:20 +00:00
|
|
|
const releasesWithId = uniqueReleasesWithId.concat(duplicateReleasesWithId);
|
|
|
|
|
|
|
|
const updated = await knex.raw(`
|
|
|
|
UPDATE releases
|
|
|
|
SET url = COALESCE(new.url, releases.url),
|
|
|
|
date = COALESCE(new.date, releases.date),
|
|
|
|
entity_id = COALESCE((new.entity->>'id')::integer, releases.entity_id),
|
|
|
|
title = COALESCE(new.title, releases.title),
|
|
|
|
description = COALESCE(new.description, releases.description),
|
|
|
|
duration = COALESCE(new.duration, releases.duration),
|
|
|
|
deep = new.url IS NOT NULL,
|
|
|
|
updated_at = NOW()
|
|
|
|
FROM json_to_recordset(:scenes)
|
|
|
|
AS new(id int, url text, date timestamptz, entity json, title text, description text, duration integer, deep boolean)
|
|
|
|
WHERE releases.id = new.id
|
|
|
|
`, {
|
2023-06-15 14:56:27 +00:00
|
|
|
scenes: JSON.stringify(curatedDuplicateReleases),
|
2022-03-26 16:40:20 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const [actors, storedSeries] = await Promise.all([
|
|
|
|
associateActors(releasesWithId, batchId),
|
|
|
|
storeSeries(releasesWithId.map((release) => release.serie && { ...release.serie, entity: release.entity }).filter(Boolean), batchId),
|
|
|
|
associateReleaseTags(releasesWithId),
|
|
|
|
storeChapters(releasesWithId),
|
|
|
|
]);
|
|
|
|
|
|
|
|
await associateSerieScenes(storedSeries, releasesWithId);
|
|
|
|
await associateDirectors(releasesWithId, batchId); // some directors may also be actors, don't associate at the same time
|
|
|
|
await updateSceneSearch(releasesWithId.map((release) => release.id));
|
|
|
|
|
|
|
|
// media is more error-prone, associate separately
|
|
|
|
await associateReleaseMedia(releasesWithId);
|
|
|
|
|
|
|
|
if (argv.sceneActors && actors) {
|
|
|
|
await scrapeActors(actors.map((actor) => actor.name));
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.info(`Stored ${storedReleaseEntries.length}, updated ${updated.rowCount} releases`);
|
|
|
|
|
|
|
|
await notify(releasesWithId);
|
|
|
|
|
|
|
|
return releasesWithId;
|
|
|
|
}
|
|
|
|
|
2020-03-16 03:10:52 +00:00
|
|
|
module.exports = {
|
2021-01-29 01:38:05 +00:00
|
|
|
associateMovieScenes,
|
2020-08-08 16:10:59 +00:00
|
|
|
storeScenes,
|
2020-08-01 13:11:07 +00:00
|
|
|
storeMovies,
|
2021-08-22 20:25:20 +00:00
|
|
|
updateSceneSearch,
|
|
|
|
updateMovieSearch,
|
2020-03-16 03:10:52 +00:00
|
|
|
};
|