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
@@ -0,0 +1,142 @@
|
||||
const { Sequelize } = require("sequelize");
|
||||
const Entry = require("../../models/Entry");
|
||||
const EntryId = require("../../models/EntryId");
|
||||
const UserCollection = require("../../models/Collection");
|
||||
const { fetchCollectionData, fetchEntryData } = require("../tmdbService");
|
||||
const { downloadCollectionMedia, doGeneratePreview } = require("../mediaService");
|
||||
const Media = require("../../models/Media");
|
||||
const Collection = require("../../models/Collection");
|
||||
const { saveEntry } = require("./entryService");
|
||||
const { logToFile } = require("../logService");
|
||||
const EntryRating = require("../../models/EntryRating");
|
||||
const Season = require("../../models/Season");
|
||||
const Genre = require("../../models/Genre");
|
||||
const UserCollectionEntry = require("../../models/UserCollectionEntry");
|
||||
|
||||
/**
|
||||
* Creates or updates an official collection,
|
||||
* it also create an association to all related library entries
|
||||
* @collection_tmdbId tmdb id of the collection
|
||||
*/
|
||||
async function upsertOfficialCollection(collection_tmdbId) {
|
||||
let collectionData = await fetchCollectionData(collection_tmdbId);
|
||||
|
||||
// * 1 - collection info
|
||||
const [oldCollection, createdCollection] = await UserCollection.findOrCreate({
|
||||
where: { tmdbId: collectionData.id },
|
||||
defaults: {
|
||||
name: collectionData.name,
|
||||
posterPath: collectionData.poster_path,
|
||||
backdropPath: collectionData.backdrop_path,
|
||||
description: collectionData.overview
|
||||
},
|
||||
include: [{ model: Media, as: 'medias' }]
|
||||
});
|
||||
|
||||
// * 2 - collection entries
|
||||
let collection = oldCollection || createdCollection;
|
||||
if (collection) {
|
||||
const partIds = collectionData.parts.map(part => part.id);
|
||||
|
||||
const entries = await Entry.findAll({
|
||||
include: [{
|
||||
model: EntryId,
|
||||
as: 'entryId',
|
||||
where: {
|
||||
tmdb_id: {
|
||||
[Sequelize.Op.in]: partIds
|
||||
}
|
||||
}
|
||||
}]
|
||||
});
|
||||
|
||||
await collection.setEntries(entries);
|
||||
|
||||
// * 3 - collection media
|
||||
// ? downloading
|
||||
const downloadedImages = await downloadCollectionMedia(collection);
|
||||
|
||||
// ? generating preview & inserting data
|
||||
for (const image of downloadedImages) {
|
||||
// ? generating preview
|
||||
doGeneratePreview('collections', image.type + 's', image.path).then((result) => {
|
||||
const msg = `Collection Service | Generated Collection Preview`;
|
||||
console.log(msg);
|
||||
logToFile(msg);
|
||||
}).catch((err) => {
|
||||
const msg = `Collection Service | Error Generating Collection Preview`;
|
||||
console.log(msg);
|
||||
console.log(err);
|
||||
logToFile(msg);
|
||||
logToFile(err);
|
||||
});
|
||||
|
||||
// ? inserting data
|
||||
await Media.create({
|
||||
mediaable: 'collection',
|
||||
mediaableId: collection.id,
|
||||
type: image.type,
|
||||
path: image.path,
|
||||
isLocal: true,
|
||||
isSelected: true
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return await Collection.findByPk(collection.id, {
|
||||
include: [{
|
||||
model: Entry,
|
||||
as: 'entries',
|
||||
include: [
|
||||
{ model: EntryId, as: 'entryId' },
|
||||
{ model: EntryRating, as: 'entryRating' },
|
||||
{ model: Season, as: 'seasons' },
|
||||
{ model: Media, as: 'medias' },
|
||||
{ model: Genre, as: 'genres' },
|
||||
],
|
||||
through: {
|
||||
model: UserCollectionEntry,
|
||||
attributes: ['order'], // attributes (to include) from the join model
|
||||
},
|
||||
}],
|
||||
order: [[Sequelize.literal("`entries->UserCollectionEntry`.`order`"), 'ASC']], // sort by 'order' in ascending order
|
||||
});;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the other parts of the collection to the library
|
||||
* @collectionId id of the collection
|
||||
*/
|
||||
async function getCollectionParts(collectionId) {
|
||||
const collection = await Collection.findByPk(collectionId);
|
||||
const collectionData = await fetchCollectionData(collection.tmdbId);
|
||||
|
||||
const partIds = collectionData.parts.map(part => part.id);
|
||||
|
||||
const entries = await Entry.findAll({
|
||||
include: [{
|
||||
model: EntryId,
|
||||
as: 'entryId',
|
||||
where: {
|
||||
tmdb_id: {
|
||||
[Sequelize.Op.in]: partIds
|
||||
}
|
||||
}
|
||||
}]
|
||||
});
|
||||
|
||||
collectionData.parts.forEach(async (part) => {
|
||||
if (entries.filter((e) => e.entryId.tmdb_id == part.id).length == 0) {
|
||||
const isTvSeries = false;
|
||||
const entryData = await fetchEntryData(part.id, isTvSeries);
|
||||
const entry = await saveEntry(entryData);
|
||||
|
||||
await collection.addEntries([entry.id]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
upsertOfficialCollection,
|
||||
getCollectionParts,
|
||||
}
|
||||
@@ -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