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 EntrySeen = require("../models/EntrySeen.js");
const Media = require("../models/Media.js"); const Media = require("../models/Media.js");
const Genre = require("../models/Genre.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 BadRequestError = require("../errors/BadRequestError");
const NotFoundError = require("../errors/NotFoundError.js"); const NotFoundError = require("../errors/NotFoundError.js");
const ValidationError = require("../errors/ValidationError.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 { logToFile } = require("./logService.js");
const collectionManager = require('./collectionManager.js'); const collectionManager = require('./collectionManager.js');
const entryService = require("./entryService"); const entryService = require("./entryService");
const mediaManager = require("./mediaManager.js"); const mediaManager = require("./mediaManager.js");
const EntryNetwork = require("../models/EntryNetwork.js");
const networkService = require("./networkService.js"); const networkService = require("./networkService.js");
const Network = require("../models/Network.js");
const _collectionManager = collectionManager; const _collectionManager = collectionManager;
const _entryService = entryService; const _entryService = entryService;
@@ -66,6 +66,8 @@ class EntryManager {
// todo: change fetchEntryData to TMDBService.fetchEntryData() // todo: change fetchEntryData to TMDBService.fetchEntryData()
// todo: change fetchEntryData to TMDBService.fetchSeasonData() // todo: change fetchEntryData to TMDBService.fetchSeasonData()
// todo: change fetchEntryData to TMDBService.fetchEntryCredits()
// todo: change fetchEntryData to TMDBService.fetchPersonDetails()
async refrechEntryById(id, plain = true) { async refrechEntryById(id, plain = true) {
if (!id) { if (!id) {
throw new BadRequestError("Entry id not provided"); 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); _mediaManager.getSeasonMedia(existingSeason);
// * Update or insert episodes for the season // * 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); _mediaManager.getEpisodenMedia(existingEpisode);
} }
} }
} }
// * Cast // * Credits
// ? Only fetch if the cast count is 0 or less than max_cast if (!oldEntry.cast_fetched || oldEntry.credits.length == 0) {
// TODO add cast refrech const creditData = await fetchEntryCredits(oldEntry.entryId.tmdb_id, oldEntry.is_tvseries);
// if (!oldEntry.casts || oldEntry.casts.length == 0 || oldEntry.casts.length < oldEntry.max_cast) { 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 // * Media
// ? Only fetch if a Poster, Backdrop or a Logo is missing // ? Only fetch if a Poster, Backdrop or a Logo is missing
+87 -3
View File
@@ -14,9 +14,12 @@ const NotFoundError = require("../errors/NotFoundError");
const mediaOptimizerService = require("./mediaOptimizerService.js"); const mediaOptimizerService = require("./mediaOptimizerService.js");
const mediaManager = require("./mediaManager.js"); const mediaManager = require("./mediaManager.js");
const { fetchSeasonData } = require("./tmdbService.js"); const { fetchSeasonData, fetchEntryCredits } = require("./tmdbService.js");
const Network = require("../models/Network.js"); const Network = require("../models/Network.js");
const networkService = require("./networkService.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 _mediaManager = mediaManager;
const _mediaOptimizerService = mediaOptimizerService; const _mediaOptimizerService = mediaOptimizerService;
@@ -72,6 +75,12 @@ class EntryService {
{ model: Season, as: 'seasons' }, { model: Season, as: 'seasons' },
{ model: Media, as: 'medias' }, { model: Media, as: 'medias' },
{ model: Network, as: 'networks' }, { model: Network, as: 'networks' },
{
model: Cast, as: 'credits',
include: [
{ model: People, as: 'person' }
]
},
{ {
model: Collection, as: 'collections', model: Collection, as: 'collections',
include: [ include: [
@@ -233,8 +242,84 @@ class EntryService {
} }
// + end of: seasons & episodes for tv series // + 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 // ? media
const downloadedImages = await _mediaManager.getEntryMedia(data.id, is_tvseries); const downloadedImages = await _mediaManager.getEntryMedia(data.id, is_tvseries);
@@ -252,7 +337,6 @@ class EntryService {
_mediaOptimizerService.doGeneratePreview('entries', image.type + 's', image.path); _mediaOptimizerService.doGeneratePreview('entries', image.type + 's', image.path);
} }
// const currentGenresNames = await data.genres.map(genre => genre.name); // const currentGenresNames = await data.genres.map(genre => genre.name);
// savedEntry.setDataValue('entryId', ids); // savedEntry.setDataValue('entryId', ids);