diff --git a/backend/services/entryManager.js b/backend/services/entryManager.js index b1c205c..aa3ffe5 100644 --- a/backend/services/entryManager.js +++ b/backend/services/entryManager.js @@ -10,19 +10,19 @@ const EntryRating = require("../models/EntryRating.js"); const EntrySeen = require("../models/EntrySeen.js"); const Media = require("../models/Media.js"); const Genre = require("../models/Genre.js"); +const People = require("../models/People.js"); +const Credit = require("../models/Credit.js"); const BadRequestError = require("../errors/BadRequestError"); const NotFoundError = require("../errors/NotFoundError.js"); const ValidationError = require("../errors/ValidationError.js"); -const { fetchEntryData, fetchSeasonData } = require("./tmdbService.js"); +const { fetchEntryData, fetchSeasonData, fetchEntryCredits } = require("./tmdbService.js"); const { logToFile } = require("./logService.js"); const collectionManager = require('./collectionManager.js'); const entryService = require("./entryService"); const mediaManager = require("./mediaManager.js"); -const EntryNetwork = require("../models/EntryNetwork.js"); const networkService = require("./networkService.js"); -const Network = require("../models/Network.js"); const _collectionManager = collectionManager; const _entryService = entryService; @@ -66,6 +66,8 @@ class EntryManager { // todo: change fetchEntryData to TMDBService.fetchEntryData() // todo: change fetchEntryData to TMDBService.fetchSeasonData() + // todo: change fetchEntryData to TMDBService.fetchEntryCredits() + // todo: change fetchEntryData to TMDBService.fetchPersonDetails() async refrechEntryById(id, plain = true) { if (!id) { throw new BadRequestError("Entry id not provided"); @@ -201,7 +203,7 @@ class EntryManager { }); } - if (existingSeason.medias.length = 0) + if (!existingSeason.medias || existingSeason.medias.length == 0) _mediaManager.getSeasonMedia(existingSeason); // * Update or insert episodes for the season @@ -246,17 +248,99 @@ class EntryManager { }); } - if (existingEpisode.medias.length == 0) + if (!existingEpisode.medias || existingEpisode.medias.length == 0) _mediaManager.getEpisodenMedia(existingEpisode); } } } - // * Cast - // ? Only fetch if the cast count is 0 or less than max_cast - // TODO add cast refrech - // if (!oldEntry.casts || oldEntry.casts.length == 0 || oldEntry.casts.length < oldEntry.max_cast) { - // } + // * Credits + if (!oldEntry.cast_fetched || oldEntry.credits.length == 0) { + const creditData = await fetchEntryCredits(oldEntry.entryId.tmdb_id, oldEntry.is_tvseries); + if (!creditData) { + throw new InternalServerError(`Couldn't fetch entry crew data`); + } + + // ? loop creditData.crew and creditData.cast and create a people for each if missing + // ? then create a cast/crew for each people if missing + for (const cast of creditData.cast) { + // ? check if the cast already exists + let existingPeople = await People.findOne({ + where: { tmdb_id: cast.id } + }); + + // ? if not, create a new people + if (!existingPeople) { + existingPeople = await People.create({ + tmdb_id: cast.id, + name: cast.name, + gender: cast.gender || null, + popularity: cast.popularity, + known_for_department: cast.known_for_department || null, + profile_path: cast.profile_path, + adult: cast.adult || false, + last_fetch: new Date() + }); + } + + // ? check if the cast already exists for this entry + let existingCast = oldEntry.credits.find(c => c.people_id === existingPeople.id); + + // ? if not, create a new cast + if (!existingCast) { + await Credit.create({ + entry_id: oldEntry.id, + people_id: existingPeople.id, + tmdb_credit_id: cast.credit_id, + character: cast.character, + order: cast.order, + profile_path: existingPeople.profile_path || null, + adult: existingPeople.adult || false, + cast_type: 'cast' + }); + } + } + + for (const crew of creditData.crew) { + // ? check if the crew already exists + let existingPeople = await People.findOne({ + where: { tmdb_id: crew.id } + }); + + // ? if not, create a new people + if (!existingPeople) { + existingPeople = await People.create({ + tmdb_id: crew.id, + name: crew.name, + gender: crew.gender || null, + popularity: crew.popularity, + known_for_department: crew.known_for_department || null, + profile_path: crew.profile_path, + adult: crew.adult || false, + last_fetch: new Date() + }); + } + + // ? check if the crew already exists for this entry + let existingCast = oldEntry.credits.find(c => c.people_id === existingPeople.id); + + // ? if not, create a new cast + if (!existingCast) { + await Credit.create({ + entry_id: oldEntry.id, + people_id: existingPeople.id, + tmdb_credit_id: crew.credit_id, + character: null, + order: null, + profile_path: existingPeople.profile_path || null, + adult: existingPeople.adult || false, + cast_type: 'crew', + department: crew.department || null, + job: crew.job || null + }); + } + } + } // * Media // ? Only fetch if a Poster, Backdrop or a Logo is missing diff --git a/backend/services/entryService.js b/backend/services/entryService.js index a097fa3..0155639 100644 --- a/backend/services/entryService.js +++ b/backend/services/entryService.js @@ -14,9 +14,12 @@ const NotFoundError = require("../errors/NotFoundError"); const mediaOptimizerService = require("./mediaOptimizerService.js"); const mediaManager = require("./mediaManager.js"); -const { fetchSeasonData } = require("./tmdbService.js"); +const { fetchSeasonData, fetchEntryCredits } = require("./tmdbService.js"); const Network = require("../models/Network.js"); const networkService = require("./networkService.js"); +const Cast = require("../models/Credit.js"); +const People = require("../models/People.js"); +const Credit = require("../models/Credit.js"); const _mediaManager = mediaManager; const _mediaOptimizerService = mediaOptimizerService; @@ -72,6 +75,12 @@ class EntryService { { model: Season, as: 'seasons' }, { model: Media, as: 'medias' }, { model: Network, as: 'networks' }, + { + model: Cast, as: 'credits', + include: [ + { model: People, as: 'person' } + ] + }, { model: Collection, as: 'collections', include: [ @@ -233,8 +242,84 @@ class EntryService { } // + end of: seasons & episodes for tv series - // ? cast + // ? Credits + const creditData = await fetchEntryCredits(data.id, is_tvseries); + if (!creditData) { + throw new InternalServerError(`Couldn't fetch entry credits data`); + } + // ? loop creditData.crew and creditData.cast and create a people for each if missing + // ? then create a cast/crew for each people if missing + for (const cast of creditData.cast) { + // ? check if the cast already exists + let existingPeople = await People.findOne({ + where: { tmdb_id: cast.id } + }); + + // ? if not, create a new people + if (!existingPeople) { + existingPeople = await People.create({ + tmdb_id: cast.id, + name: cast.name, + gender: cast.gender || null, + popularity: cast.popularity, + known_for_department: cast.known_for_department || null, + profile_path: cast.profile_path, + adult: cast.adult || false, + last_fetch: new Date() + }); + } + + // ? create a new cast + await Credit.create({ + entry_id: savedEntry.id, + people_id: existingPeople.id, + tmdb_credit_id: cast.credit_id, + character: cast.character, + order: cast.order, + profile_path: existingPeople.profile_path || null, + adult: existingPeople.adult || false, + cast_type: 'cast' + }); + } + + for (const crew of creditData.crew) { + // ? check if the crew already exists + let existingPeople = await People.findOne({ + where: { tmdb_id: crew.id } + }); + + // ? if not, create a new people + if (!existingPeople) { + existingPeople = await People.create({ + tmdb_id: crew.id, + name: crew.name, + gender: crew.gender, + popularity: crew.popularity, + known_for_department: crew.known_for_department || null, + profile_path: crew.profile_path, + adult: crew.adult || false, + last_fetch: new Date() + }); + } + + // ? create a new cast + await Credit.create({ + entry_id: savedEntry.id, + people_id: existingPeople.id, + tmdb_credit_id: crew.credit_id, + character: null, + order: null, + profile_path: existingPeople.profile_path || null, + adult: existingPeople.adult || false, + cast_type: 'crew', + department: crew.department || null, + job: crew.job || null + }); + } + + savedEntry.credit_fetched = true; + await savedEntry.save(); // ? media const downloadedImages = await _mediaManager.getEntryMedia(data.id, is_tvseries); @@ -252,7 +337,6 @@ class EntryService { _mediaOptimizerService.doGeneratePreview('entries', image.type + 's', image.path); } - // const currentGenresNames = await data.genres.map(genre => genre.name); // savedEntry.setDataValue('entryId', ids);