implemented season/episode fetch + update

This commit is contained in:
X4Lo committed 2024-04-20 15:46:33 +01:00
1 parent 4fdb670510
commit 90a269d449
1 file changed
+152 -46
+152 -46
View File
@@ -4,6 +4,8 @@ const EntryRating = require('../models/EntryRating.js');
const Season = require('../models/Season.js'); const Season = require('../models/Season.js');
const EntryGenre = require('../models/EntryGenre.js'); const EntryGenre = require('../models/EntryGenre.js');
const EntrySeen = require('../models/EntrySeen.js'); const EntrySeen = require('../models/EntrySeen.js');
const { fetchSeasonData } = require('./tmdbService.js');
const Episode = require('../models/Episode.js');
async function saveEntry(data) { async function saveEntry(data) {
// creating the object // creating the object
@@ -28,62 +30,86 @@ async function saveEntry(data) {
banner_path: data.backdrop_path, banner_path: data.backdrop_path,
}); });
const savedMovie = await movie.save(); const savedEntry = await movie.save();
// genres // genres
data.genres.forEach(async (genre) => { data.genres.forEach(async (genre) => {
await EntryGenre.create({ await EntryGenre.create({
genre_id: genre.id, genre_id: genre.id,
entry_id: savedMovie.id entry_id: savedEntry.id
}) })
}); });
const ratings = await EntryRating.create({ const ratings = await EntryRating.create({
entry_id: savedMovie.id, entry_id: savedEntry.id,
user_rating: 0, user_rating: 0,
tmdb_rating: data.vote_average, tmdb_rating: data.vote_average,
}); });
const ids = await EntryId.create({ const ids = await EntryId.create({
entry_id: savedMovie.id, entry_id: savedEntry.id,
tmdb_id: data.id, tmdb_id: data.id,
imdb_id: data.imdb_id, imdb_id: data.imdb_id,
}); });
let seasons = [];
if (is_tvseries) { if (is_tvseries) {
data.seasons.forEach(async (season) => { let seasons = [];
const s = await Season.create({ for (const season of data.seasons) {
tmdb_id: season.id, let seasonData = await fetchSeasonData(data.id, season.season_number);
const savedSeason = await Season.create({
tmdb_id: seasonData.id,
entry_id: savedMovie.id, entry_id: savedEntry.id,
name: season.name, name: seasonData.name,
overview: season.overview, overview: seasonData.overview,
season_number: season.season_number, season_number: seasonData.season_number,
air_date: season.air_date, episode_count: seasonData.episodes.length,
air_date: seasonData.air_date,
user_rating: 0, user_rating: 0,
tmdb_rating: season.vote_average, tmdb_rating: seasonData.vote_average,
poster_path: season.poster_path, poster_path: seasonData.poster_path,
banner_path: null, banner_path: null,
}) })
seasons.push(s) // save episodes
}); let episodes = [];
for (const episode of seasonData.episodes) {
const savedEpisode = await Episode.create({
tmdb_id: episode.id,
await savedMovie.setDataValue('seasons', seasons); season_id: savedSeason.id,
name: episode.name,
overview: episode.overview,
episode_number: episode.episode_number,
episode_type: episode.episode_type,
runtime: episode.runtime,
air_date: episode.air_date,
tmdb_rating: episode.vote_average,
still_path: episode.still_path,
});
episodes.push(savedEpisode);
}
savedSeason.setDataValue('episodes', episodes);
seasons.push(savedSeason)
}
savedEntry.setDataValue('seasons', seasons);
} }
const currentGenresNames = await data.genres.map(genre => genre.name); const currentGenresNames = await data.genres.map(genre => genre.name);
savedEntry.setDataValue('entryId', ids);
savedEntry.setDataValue('entryRating', ratings);
savedEntry.setDataValue('genres', currentGenresNames);
savedMovie.setDataValue('entryId', ids); return savedEntry;
savedMovie.setDataValue('entryRating', ratings);
savedMovie.setDataValue('genres', currentGenresNames);
return savedMovie;
} }
async function updateEntry(oldEntry, data) { async function updateEntry(oldEntry, data) {
@@ -121,47 +147,127 @@ async function updateEntry(oldEntry, data) {
const currentGenresIds = oldEntry.genres.map(genre => genre.id); const currentGenresIds = oldEntry.genres.map(genre => genre.id);
let currentGenresNames = []; let currentGenresNames = [];
data.genres.forEach(async (genre) => { data.genres.forEach(async (genre) => {
// add genres names to an array so it can be returned with the entry object
currentGenresNames.push(genre.name); currentGenresNames.push(genre.name);
// if the entry already has this genre then skip
if (currentGenresIds.includes(genre.id)) return; if (currentGenresIds.includes(genre.id)) return;
// otherwise assign the genre to the entry
await EntryGenre.create({ await EntryGenre.create({
genre_id: genre.id, genre_id: genre.id,
entry_id: savedEntry.id entry_id: savedEntry.id
}) })
}); });
// entry seens // fetch entry-seens
const entrySeens = await EntrySeen.findAll({ where: { entry_id: oldEntry.id } }); const entrySeens = await EntrySeen.findAll({ where: { entry_id: oldEntry.id } });
// getting updated genres name
// const currentGenresNames = await data.genres.map((genre) => genre.name);
// TODO: refrech seasons // TODO: refrech seasons
// let seasons = []; if (oldEntry.is_tvseries) {
// if (oldEntry.is_tvseries) { let updatedSeasons = [];
// data.seasons.forEach(async (season) => {
// const s = await Season.create({
// tmdb_id: season.id,
// entry_id: savedMovie.id, for (const season of data.seasons) {
// fetch new season data from tmdb
let seasonData = await fetchSeasonData(data.id, season.season_number);
// name: season.name, // Fetch existing season if it exists (using tmdb_id)
// overview: season.overview, let existingSeason = await Season.findOne({
// season_number: season.season_number, where: {
// air_date: season.air_date, tmdb_id: season.id
}
});
// user_rating: 0, // If the season exists, update it; otherwise, create a new season
// tmdb_rating: season.vote_average, if (existingSeason) {
// Update existing season data
existingSeason.tmdb_id = seasonData.id;
existingSeason.name = seasonData.name;
existingSeason.overview = seasonData.overview;
existingSeason.episode_count = seasonData.episodes.length;
existingSeason.air_date = seasonData.air_date;
existingSeason.tmdb_rating = seasonData.vote_average;
existingSeason.poster_path = seasonData.poster_path;
existingSeason.banner_path = null;
// poster_path: season.poster_path, // Save the updated season
// banner_path: null, await existingSeason.save();
// }) } else {
// Create a new season
const savedSeason = await Season.create({
tmdb_id: seasonData.id,
entry_id: oldEntry.id,
name: seasonData.name,
overview: seasonData.overview,
season_number: seasonData.season_number,
episode_count: seasonData.episodes.length,
air_date: seasonData.air_date,
user_rating: 0,
tmdb_rating: seasonData.vote_average,
poster_path: seasonData.poster_path,
banner_path: null,
});
// seasons.push(s) // Add the newly created season to the list of updated seasons
// }); existingSeason = savedSeason;
}
console.log(season.episodes)
// Update or insert episodes for the season
let updatedEpisodes = [];
for (const episode of seasonData.episodes) {
// Fetch existing episode data if it exists
let existingEpisode = await Episode.findOne({
where: {
tmdb_id: episode.id,
}
});
// If the episode exists, update it; otherwise, create a new episode
if (existingEpisode) {
// Update existing episode data
existingEpisode.tmdb_id = episode.tmdb_id;
existingEpisode.name = episode.name;
existingEpisode.overview = episode.overview;
existingEpisode.episode_type = episode.episode_type;
existingEpisode.runtime = episode.runtime;
existingEpisode.air_date = episode.air_date;
existingEpisode.tmdb_rating = episode.vote_average;
existingEpisode.still_path = episode.still_path;
// Save the updated episode
await existingEpisode.save();
} else {
// Create a new episode
const savedEpisode = await Episode.create({
tmdb_id: episode.id,
season_id: existingSeason.id,
name: episode.name,
overview: episode.overview,
episode_number: episode.episode_number,
episode_type: episode.episode_type,
runtime: episode.runtime,
air_date: episode.air_date,
tmdb_rating: episode.vote_average,
still_path: episode.still_path,
});
// Add the newly created episode to the list of updated episodes
existingEpisode = savedEpisode;
}
updatedEpisodes.push(existingEpisode);
}
// Update the episodes of the current season
existingSeason.setDataValue('episodes', updatedEpisodes);
updatedSeasons.push(existingSeason);
}
// Update the seasons of the movie
savedEntry.setDataValue('seasons', updatedSeasons);
}
// await savedMovie.setDataValue('seasons', seasons);
// }
savedEntry.setDataValue('entryId', ids); savedEntry.setDataValue('entryId', ids);
savedEntry.setDataValue('entryRating', ratings); savedEntry.setDataValue('entryRating', ratings);