traxxx/src/releases.js

468 lines
9.3 KiB
JavaScript
Raw Normal View History

'use strict';
const inquirer = require('inquirer');
const logger = require('./logger')(__filename);
const knex = require('./knex');
const argv = require('./argv');
const { updateSceneSearch } = require('./update-search');
const { flushOrphanedMedia } = require('./media');
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
}
}
chapters(orderBy: TIME_ASC) @include(if: $full) {
id
index
time
duration
title
description
2021-02-27 16:19:07 +00:00
tags: chaptersTags {
tag {
id
name
slug
}
}
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
}
}
}
poster: releasesPoster {
media {
id
path
thumbnail
s3: isS3
width
height
size
}
}
photos: releasesPhotos @include (if: $full) {
media {
id
path
thumbnail
s3: isS3
width
height
size
}
}
trailer: releasesTrailer @include (if: $full) {
2021-02-27 03:05:38 +00:00
media {
id
path
s3: isS3
vr: isVr
quality
size
}
}
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,
...(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,
entity: release.entity && {
id: release.entity.id,
name: release.entity.name,
slug: release.entity.slug,
parent: release.parent && {
id: release.parent.id,
name: release.parent.name,
slug: release.parent.slug,
},
2020-05-19 23:11:32 +00:00
},
actors: (release.actors || []).map((actor) => ({
2020-05-19 23:11:32 +00:00
id: actor.id,
name: actor.name,
slug: actor.slug,
2020-05-19 23:11:32 +00:00
gender: actor.gender,
entityId: actor.entity_id,
aliasFor: actor.alias_for,
})),
tags: (release.tags || []).map((tag) => ({
id: tag.id,
name: tag.name,
slug: tag.slug,
2020-05-19 23:11:32 +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 && {
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,
};
}
function curateGraphqlRelease(release) {
if (!release) {
return null;
2020-12-19 00:03:15 +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,
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,
tags: chapter.tags.map((tag) => tag.tag),
2021-02-27 02:57:37 +00:00
poster: chapter.poster?.media || null,
photos: chapter.photos.map((photo) => photo.media),
2021-02-27 02:57:37 +00:00
})) }),
poster: release.poster?.media || null,
...(release.photos && { photos: release.photos.map((photo) => photo.media) }),
trailer: release.trailer?.media || null,
createdAt: release.createdAt,
};
2020-05-19 23:11:32 +00:00
}
async function fetchScene(releaseId) {
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
return curateGraphqlRelease(release);
2020-05-19 23:11:32 +00:00
}
async function fetchScenes(limit = 100) {
const { releases } = await graphql(`
query SearchReleases(
$limit: Int = 20
$full: Boolean = false
) {
releases(
first: $limit
orderBy: DATE_DESC
) {
${releaseFields}
}
}
`, {
limit: Math.min(limit, 10000),
});
return releases.map((release) => curateGraphqlRelease(release));
}
async function searchScenes(query, limit = 100, relevance = 0) {
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
return releases.map((release) => curateGraphqlRelease({ ...release.release, relevance: release.rank }));
}
async function deleteScenes(sceneIds) {
if (sceneIds.length === 0) {
return 0;
}
// there can be too many scene IDs for where in, causing a stack depth error
const deleteCount = await knex('releases')
.whereRaw('id = ANY(:sceneIds)', { sceneIds })
.delete();
await updateSceneSearch(sceneIds);
logger.info(`Removed ${deleteCount}/${sceneIds.length} scenes`);
return deleteCount;
}
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;
}
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);
if (argv.flushOrphanedMedia !== false) {
await flushOrphanedMedia();
}
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;
}
2021-02-05 03:14:13 +00:00
const deleteCount = await deleteMovies(movieIds);
if (argv.flushOrphanedMedia !== false) {
await flushOrphanedMedia();
}
logger.info(`Removed ${deleteCount}/${movieIds.length} movies`);
}
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);
if (argv.flushOrphanedMedia !== false) {
await flushOrphanedMedia();
}
2022-03-26 16:56:22 +00:00
logger.info(`Removed ${deleteCount}/${serieIds.length} series`);
}
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;
}
const [deletedScenesCount, deletedMoviesCount] = await Promise.all([
deleteScenes(sceneIds),
deleteMovies(movieIds),
]);
logger.info(`Removed ${deletedScenesCount} scenes and ${deletedMoviesCount} movies for batches ${batchIds}`);
if (argv.flushOrphanedMedia !== false) {
await flushOrphanedMedia();
}
}
module.exports = {
curateRelease,
fetchScene,
fetchScenes,
flushBatches,
2021-02-05 03:14:13 +00:00
flushMovies,
2022-03-26 16:56:22 +00:00
flushSeries,
flushScenes,
searchScenes,
deleteScenes,
deleteMovies,
2022-03-26 16:56:22 +00:00
deleteSeries,
};