'use strict'; const util = require('util'); const argv = require('./argv'); const initServer = require('./web/server'); const knex = require('./knex'); const fetchUpdates = require('./updates'); const { fetchScenes, fetchMovies } = require('./deep'); const { storeScenes, storeMovies, updateReleasesSearch } = require('./store-releases'); const { scrapeActors } = require('./actors'); const getFileEntries = require('./utils/file-entries'); async function init() { if (argv.server) { await initServer(); return; } if (argv.updateSearch) { await updateReleasesSearch(); } const actorsFromFile = argv.actorsFile && await getFileEntries(argv.actorsFile); const actorNames = (argv.actors || []).concat(actorsFromFile || []); const actors = (argv.actors || argv.actorsUpdate || argv.actorsFile) && await scrapeActors(actorNames); const actorBaseScenes = argv.actors && argv.actorScenes && actors.map(actor => actor.releases).flat().filter(Boolean); const updateBaseScenes = (argv.latest || argv.upcoming || argv.channels || argv.networks || argv.movies) && await fetchUpdates(); const scenesFromFile = argv.scenesFile && await getFileEntries(argv.scenesFile); const sceneUrls = (argv.scene || []).concat(scenesFromFile || []); const deepScenes = argv.deep ? await fetchScenes([...(sceneUrls), ...(updateBaseScenes || []), ...(actorBaseScenes || [])]) : [...(updateBaseScenes || []), ...(actorBaseScenes || [])]; const sceneMovies = deepScenes && deepScenes.map(scene => scene.movie).filter(Boolean); const deepMovies = argv.sceneMovies && await fetchMovies([...(argv.movie || []), ...(sceneMovies || [])]); const movieScenes = argv.movieScenes ? deepMovies.map(movie => movie.scenes?.map(scene => ({ ...scene, entity: movie.entity }))).flat().filter(Boolean) : []; const deepMovieScenes = argv.deep ? await fetchScenes(movieScenes) : movieScenes; if (argv.inspect) { console.log(util.inspect(deepScenes)); console.log(util.inspect(deepMovies)); } if (argv.save) { if (deepMovies.length > 0) { const storedMovieScenes = await storeScenes(deepMovieScenes); await storeMovies(deepMovies, storedMovieScenes || []); } if (deepScenes.length > 0 || deepMovieScenes.length > 0) { await storeScenes(deepScenes || []); } } knex.destroy(); } module.exports = init;