Refresh airing TV series when the calendar is opened
- POST /entries/sync-series starts a background refresh of airing TV series (not Ended/Canceled, auto sync on) not refreshed in the last 24h; manual refreshes reset that clock. GET kept as an alias, plus GET /entries/sync-series/status for progress - Reuses the rate-limited stale refresh job with a series-only scope; only one refresh job runs at a time - Calendar starts the sync on visit, shows progress, and reloads episodes and the library when it finishes - Remove the old SyncSeriesData, whose date check no longer worked
This commit is contained in:
1 parent
a88423163e
commit
ac0030d611
8 files changed
+119
-214
No files matched your search
@@ -565,210 +565,6 @@ class EntryManager {
|
||||
return entry;
|
||||
}
|
||||
|
||||
/** This function will refresh data of series that have the status not 'ended/canceled' and have not been refreshed today
|
||||
*/
|
||||
async SyncSeriesData() {
|
||||
const today = moment().format("YYYY-MM-DD");
|
||||
const entries = await Entry.findAll({
|
||||
where: {
|
||||
is_tvseries: true,
|
||||
autoSync: true,
|
||||
last_fetch: {
|
||||
[Op.ne]: today,
|
||||
},
|
||||
status: {
|
||||
[Op.notIn]: ["Ended", "Canceled"],
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
logToFile(`Started syncing ${entries.length} Tv-Series`);
|
||||
|
||||
const results = await Promise.all(
|
||||
entries.map((entry) => this.SyncSerieData(entry.id)),
|
||||
);
|
||||
logToFile(`Syncing Tv-Series Finished`);
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
async SyncSerieData(entryId) {
|
||||
const oldEntry = await Entry.findByPk(entryId, {
|
||||
include: [
|
||||
{ model: EntryId, as: "entryId" },
|
||||
{ model: EntryRating, as: "entryRating" },
|
||||
{ model: EntrySeen, as: "entrySeen" },
|
||||
{ model: Season, as: "seasons" },
|
||||
{ model: Media, as: "medias" },
|
||||
{
|
||||
model: Genre,
|
||||
as: "genres",
|
||||
attributes: ["id"],
|
||||
through: {
|
||||
attributes: [],
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
logToFile(
|
||||
`Sync Started For: ${oldEntry.entryId.tmdb_id}/${oldEntry.title}`,
|
||||
);
|
||||
|
||||
if (!oldEntry) throw NotFoundError("Entry Not Found");
|
||||
|
||||
const entryData = await fetchEntryData(
|
||||
oldEntry.entryId.tmdb_id,
|
||||
oldEntry.is_tvseries,
|
||||
);
|
||||
|
||||
if (!entryData) throw new ValidationError(`Couldn't Fetch Entry Data`);
|
||||
|
||||
// ! 1 - Normal Data
|
||||
let newData = {
|
||||
title: entryData.title || entryData.name,
|
||||
original_title: entryData.original_title || entryData.original_name,
|
||||
release_date: entryData.release_date || entryData.first_air_date,
|
||||
runtime: entryData.runtime || null,
|
||||
|
||||
tagline: entryData.tagline || null,
|
||||
storyline: entryData.overview,
|
||||
country:
|
||||
entryData.production_countries.length > 0
|
||||
? entryData.production_countries[0].name
|
||||
: null,
|
||||
|
||||
status: entryData.status,
|
||||
|
||||
poster_path: entryData.poster_path,
|
||||
banner_path: entryData.backdrop_path,
|
||||
|
||||
last_fetch: new Date(),
|
||||
};
|
||||
|
||||
await oldEntry.update(newData);
|
||||
|
||||
// ! 2 - Ids
|
||||
// TODO: get more ids
|
||||
await oldEntry.entryId.update({
|
||||
imdb_id: entryData.imdb_id,
|
||||
});
|
||||
|
||||
// ! 3 - Ratings
|
||||
await oldEntry.entryRating.update({
|
||||
tmdb_rating: entryData.vote_average,
|
||||
});
|
||||
|
||||
// ! 4 - Genres
|
||||
const currentGenresIds = oldEntry.genres.map((genre) => genre.id);
|
||||
entryData.genres.forEach(async (genre) => {
|
||||
// ? only assing the genre if its not already asigned
|
||||
if (!currentGenresIds.includes(genre.id)) {
|
||||
await EntryGenre.create({
|
||||
genre_id: genre.id,
|
||||
entry_id: oldEntry.id,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// ! 5 - Seasons
|
||||
// ? only update the last seasons available + season 0 (specials)
|
||||
if (oldEntry.is_tvseries) {
|
||||
const latestAvailableSeason = await Math.max(
|
||||
...oldEntry.seasons.map((s) => s.season_number),
|
||||
);
|
||||
for (const season of entryData.seasons) {
|
||||
if (
|
||||
season.season_number != 0 &&
|
||||
season.season_number != latestAvailableSeason
|
||||
)
|
||||
continue;
|
||||
|
||||
// ? fetch new season data from tmdb
|
||||
let seasonData = await fetchSeasonData(
|
||||
oldEntry.entryId.tmdb_id,
|
||||
season.season_number,
|
||||
);
|
||||
|
||||
// * find or create the season
|
||||
const [oldSeason, createdSeason] = await Season.findOrCreate({
|
||||
where: { tmdb_id: season.id },
|
||||
defaults: {
|
||||
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,
|
||||
},
|
||||
});
|
||||
|
||||
// ? if the season already exist then just update it with new data
|
||||
if (!createdSeason) {
|
||||
await oldSeason.update({
|
||||
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,
|
||||
});
|
||||
}
|
||||
|
||||
// * forging updated/created season into a variable
|
||||
let existingSeason = oldSeason || createdSeason;
|
||||
|
||||
// ! 6 - Episodes
|
||||
for (const episode of seasonData.episodes) {
|
||||
// * find or create the episode
|
||||
const [oldEpisode, createdEpisode] = await Episode.findOrCreate({
|
||||
where: { tmdb_id: episode.id },
|
||||
defaults: {
|
||||
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,
|
||||
},
|
||||
});
|
||||
|
||||
// ? if the episode already exist then just update it with new data
|
||||
if (!createdEpisode) {
|
||||
await oldEpisode.update({
|
||||
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,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await oldEntry.reload();
|
||||
|
||||
return oldEntry;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new EntryManager();
|
||||
@@ -575,14 +575,20 @@ class MaintenanceService {
|
||||
* Entries due for a TMDB refresh: never fetched, or last fetched more than
|
||||
* `hours` ago. Entries with auto sync turned off are skipped.
|
||||
* @param {number} [hours=24]
|
||||
* @param {{ seriesOnly?: boolean }} [options] - seriesOnly: only TV series
|
||||
* that are still airing (not Ended/Canceled), for the calendar sync
|
||||
*/
|
||||
async getStaleEntries(hours = 24) {
|
||||
async getStaleEntries(hours = 24, { seriesOnly = false } = {}) {
|
||||
const cutoff = new Date(Date.now() - hours * 60 * 60 * 1000);
|
||||
return Entry.findAll({
|
||||
attributes: ["id", "title", "last_fetch"],
|
||||
where: {
|
||||
autoSync: { [Op.not]: false },
|
||||
[Op.or]: [{ last_fetch: null }, { last_fetch: { [Op.lt]: cutoff } }],
|
||||
...(seriesOnly && {
|
||||
is_tvseries: true,
|
||||
status: { [Op.or]: [null, { [Op.notIn]: ["Ended", "Canceled"] }] },
|
||||
}),
|
||||
},
|
||||
order: [["last_fetch", "ASC"]],
|
||||
});
|
||||
@@ -600,18 +606,20 @@ class MaintenanceService {
|
||||
* TMDB requests go through tmdbService's rate limiter, so a large library
|
||||
* is spread out instead of hammering the API.
|
||||
* @param {number} [hours=24]
|
||||
* @param {{ seriesOnly?: boolean }} [options] - see getStaleEntries
|
||||
* @returns {Object} the job status, or `{ alreadyRunning: true, ... }`
|
||||
*/
|
||||
async startStaleRefresh(hours = 24) {
|
||||
async startStaleRefresh(hours = 24, options = {}) {
|
||||
if (this.staleRefreshJob?.running) {
|
||||
return { alreadyRunning: true, ...this.getStaleRefreshStatus() };
|
||||
}
|
||||
|
||||
const entries = await this.getStaleEntries(hours);
|
||||
const entries = await this.getStaleEntries(hours, options);
|
||||
const job = {
|
||||
running: true,
|
||||
cancelRequested: false,
|
||||
hours,
|
||||
scope: options.seriesOnly ? "series" : "all",
|
||||
total: entries.length,
|
||||
done: 0,
|
||||
succeeded: 0,
|
||||
|
||||
Reference in new issue
Block a user