Backend: Implemented people fetching on entry create/refrech

This commit is contained in:
X4Lo committed 2025-07-11 23:28:37 +01:00
1 parent b5ea25631d
commit 381f921df7
2 files changed
+181 -13

No files matched your search

+94 -10
View File
@@ -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