From 17d15144ebe9e8176cbef946acf089f58aa32f63 Mon Sep 17 00:00:00 2001 From: advplyr Date: Wed, 11 May 2022 18:55:19 -0500 Subject: [PATCH] Update:Podcast new episode check cronjob to use last episode pub date if exists otherwise fallback to using last check date --- server/managers/PodcastManager.js | 24 ++++++++++++------------ server/objects/mediaTypes/Podcast.js | 9 +++++++++ 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/server/managers/PodcastManager.js b/server/managers/PodcastManager.js index 347ccecc..b15698aa 100644 --- a/server/managers/PodcastManager.js +++ b/server/managers/PodcastManager.js @@ -183,8 +183,15 @@ class PodcastManager { for (const libraryItem of podcastsWithAutoDownload) { const lastEpisodeCheckDate = new Date(libraryItem.media.lastEpisodeCheck || 0) - Logger.info(`[PodcastManager] checkForNewEpisodes Cron for "${libraryItem.media.metadata.title}" - Last episode check: ${lastEpisodeCheckDate}`) - var newEpisodes = await this.checkPodcastForNewEpisodes(libraryItem) + const latestEpisodePublishedAt = libraryItem.media.latestEpisodePublished + Logger.info(`[PodcastManager] checkForNewEpisodes: "${libraryItem.media.metadata.title}" | Last check: ${lastEpisodeCheckDate} | ${latestEpisodePublishedAt ? `Latest episode pubDate: ${new Date(latestEpisodePublishedAt)}` : 'No latest episode'}`) + + // Use latest episode pubDate if exists OR fallback to using lastEpisodeCheckDate + // lastEpisodeCheckDate will be the current time when adding a new podcast + const dateToCheckForEpisodesAfter = latestEpisodePublishedAt || lastEpisodeCheckDate + Logger.debug(`[PodcastManager] checkForNewEpisodes: "${libraryItem.media.metadata.title}" checking for episodes after ${new Date(dateToCheckForEpisodesAfter)}`) + + var newEpisodes = await this.checkPodcastForNewEpisodes(libraryItem, dateToCheckForEpisodesAfter) Logger.debug(`[PodcastManager] checkForNewEpisodes checked result ${newEpisodes ? newEpisodes.length : 'N/A'}`) if (!newEpisodes) { // Failed @@ -214,7 +221,7 @@ class PodcastManager { } } - async checkPodcastForNewEpisodes(podcastLibraryItem) { + async checkPodcastForNewEpisodes(podcastLibraryItem, dateToCheckForEpisodesAfter) { if (!podcastLibraryItem.media.metadata.feedUrl) { Logger.error(`[PodcastManager] checkPodcastForNewEpisodes no feed url for ${podcastLibraryItem.media.metadata.title} (ID: ${podcastLibraryItem.id})`) return false @@ -225,15 +232,8 @@ class PodcastManager { return false } - // Added for testing - Logger.debug(`[PodcastManager] checkPodcastForNewEpisodes: ${feed.episodes.length} episodes in feed for "${podcastLibraryItem.media.metadata.title}"`) - const latestEpisodes = feed.episodes.slice(0, 3) - latestEpisodes.forEach((ep) => { - Logger.debug(`[PodcastManager] checkPodcastForNewEpisodes: Recent episode "${ep.title}", pubDate=${ep.pubDate}, publishedAt=${ep.publishedAt}/${new Date(ep.publishedAt)} for "${podcastLibraryItem.media.metadata.title}"`) - }) - // Filter new and not already has - var newEpisodes = feed.episodes.filter(ep => ep.publishedAt > podcastLibraryItem.media.lastEpisodeCheck && !podcastLibraryItem.media.checkHasEpisodeByFeedUrl(ep.enclosure.url)) + var newEpisodes = feed.episodes.filter(ep => ep.publishedAt > dateToCheckForEpisodesAfter && !podcastLibraryItem.media.checkHasEpisodeByFeedUrl(ep.enclosure.url)) // Max new episodes for safety = 3 newEpisodes = newEpisodes.slice(0, 3) return newEpisodes @@ -242,7 +242,7 @@ class PodcastManager { async checkAndDownloadNewEpisodes(libraryItem) { const lastEpisodeCheckDate = new Date(libraryItem.media.lastEpisodeCheck || 0) Logger.info(`[PodcastManager] checkAndDownloadNewEpisodes for "${libraryItem.media.metadata.title}" - Last episode check: ${lastEpisodeCheckDate}`) - var newEpisodes = await this.checkPodcastForNewEpisodes(libraryItem) + var newEpisodes = await this.checkPodcastForNewEpisodes(libraryItem, libraryItem.media.lastEpisodeCheck) if (newEpisodes.length) { Logger.info(`[PodcastManager] Found ${newEpisodes.length} new episodes for podcast "${libraryItem.media.metadata.title}" - starting download`) this.downloadPodcastEpisodes(libraryItem, newEpisodes) diff --git a/server/objects/mediaTypes/Podcast.js b/server/objects/mediaTypes/Podcast.js index 3a609968..55ae0c15 100644 --- a/server/objects/mediaTypes/Podcast.js +++ b/server/objects/mediaTypes/Podcast.js @@ -104,6 +104,15 @@ class Podcast { get numTracks() { return this.episodes.length } + get latestEpisodePublished() { + var largestPublishedAt = 0 + this.episodes.forEach((ep) => { + if (ep.publishedAt && ep.publishedAt > largestPublishedAt) { + largestPublishedAt = ep.publishedAt + } + }) + return largestPublishedAt + } update(payload) { var json = this.toJSON()