Major rework of media handelling, and collections
This commit is contained in:
1 parent
b42e055691
commit
f7cb2bed53
37 files changed
+1120
-411
No files matched your search
@@ -1,3 +1,14 @@
|
||||
const moment = require('../../utils/moment.js');
|
||||
|
||||
const { fetchSeasonData, fetchEntryData } = require('../tmdbService.js');
|
||||
const { getEntryMedia, doGeneratePreview } = require('../mediaService.js');
|
||||
const { logToFile } = require('../logService.js');
|
||||
|
||||
const NotFoundError = require('../../errors/NotFoundError.js');
|
||||
const InternalServerError = require('../../errors/InternalServerError.js');
|
||||
|
||||
const { ValidationError, Op } = require('sequelize');
|
||||
const Collection = require('../../models/Collection.js');
|
||||
const Entry = require('../../models/Entry.js')
|
||||
const EntryId = require('../../models/EntryId.js')
|
||||
const EntryRating = require('../../models/EntryRating.js');
|
||||
@@ -8,15 +19,6 @@ const Episode = require('../../models/Episode.js');
|
||||
const Media = require('../../models/Media.js');
|
||||
const Genre = require('../../models/Genre.js');
|
||||
|
||||
const NotFoundError = require('../../errors/NotFoundError.js');
|
||||
const InternalServerError = require('../../errors/InternalServerError.js');
|
||||
|
||||
const { fetchSeasonData, fetchEntryData } = require('../tmdbService.js');
|
||||
const { getEntryMedia } = require('../mediaService.js');
|
||||
const { logToFile } = require('../logService.js');
|
||||
const { ValidationError, Op } = require('sequelize');
|
||||
const moment = require('../../utils/moment.js');
|
||||
|
||||
async function saveEntry(data) {
|
||||
try {
|
||||
if (data == null) return;
|
||||
@@ -132,7 +134,7 @@ async function saveEntry(data) {
|
||||
|
||||
// ? media
|
||||
const downloadedImages = await getEntryMedia(data.id, is_tvseries);
|
||||
console.log(`entry service, downloaded images: `, downloadedImages)
|
||||
// console.log(`entry service, downloaded images: `, downloadedImages)
|
||||
for (const image of downloadedImages) {
|
||||
await Media.create({
|
||||
mediaable: 'entry',
|
||||
@@ -141,7 +143,9 @@ async function saveEntry(data) {
|
||||
path: image.path,
|
||||
isLocal: true,
|
||||
isSelected: true
|
||||
})
|
||||
});
|
||||
|
||||
doGeneratePreview('entries', image.type + 's', image.path);
|
||||
}
|
||||
|
||||
|
||||
@@ -348,27 +352,27 @@ async function refrechEntry(entryId) {
|
||||
}
|
||||
|
||||
// ? getting new data from TMDB
|
||||
const data = await fetchEntryData(oldEntry.entryId.tmdb_id, oldEntry.is_tvseries);
|
||||
if (data == null) {
|
||||
const entryData = await fetchEntryData(oldEntry.entryId.tmdb_id, oldEntry.is_tvseries);
|
||||
if (entryData == null) {
|
||||
throw new InternalServerError(`Counldn't fetch data from TMDB`);
|
||||
}
|
||||
|
||||
// ! the refrech logic
|
||||
let newData = {
|
||||
title: data.title || data.name,
|
||||
original_title: data.original_title || data.original_name,
|
||||
release_date: data.release_date || data.first_air_date,
|
||||
release_status: data.status,
|
||||
runtime: data.runtime || null,
|
||||
title: entryData.title || entryData.name,
|
||||
original_title: entryData.original_title || entryData.original_name,
|
||||
release_date: entryData.release_date || entryData.first_air_date,
|
||||
release_status: entryData.status,
|
||||
runtime: entryData.runtime || null,
|
||||
|
||||
tagline: data.tagline || null,
|
||||
storyline: data.overview,
|
||||
country: data.production_countries.length > 0 ? data.production_countries[0].name : null,
|
||||
tagline: entryData.tagline || null,
|
||||
storyline: entryData.overview,
|
||||
country: entryData.production_countries.length > 0 ? entryData.production_countries[0].name : null,
|
||||
|
||||
status: data.status,
|
||||
status: entryData.status,
|
||||
|
||||
poster_path: data.poster_path,
|
||||
banner_path: data.backdrop_path,
|
||||
poster_path: entryData.poster_path,
|
||||
banner_path: entryData.backdrop_path,
|
||||
|
||||
last_fetch: new Date()
|
||||
};
|
||||
@@ -378,17 +382,17 @@ async function refrechEntry(entryId) {
|
||||
// * updating ids
|
||||
// TODO: get more ids
|
||||
await oldEntry.entryId.update({
|
||||
imdb_id: data.imdb_id,
|
||||
imdb_id: entryData.imdb_id,
|
||||
});
|
||||
|
||||
// * updating ratings
|
||||
await oldEntry.entryRating.update({
|
||||
tmdb_rating: data.vote_average
|
||||
tmdb_rating: entryData.vote_average
|
||||
});
|
||||
|
||||
// * updating genres
|
||||
const currentGenresIds = oldEntry.genres.map(genre => genre.id);
|
||||
data.genres.forEach(async (genre) => {
|
||||
entryData.genres.forEach(async (genre) => {
|
||||
// if the entry already has this genre then skip
|
||||
if (currentGenresIds.includes(genre.id)) return;
|
||||
|
||||
@@ -406,9 +410,9 @@ async function refrechEntry(entryId) {
|
||||
// ? and atleast 48 hours has passed
|
||||
|
||||
// * Update or insert seasons for the entry
|
||||
for (const season of data.seasons) {
|
||||
for (const season of entryData.seasons) {
|
||||
// fetch new season data from tmdb
|
||||
let seasonData = await fetchSeasonData(data.id, season.season_number);
|
||||
let seasonData = await fetchSeasonData(entryData.id, season.season_number);
|
||||
|
||||
// Fetch existing season if it exists (using tmdb_id)
|
||||
let existingSeason = await Season.findOne({
|
||||
@@ -498,32 +502,27 @@ async function refrechEntry(entryId) {
|
||||
|
||||
// * Media
|
||||
// ? Only fetch if a Poster, Backdrop or a Logo is missing
|
||||
// ? only get missing media types
|
||||
|
||||
// ? count media by type
|
||||
let availableMedia = {
|
||||
'posters': 0,
|
||||
'logos': 0,
|
||||
'backdrops': 0
|
||||
};
|
||||
for (let i = 0; i < oldEntry.medias.length; i++) {
|
||||
availableMedia[oldEntry.medias[i].type + 's'] += 1;
|
||||
}
|
||||
|
||||
// ? if the count of a media type == 0, the add the type to mediaToGet
|
||||
let mediaToGet = [];
|
||||
for (let key in availableMedia) {
|
||||
if (availableMedia[key] == 0) mediaToGet.push(key);
|
||||
}
|
||||
|
||||
let downloadedImages;
|
||||
if (oldEntry.medias.length < 3) {
|
||||
// ? if no media is available, directly get all
|
||||
downloadedImages = await getEntryMedia(data.id, oldEntry.is_tvseries);
|
||||
} else {
|
||||
// ? only get missing media types
|
||||
let availableMedia = {
|
||||
'posters': 0,
|
||||
'logos': 0,
|
||||
'backdrops': 0
|
||||
};
|
||||
|
||||
// ? count media by type
|
||||
for (let i = 0; i < oldEntry.medias.length; i++) {
|
||||
availableMedia[oldEntry.medias[i].type] += 1;
|
||||
}
|
||||
|
||||
// ? if the count of a media type == 0, the add the type to mediaToGet
|
||||
let mediaToGet = [];
|
||||
for (let key in availableMedia) {
|
||||
if (availableMedia[key] == 0) mediaToGet.push(availableMedia[key]);
|
||||
}
|
||||
|
||||
if (mediaToGet.length != 0) {
|
||||
downloadedImages = await getEntryMedia(data.id, oldEntry.is_tvseries, mediaToGet);
|
||||
}
|
||||
if (mediaToGet.length != 0) {
|
||||
downloadedImages = await getEntryMedia(entryData.id, oldEntry.is_tvseries, mediaToGet);
|
||||
}
|
||||
|
||||
if (downloadedImages) {
|
||||
@@ -539,6 +538,12 @@ async function refrechEntry(entryId) {
|
||||
}
|
||||
}
|
||||
|
||||
// ? fetch collection
|
||||
if (entryData.belongs_to_collection) {
|
||||
const collectionService = require('./collectionService.js');
|
||||
await collectionService.upsertOfficialCollection(entryData.belongs_to_collection.id);
|
||||
}
|
||||
|
||||
// + end of: refrech logic
|
||||
|
||||
// ! returning the refreshed entry
|
||||
@@ -548,6 +553,7 @@ async function refrechEntry(entryId) {
|
||||
{ model: EntrySeen, as: 'entrySeen' },
|
||||
{ model: Season, as: 'seasons' },
|
||||
{ model: Media, as: 'medias' },
|
||||
{ model: Collection, as: 'collections' },
|
||||
{
|
||||
model: Genre,
|
||||
as: 'genres',
|
||||
@@ -566,8 +572,8 @@ async function refrechEntry(entryId) {
|
||||
]
|
||||
});
|
||||
|
||||
const entryData = updatedEntry.get({ plain: true });
|
||||
entryData.genres = entryData.genres.map(genre => genre.name);
|
||||
const plainEntry = updatedEntry.get({ plain: true });
|
||||
plainEntry.genres = plainEntry.genres.map(genre => genre.name);
|
||||
|
||||
return entryData;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user