2019-05-06 00:01:57 +00:00
|
|
|
'use strict';
|
|
|
|
|
2020-10-24 22:52:40 +00:00
|
|
|
const inquirer = require('inquirer');
|
|
|
|
|
|
|
|
const logger = require('./logger')(__filename);
|
2019-05-06 00:01:57 +00:00
|
|
|
const knex = require('./knex');
|
2023-06-03 00:51:42 +00:00
|
|
|
const argv = require('./argv');
|
2023-07-25 01:03:41 +00:00
|
|
|
const { updateSceneSearch } = require('./update-search');
|
2020-10-24 22:52:40 +00:00
|
|
|
const { flushOrphanedMedia } = require('./media');
|
2021-02-27 02:52:27 +00:00
|
|
|
|
|
|
|
const { graphql } = require('./web/graphql');
|
|
|
|
|
|
|
|
const releaseFields = `
|
|
|
|
id
|
|
|
|
entryId
|
|
|
|
shootId
|
|
|
|
title
|
|
|
|
url
|
|
|
|
date
|
|
|
|
description
|
|
|
|
duration
|
|
|
|
entity {
|
|
|
|
id
|
|
|
|
name
|
|
|
|
slug
|
|
|
|
parent {
|
|
|
|
id
|
|
|
|
name
|
|
|
|
slug
|
|
|
|
}
|
|
|
|
}
|
|
|
|
actors: releasesActors {
|
|
|
|
actor {
|
|
|
|
id
|
|
|
|
name
|
|
|
|
slug
|
|
|
|
gender
|
|
|
|
aliasFor
|
|
|
|
entityId
|
|
|
|
entryId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tags: releasesTags {
|
|
|
|
tag {
|
|
|
|
id
|
|
|
|
name
|
|
|
|
slug
|
|
|
|
}
|
|
|
|
}
|
2021-02-27 17:05:06 +00:00
|
|
|
chapters(orderBy: TIME_ASC) @include(if: $full) {
|
2021-02-27 02:52:27 +00:00
|
|
|
id
|
|
|
|
index
|
|
|
|
time
|
|
|
|
duration
|
|
|
|
title
|
|
|
|
description
|
2021-02-27 16:19:07 +00:00
|
|
|
tags: chaptersTags {
|
|
|
|
tag {
|
|
|
|
id
|
|
|
|
name
|
|
|
|
slug
|
|
|
|
}
|
|
|
|
}
|
2022-01-12 16:50:23 +00:00
|
|
|
poster: chaptersPoster {
|
2021-02-27 02:57:37 +00:00
|
|
|
media {
|
|
|
|
id
|
|
|
|
path
|
|
|
|
thumbnail
|
|
|
|
s3: isS3
|
|
|
|
width
|
|
|
|
height
|
|
|
|
size
|
|
|
|
}
|
|
|
|
}
|
|
|
|
photos: chaptersPhotos {
|
|
|
|
media {
|
|
|
|
id
|
|
|
|
path
|
|
|
|
thumbnail
|
|
|
|
s3: isS3
|
|
|
|
width
|
|
|
|
height
|
|
|
|
size
|
|
|
|
}
|
|
|
|
}
|
2021-02-27 02:52:27 +00:00
|
|
|
}
|
2022-01-12 16:50:23 +00:00
|
|
|
poster: releasesPoster {
|
2021-02-27 02:52:27 +00:00
|
|
|
media {
|
|
|
|
id
|
|
|
|
path
|
|
|
|
thumbnail
|
|
|
|
s3: isS3
|
|
|
|
width
|
|
|
|
height
|
|
|
|
size
|
|
|
|
}
|
|
|
|
}
|
|
|
|
photos: releasesPhotos @include (if: $full) {
|
|
|
|
media {
|
|
|
|
id
|
|
|
|
path
|
|
|
|
thumbnail
|
|
|
|
s3: isS3
|
|
|
|
width
|
|
|
|
height
|
|
|
|
size
|
|
|
|
}
|
|
|
|
}
|
2022-01-12 16:50:23 +00:00
|
|
|
trailer: releasesTrailer @include (if: $full) {
|
2021-02-27 03:05:38 +00:00
|
|
|
media {
|
|
|
|
id
|
|
|
|
path
|
|
|
|
s3: isS3
|
|
|
|
vr: isVr
|
|
|
|
quality
|
|
|
|
size
|
|
|
|
}
|
|
|
|
}
|
2021-02-27 02:52:27 +00:00
|
|
|
createdAt
|
|
|
|
`;
|
2019-11-17 02:56:45 +00:00
|
|
|
|
2020-12-19 00:03:15 +00:00
|
|
|
function curateRelease(release, withMedia = false, withPoster = true) {
|
2020-05-19 23:11:32 +00:00
|
|
|
if (!release) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
id: release.id,
|
2021-02-26 16:22:54 +00:00
|
|
|
...(release.relevance && { relevance: release.relevance }),
|
2020-05-19 23:11:32 +00:00
|
|
|
entryId: release.entry_id,
|
|
|
|
shootId: release.shoot_id,
|
|
|
|
title: release.title,
|
|
|
|
url: release.url,
|
|
|
|
date: release.date,
|
|
|
|
description: release.description,
|
|
|
|
duration: release.duration,
|
2020-07-17 01:39:13 +00:00
|
|
|
entity: release.entity && {
|
|
|
|
id: release.entity.id,
|
|
|
|
name: release.entity.name,
|
|
|
|
slug: release.entity.slug,
|
2020-08-17 13:53:20 +00:00
|
|
|
parent: release.parent && {
|
|
|
|
id: release.parent.id,
|
|
|
|
name: release.parent.name,
|
|
|
|
slug: release.parent.slug,
|
2020-07-17 01:39:13 +00:00
|
|
|
},
|
2020-05-19 23:11:32 +00:00
|
|
|
},
|
2021-11-20 22:59:15 +00:00
|
|
|
actors: (release.actors || []).map((actor) => ({
|
2020-05-19 23:11:32 +00:00
|
|
|
id: actor.id,
|
|
|
|
name: actor.name,
|
2020-05-19 23:38:58 +00:00
|
|
|
slug: actor.slug,
|
2020-05-19 23:11:32 +00:00
|
|
|
gender: actor.gender,
|
2020-07-17 01:39:13 +00:00
|
|
|
entityId: actor.entity_id,
|
2020-05-19 23:38:58 +00:00
|
|
|
aliasFor: actor.alias_for,
|
2020-05-21 01:44:44 +00:00
|
|
|
})),
|
2021-11-20 22:59:15 +00:00
|
|
|
tags: (release.tags || []).map((tag) => ({
|
2020-05-21 01:44:44 +00:00
|
|
|
id: tag.id,
|
|
|
|
name: tag.name,
|
|
|
|
slug: tag.slug,
|
2020-05-19 23:11:32 +00:00
|
|
|
})),
|
2021-11-20 22:59:15 +00:00
|
|
|
chapters: (release.chapters || []).map((chapter) => ({
|
2021-02-26 23:37:22 +00:00
|
|
|
id: chapter.id,
|
|
|
|
index: chapter.index,
|
|
|
|
time: chapter.time,
|
|
|
|
duration: chapter.duration,
|
|
|
|
title: chapter.title,
|
|
|
|
description: chapter.description,
|
|
|
|
})),
|
2020-12-19 00:03:15 +00:00
|
|
|
...((withMedia || withPoster) && {
|
2020-05-19 23:11:32 +00:00
|
|
|
poster: release.poster ? {
|
|
|
|
id: release.poster.id,
|
|
|
|
path: release.poster.path,
|
2020-12-19 00:03:15 +00:00
|
|
|
thumbnail: release.poster.thumbnail,
|
2020-05-19 23:11:32 +00:00
|
|
|
width: release.poster.width,
|
|
|
|
height: release.poster.height,
|
|
|
|
size: release.poster.size,
|
|
|
|
} : null,
|
2020-12-19 00:03:15 +00:00
|
|
|
}),
|
|
|
|
...(withMedia && {
|
2021-11-20 22:59:15 +00:00
|
|
|
photos: (release.photos || []).map((photo) => ({
|
2020-05-19 23:11:32 +00:00
|
|
|
id: photo.id,
|
|
|
|
path: photo.path,
|
2020-12-19 00:03:15 +00:00
|
|
|
thumbnail: release.poster.thumbnail,
|
2020-05-19 23:11:32 +00:00
|
|
|
width: photo.width,
|
|
|
|
height: photo.height,
|
|
|
|
size: photo.size,
|
|
|
|
})),
|
2020-12-19 00:03:15 +00:00
|
|
|
trailer: release.trailer ? {
|
|
|
|
id: release.trailer.id,
|
|
|
|
path: release.trailer.path,
|
|
|
|
} : null,
|
2020-05-19 23:11:32 +00:00
|
|
|
}),
|
|
|
|
createdAt: release.created_at,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-02-27 02:52:27 +00:00
|
|
|
function curateGraphqlRelease(release) {
|
|
|
|
if (!release) {
|
|
|
|
return null;
|
2020-12-19 00:03:15 +00:00
|
|
|
}
|
|
|
|
|
2021-02-27 02:52:27 +00:00
|
|
|
return {
|
|
|
|
id: release.id,
|
|
|
|
...(release.relevance && { relevance: release.relevance }),
|
|
|
|
entryId: release.entryId,
|
|
|
|
shootId: release.shootId,
|
|
|
|
title: release.title || null,
|
|
|
|
url: release.url || null,
|
|
|
|
date: release.date,
|
|
|
|
description: release.description || null,
|
|
|
|
duration: release.duration,
|
|
|
|
entity: release.entity,
|
2021-11-20 22:59:15 +00:00
|
|
|
actors: release.actors.map((actor) => actor.actor),
|
|
|
|
tags: release.tags.map((tag) => tag.tag),
|
|
|
|
...(release.chapters && { chapters: release.chapters.map((chapter) => ({
|
2021-02-27 02:57:37 +00:00
|
|
|
...chapter,
|
2021-11-20 22:59:15 +00:00
|
|
|
tags: chapter.tags.map((tag) => tag.tag),
|
2021-02-27 02:57:37 +00:00
|
|
|
poster: chapter.poster?.media || null,
|
2021-11-20 22:59:15 +00:00
|
|
|
photos: chapter.photos.map((photo) => photo.media),
|
2021-02-27 02:57:37 +00:00
|
|
|
})) }),
|
2021-02-27 02:52:27 +00:00
|
|
|
poster: release.poster?.media || null,
|
2021-11-20 22:59:15 +00:00
|
|
|
...(release.photos && { photos: release.photos.map((photo) => photo.media) }),
|
2021-02-27 02:52:27 +00:00
|
|
|
trailer: release.trailer?.media || null,
|
|
|
|
createdAt: release.createdAt,
|
|
|
|
};
|
2020-05-19 23:11:32 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 23:40:36 +00:00
|
|
|
async function fetchScene(releaseId) {
|
2021-02-27 02:52:27 +00:00
|
|
|
const { release } = await graphql(`
|
|
|
|
query Release(
|
|
|
|
$releaseId: Int!
|
|
|
|
$full: Boolean = true
|
|
|
|
) {
|
|
|
|
release(id: $releaseId) {
|
|
|
|
${releaseFields}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`, {
|
|
|
|
releaseId: Number(releaseId),
|
|
|
|
});
|
2020-05-19 23:11:32 +00:00
|
|
|
|
2021-02-27 02:52:27 +00:00
|
|
|
return curateGraphqlRelease(release);
|
2020-05-19 23:11:32 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 23:40:36 +00:00
|
|
|
async function fetchScenes(limit = 100) {
|
2021-02-27 02:52:27 +00:00
|
|
|
const { releases } = await graphql(`
|
|
|
|
query SearchReleases(
|
|
|
|
$limit: Int = 20
|
|
|
|
$full: Boolean = false
|
|
|
|
) {
|
|
|
|
releases(
|
|
|
|
first: $limit
|
|
|
|
orderBy: DATE_DESC
|
|
|
|
) {
|
|
|
|
${releaseFields}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`, {
|
|
|
|
limit: Math.min(limit, 10000),
|
|
|
|
});
|
2020-03-10 22:46:55 +00:00
|
|
|
|
2021-11-20 22:59:15 +00:00
|
|
|
return releases.map((release) => curateGraphqlRelease(release));
|
2020-03-29 02:00:46 +00:00
|
|
|
}
|
|
|
|
|
2021-02-26 16:29:02 +00:00
|
|
|
async function searchScenes(query, limit = 100, relevance = 0) {
|
2021-02-27 02:52:27 +00:00
|
|
|
const { releases } = await graphql(`
|
|
|
|
query SearchReleases(
|
|
|
|
$query: String!
|
|
|
|
$limit: Int = 20
|
|
|
|
$relevance: Float = 0.025
|
|
|
|
$full: Boolean = false
|
|
|
|
) {
|
|
|
|
releases: searchReleases(
|
|
|
|
query: $query
|
|
|
|
first: $limit
|
|
|
|
orderBy: RANK_DESC
|
|
|
|
filter: {
|
|
|
|
rank: {
|
|
|
|
greaterThan: $relevance
|
|
|
|
}
|
|
|
|
}
|
|
|
|
) {
|
|
|
|
rank
|
|
|
|
release {
|
|
|
|
${releaseFields}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`, {
|
|
|
|
query,
|
|
|
|
limit,
|
|
|
|
relevance,
|
|
|
|
});
|
2021-02-26 18:39:48 +00:00
|
|
|
|
2021-11-20 22:59:15 +00:00
|
|
|
return releases.map((release) => curateGraphqlRelease({ ...release.release, relevance: release.rank }));
|
2020-02-26 21:33:15 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 00:02:21 +00:00
|
|
|
async function deleteScenes(sceneIds) {
|
2020-10-30 16:37:10 +00:00
|
|
|
if (sceneIds.length === 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-02-18 03:44:04 +00:00
|
|
|
// there can be too many scene IDs for where in, causing a stack depth error
|
2020-10-30 16:37:10 +00:00
|
|
|
const deleteCount = await knex('releases')
|
2021-02-18 03:44:04 +00:00
|
|
|
.whereRaw('id = ANY(:sceneIds)', { sceneIds })
|
2020-10-19 00:02:21 +00:00
|
|
|
.delete();
|
2020-10-30 16:37:10 +00:00
|
|
|
|
2023-07-25 01:03:41 +00:00
|
|
|
await updateSceneSearch(sceneIds);
|
|
|
|
|
2020-10-30 16:37:10 +00:00
|
|
|
logger.info(`Removed ${deleteCount}/${sceneIds.length} scenes`);
|
|
|
|
|
|
|
|
return deleteCount;
|
2020-10-24 22:52:40 +00:00
|
|
|
}
|
|
|
|
|
2021-02-05 03:14:13 +00:00
|
|
|
async function deleteMovies(movieIds) {
|
|
|
|
if (movieIds.length === 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
await knex('movies_scenes')
|
|
|
|
.whereIn('movie_id', movieIds)
|
|
|
|
.delete();
|
|
|
|
|
|
|
|
const deleteCount = await knex('movies')
|
|
|
|
.whereIn('id', movieIds)
|
|
|
|
.delete();
|
|
|
|
|
|
|
|
logger.info(`Removed ${deleteCount}/${movieIds.length} movies`);
|
|
|
|
|
|
|
|
return deleteCount;
|
|
|
|
}
|
|
|
|
|
2022-03-26 16:56:22 +00:00
|
|
|
async function deleteSeries(serieIds) {
|
|
|
|
if (serieIds.length === 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
await knex('series_scenes')
|
|
|
|
.whereIn('serie_id', serieIds)
|
|
|
|
.delete();
|
|
|
|
|
|
|
|
const deleteCount = await knex('series')
|
|
|
|
.whereIn('id', serieIds)
|
|
|
|
.delete();
|
|
|
|
|
|
|
|
logger.info(`Removed ${deleteCount}/${serieIds.length} series`);
|
|
|
|
|
|
|
|
return deleteCount;
|
|
|
|
}
|
|
|
|
|
2020-12-30 03:17:09 +00:00
|
|
|
async function flushScenes() {
|
|
|
|
const sceneIds = await knex('releases').select('id').pluck('id');
|
|
|
|
|
|
|
|
const confirmed = await inquirer.prompt([{
|
|
|
|
type: 'confirm',
|
|
|
|
name: 'flushScenes',
|
|
|
|
message: `You are about to remove ${sceneIds.length} scenes. Are you sure?`,
|
|
|
|
default: false,
|
|
|
|
}]);
|
|
|
|
|
|
|
|
if (!confirmed.flushScenes) {
|
|
|
|
logger.warn('Confirmation rejected, not flushing scenes');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const deleteCount = await deleteScenes(sceneIds);
|
|
|
|
|
2023-06-03 00:51:42 +00:00
|
|
|
if (argv.flushOrphanedMedia !== false) {
|
|
|
|
await flushOrphanedMedia();
|
|
|
|
}
|
2020-12-30 03:17:09 +00:00
|
|
|
|
|
|
|
logger.info(`Removed ${deleteCount}/${sceneIds.length} scenes`);
|
|
|
|
}
|
|
|
|
|
2021-02-05 03:14:13 +00:00
|
|
|
async function flushMovies() {
|
|
|
|
const movieIds = await knex('movies').select('id').pluck('id');
|
|
|
|
|
|
|
|
const confirmed = await inquirer.prompt([{
|
|
|
|
type: 'confirm',
|
|
|
|
name: 'flushMovies',
|
|
|
|
message: `You are about to remove ${movieIds.length} movies. Are you sure?`,
|
|
|
|
default: false,
|
|
|
|
}]);
|
|
|
|
|
2021-02-05 03:17:47 +00:00
|
|
|
if (!confirmed.flushMovies) {
|
|
|
|
logger.warn('Confirmation rejected, not flushing movies');
|
2021-02-05 03:14:13 +00:00
|
|
|
return;
|
2020-10-30 16:37:10 +00:00
|
|
|
}
|
2021-02-05 03:14:13 +00:00
|
|
|
const deleteCount = await deleteMovies(movieIds);
|
2020-10-30 16:37:10 +00:00
|
|
|
|
2023-06-03 00:51:42 +00:00
|
|
|
if (argv.flushOrphanedMedia !== false) {
|
|
|
|
await flushOrphanedMedia();
|
|
|
|
}
|
2020-10-30 16:37:10 +00:00
|
|
|
|
|
|
|
logger.info(`Removed ${deleteCount}/${movieIds.length} movies`);
|
2020-10-24 22:52:40 +00:00
|
|
|
}
|
|
|
|
|
2022-03-26 16:56:22 +00:00
|
|
|
async function flushSeries() {
|
|
|
|
const serieIds = await knex('series').select('id').pluck('id');
|
|
|
|
|
|
|
|
const confirmed = await inquirer.prompt([{
|
|
|
|
type: 'confirm',
|
|
|
|
name: 'flushSeries',
|
|
|
|
message: `You are about to remove ${serieIds.length} series. Are you sure?`,
|
|
|
|
default: false,
|
|
|
|
}]);
|
|
|
|
|
|
|
|
if (!confirmed.flushSeries) {
|
|
|
|
logger.warn('Confirmation rejected, not flushing series');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const deleteCount = await deleteSeries(serieIds);
|
|
|
|
|
2023-06-03 00:51:42 +00:00
|
|
|
if (argv.flushOrphanedMedia !== false) {
|
|
|
|
await flushOrphanedMedia();
|
|
|
|
}
|
2022-03-26 16:56:22 +00:00
|
|
|
|
|
|
|
logger.info(`Removed ${deleteCount}/${serieIds.length} series`);
|
|
|
|
}
|
|
|
|
|
2020-10-24 22:52:40 +00:00
|
|
|
async function flushBatches(batchIds) {
|
|
|
|
const [sceneIds, movieIds] = await Promise.all([
|
|
|
|
knex('releases')
|
|
|
|
.select('releases.id')
|
|
|
|
.whereIn('created_batch_id', batchIds)
|
|
|
|
.pluck('releases.id'),
|
|
|
|
knex('movies').whereIn('created_batch_id', batchIds)
|
|
|
|
.select('movies.id')
|
|
|
|
.whereIn('created_batch_id', batchIds)
|
|
|
|
.pluck('movies.id'),
|
|
|
|
]);
|
|
|
|
|
|
|
|
const confirmed = await inquirer.prompt([{
|
|
|
|
type: 'confirm',
|
|
|
|
name: 'flushBatches',
|
|
|
|
message: `You are about to remove ${sceneIds.length} scenes and ${movieIds.length} movies from batches ${batchIds}. Are you sure?`,
|
|
|
|
default: false,
|
|
|
|
}]);
|
|
|
|
|
|
|
|
if (!confirmed.flushBatches) {
|
|
|
|
logger.warn(`Confirmation rejected, not flushing scenes or movies for batches ${batchIds}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-30 16:37:10 +00:00
|
|
|
const [deletedScenesCount, deletedMoviesCount] = await Promise.all([
|
2020-10-24 22:52:40 +00:00
|
|
|
deleteScenes(sceneIds),
|
|
|
|
deleteMovies(movieIds),
|
|
|
|
]);
|
2020-10-19 00:02:21 +00:00
|
|
|
|
2020-10-30 16:37:10 +00:00
|
|
|
logger.info(`Removed ${deletedScenesCount} scenes and ${deletedMoviesCount} movies for batches ${batchIds}`);
|
|
|
|
|
2023-06-03 00:51:42 +00:00
|
|
|
if (argv.flushOrphanedMedia !== false) {
|
|
|
|
await flushOrphanedMedia();
|
|
|
|
}
|
2020-10-19 00:02:21 +00:00
|
|
|
}
|
|
|
|
|
2019-05-06 00:01:57 +00:00
|
|
|
module.exports = {
|
2020-08-23 00:43:10 +00:00
|
|
|
curateRelease,
|
2020-12-18 23:40:36 +00:00
|
|
|
fetchScene,
|
|
|
|
fetchScenes,
|
2020-10-24 22:52:40 +00:00
|
|
|
flushBatches,
|
2021-02-05 03:14:13 +00:00
|
|
|
flushMovies,
|
2022-03-26 16:56:22 +00:00
|
|
|
flushSeries,
|
2020-12-30 03:17:09 +00:00
|
|
|
flushScenes,
|
2020-12-18 23:40:36 +00:00
|
|
|
searchScenes,
|
2020-10-19 00:02:21 +00:00
|
|
|
deleteScenes,
|
2020-10-24 22:52:40 +00:00
|
|
|
deleteMovies,
|
2022-03-26 16:56:22 +00:00
|
|
|
deleteSeries,
|
2019-05-06 00:01:57 +00:00
|
|
|
};
|