traxxx/src/app.js

173 lines
4.8 KiB
JavaScript
Raw Normal View History

'use strict';
2020-05-20 00:23:45 +00:00
const util = require('util');
// const log = require('why-is-node-running');
2021-10-17 21:56:49 +00:00
const Inspector = require('inspector-api');
const fs = require('fs').promises;
const dayjs = require('dayjs');
2020-05-20 00:23:45 +00:00
const argv = require('./argv');
const initServer = require('./web/server');
const http = require('./utils/http');
2021-10-17 21:56:49 +00:00
const logger = require('./logger')(__filename);
const knex = require('./knex');
const fetchUpdates = require('./updates');
const { fetchScenes, fetchMovies } = require('./deep');
const { storeScenes, storeMovies, updateSceneSearch, updateMovieSearch, associateMovieScenes } = require('./store-releases');
const { scrapeActors, deleteActors, flushActors, flushProfiles, interpolateProfiles } = require('./actors');
const { flushEntities } = require('./entities');
const { deleteScenes, deleteMovies, flushScenes, flushMovies, flushBatches } = require('./releases');
const { flushOrphanedMedia } = require('./media');
const getFileEntries = require('./utils/file-entries');
2021-10-17 21:56:49 +00:00
const inspector = new Inspector();
function logActive() {
console.log('log active!');
setTimeout(() => {
// log();
logActive();
}, typeof argv.logActive === 'number' ? argv.logActive : 60000);
}
2021-10-17 21:56:49 +00:00
async function stopMemorySample() {
const profile = await inspector.heap.stopSampling();
const filepath = `${dayjs().format('YYYY-MM-DD_HH-mm-ss')}.heapprofile`;
await inspector.heap.disable();
fs.writeFile(filepath, JSON.stringify(profile));
logger.info(`Saved heap sample to ${filepath}`);
}
async function init() {
2021-10-17 21:56:49 +00:00
try {
if (argv.memory) {
await inspector.heap.enable();
await inspector.heap.startSampling();
2021-10-17 21:56:49 +00:00
logger.info('Started heap sampling');
}
2021-10-17 21:56:49 +00:00
if (argv.logActive) {
logActive();
}
2021-10-17 21:56:49 +00:00
if (argv.server) {
await initServer();
return;
}
2020-12-30 01:23:43 +00:00
2021-10-17 21:56:49 +00:00
if (argv.updateSearch) {
await Promise.all([
updateSceneSearch(),
updateMovieSearch(),
]);
}
2020-12-30 02:19:09 +00:00
2021-10-17 21:56:49 +00:00
if (argv.interpolateProfiles) {
await interpolateProfiles(argv.interpolateProfiles.length > 0 ? argv.interpolateProfiles : null);
}
2021-10-17 21:56:49 +00:00
if (argv.flushActors) {
await flushActors(argv.flushActors);
}
2021-10-17 21:56:49 +00:00
if (argv.flushProfiles) {
await flushProfiles(argv.flushProfiles.length > 0 ? argv.flushProfiles : null);
}
2021-10-17 21:56:49 +00:00
if (argv.flushNetworks || argv.flushChannels) {
await flushEntities(argv.flushNetworks, argv.flushChannels);
}
2021-10-17 21:56:49 +00:00
if (argv.flushBatches) {
await flushBatches(argv.flushBatches);
}
2021-10-17 21:56:49 +00:00
if (argv.flushScenes) {
await flushScenes();
}
2021-10-17 21:56:49 +00:00
if (argv.flushMovies) {
await flushMovies();
}
2021-10-17 21:56:49 +00:00
if (argv.deleteActors) {
await deleteActors(argv.deleteActors);
}
2021-10-17 21:56:49 +00:00
if (argv.deleteScenes) {
await deleteScenes(argv.deleteScenes);
}
2021-10-17 21:56:49 +00:00
if (argv.deleteMovies) {
await deleteMovies(argv.deleteMovies);
}
2021-10-17 21:56:49 +00:00
if (argv.flushOrphanedMedia) {
await flushOrphanedMedia();
}
2021-10-17 21:56:49 +00:00
if (argv.request) {
const res = await http.get(argv.request);
2021-10-17 21:56:49 +00:00
console.log(res.status, res.body);
}
2021-10-17 21:56:49 +00:00
const actorsFromFile = argv.actorsFile && await getFileEntries(argv.actorsFile);
const actorNames = (argv.actors || []).concat(actorsFromFile || []);
2021-10-17 21:56:49 +00:00
const actors = (argv.actors || argv.actorsUpdate || argv.actorsFile) && await scrapeActors(actorNames);
const actorBaseScenes = argv.actors && argv.actorScenes && actors.map(actor => actor.scenes).flat().filter(Boolean);
2021-10-17 21:56:49 +00:00
const updateBaseScenes = (argv.latest || argv.upcoming || argv.channels || argv.networks || argv.movies) && await fetchUpdates();
2021-10-17 21:56:49 +00:00
const scenesFromFile = argv.scenesFile && await getFileEntries(argv.scenesFile);
const sceneUrls = (argv.scene || []).concat(scenesFromFile || []);
2021-10-17 21:56:49 +00:00
const deepScenes = argv.deep
? await fetchScenes([...(sceneUrls), ...(updateBaseScenes || []), ...(actorBaseScenes || [])])
: [...(updateBaseScenes || []), ...(actorBaseScenes || [])];
2021-10-17 21:56:49 +00:00
const sceneMovies = deepScenes ? deepScenes.filter(scene => scene.movie).map(scene => ({ ...scene.movie, entity: scene.entity })) : [];
const deepMovies = argv.sceneMovies || argv.movie ? await fetchMovies([...(argv.movie || []), ...(sceneMovies || [])]) : sceneMovies;
2020-05-20 00:23:45 +00:00
2021-10-17 21:56:49 +00:00
const movieScenes = argv.movieScenes ? deepMovies.map(movie => movie.scenes?.map(scene => ({ ...scene, movie, entity: movie.entity }))).flat().filter(Boolean) : [];
const deepMovieScenes = argv.deep ? await fetchScenes(movieScenes) : movieScenes;
2021-01-26 23:21:58 +00:00
2021-10-17 21:56:49 +00:00
if (argv.report) {
console.log(util.inspect(deepScenes, { depth: Infinity, colors: true }));
console.log(util.inspect(deepMovies, { depth: Infinity, colors: true }));
}
2021-10-17 21:56:49 +00:00
if (argv.save) {
const storedMovies = await storeMovies(deepMovies);
const storedScenes = await storeScenes([...(deepScenes || []), ...(deepMovieScenes || [])]);
await associateMovieScenes(storedMovies, storedScenes);
}
if (argv.memory) {
await stopMemorySample();
}
knex.destroy();
} catch (error) {
logger.error(error);
if (argv.memory) {
await stopMemorySample();
}
knex.destroy();
throw error;
}
}
2019-12-09 04:00:49 +00:00
module.exports = init;