2022-03-22 01:24:38 +01:00
const fs = require ( 'fs-extra' )
2022-06-08 03:04:51 +02:00
const cron = require ( '../libs/nodeCron' )
2022-03-26 17:59:34 +01:00
const axios = require ( 'axios' )
const { parsePodcastRssFeedXml } = require ( '../utils/podcastUtils' )
2022-03-22 01:24:38 +01:00
const Logger = require ( '../Logger' )
const { downloadFile } = require ( '../utils/fileUtils' )
2022-05-29 18:46:45 +02:00
const opmlParser = require ( '../utils/parsers/parseOPML' )
2022-03-22 01:24:38 +01:00
const prober = require ( '../utils/prober' )
const LibraryFile = require ( '../objects/files/LibraryFile' )
const PodcastEpisodeDownload = require ( '../objects/PodcastEpisodeDownload' )
const PodcastEpisode = require ( '../objects/entities/PodcastEpisode' )
const AudioFile = require ( '../objects/files/AudioFile' )
2022-03-20 22:41:06 +01:00
class PodcastManager {
2022-03-22 01:24:38 +01:00
constructor ( db , watcher , emitter ) {
2022-03-20 22:41:06 +01:00
this . db = db
2022-03-22 01:24:38 +01:00
this . watcher = watcher
this . emitter = emitter
2022-03-20 22:41:06 +01:00
this . downloadQueue = [ ]
2022-03-22 01:24:38 +01:00
this . currentDownload = null
2022-03-26 17:59:34 +01:00
this . episodeScheduleTask = null
2022-05-02 02:54:33 +02:00
this . failedCheckMap = { }
2022-03-26 17:59:34 +01:00
}
2022-03-27 01:58:59 +01:00
get serverSettings ( ) {
return this . db . serverSettings || { }
}
2022-03-26 17:59:34 +01:00
init ( ) {
2022-03-27 01:58:59 +01:00
var podcastsWithAutoDownload = this . db . libraryItems . some ( li => li . mediaType === 'podcast' && li . media . autoDownloadEpisodes )
if ( podcastsWithAutoDownload ) {
2022-03-26 17:59:34 +01:00
this . schedulePodcastEpisodeCron ( )
}
2022-03-22 01:24:38 +01:00
}
2022-04-24 02:41:06 +02:00
getEpisodeDownloadsInQueue ( libraryItemId ) {
return this . downloadQueue . filter ( d => d . libraryItemId === libraryItemId )
}
clearDownloadQueue ( libraryItemId = null ) {
if ( ! this . downloadQueue . length ) return
if ( ! libraryItemId ) {
Logger . info ( ` [PodcastManager] Clearing all downloads in queue ( ${ this . downloadQueue . length } ) ` )
this . downloadQueue = [ ]
} else {
var itemDownloads = this . getEpisodeDownloadsInQueue ( libraryItemId )
Logger . info ( ` [PodcastManager] Clearing downloads in queue for item " ${ libraryItemId } " ( ${ itemDownloads . length } ) ` )
this . downloadQueue = this . downloadQueue . filter ( d => d . libraryItemId !== libraryItemId )
}
}
2022-03-22 01:24:38 +01:00
async downloadPodcastEpisodes ( libraryItem , episodesToDownload ) {
2022-03-27 01:58:59 +01:00
var index = libraryItem . media . episodes . length + 1
2022-03-22 01:24:38 +01:00
episodesToDownload . forEach ( ( ep ) => {
var newPe = new PodcastEpisode ( )
newPe . setData ( ep , index ++ )
2022-04-06 02:40:40 +02:00
newPe . libraryItemId = libraryItem . id
2022-03-22 01:24:38 +01:00
var newPeDl = new PodcastEpisodeDownload ( )
newPeDl . setData ( newPe , libraryItem )
this . startPodcastEpisodeDownload ( newPeDl )
} )
2022-03-20 22:41:06 +01:00
}
2022-03-22 01:24:38 +01:00
async startPodcastEpisodeDownload ( podcastEpisodeDownload ) {
if ( this . currentDownload ) {
this . downloadQueue . push ( podcastEpisodeDownload )
2022-04-24 02:41:06 +02:00
this . emitter ( 'episode_download_queued' , podcastEpisodeDownload . toJSONForClient ( ) )
2022-03-22 01:24:38 +01:00
return
}
2022-04-24 02:41:06 +02:00
this . emitter ( 'episode_download_started' , podcastEpisodeDownload . toJSONForClient ( ) )
2022-03-22 01:24:38 +01:00
this . currentDownload = podcastEpisodeDownload
// Ignores all added files to this dir
this . watcher . addIgnoreDir ( this . currentDownload . libraryItem . path )
var success = await downloadFile ( this . currentDownload . url , this . currentDownload . targetPath ) . then ( ( ) => true ) . catch ( ( error ) => {
Logger . error ( ` [PodcastManager] Podcast Episode download failed ` , error )
return false
} )
if ( success ) {
success = await this . scanAddPodcastEpisodeAudioFile ( )
if ( ! success ) {
await fs . remove ( this . currentDownload . targetPath )
2022-04-24 02:41:06 +02:00
this . currentDownload . setFinished ( false )
2022-03-22 01:24:38 +01:00
} else {
Logger . info ( ` [PodcastManager] Successfully downloaded podcast episode " ${ this . currentDownload . podcastEpisode . title } " ` )
2022-04-24 02:41:06 +02:00
this . currentDownload . setFinished ( true )
2022-03-22 01:24:38 +01:00
}
2022-04-24 02:41:06 +02:00
} else {
this . currentDownload . setFinished ( false )
2022-03-22 01:24:38 +01:00
}
2022-04-24 02:41:06 +02:00
this . emitter ( 'episode_download_finished' , this . currentDownload . toJSONForClient ( ) )
2022-03-22 01:24:38 +01:00
this . watcher . removeIgnoreDir ( this . currentDownload . libraryItem . path )
this . currentDownload = null
if ( this . downloadQueue . length ) {
this . startPodcastEpisodeDownload ( this . downloadQueue . shift ( ) )
}
}
async scanAddPodcastEpisodeAudioFile ( ) {
var libraryFile = await this . getLibraryFile ( this . currentDownload . targetPath , this . currentDownload . targetRelPath )
2022-04-13 00:32:27 +02:00
// TODO: Set meta tags on new audio file
2022-03-22 01:24:38 +01:00
var audioFile = await this . probeAudioFile ( libraryFile )
if ( ! audioFile ) {
return false
}
var libraryItem = this . db . libraryItems . find ( li => li . id === this . currentDownload . libraryItem . id )
if ( ! libraryItem ) {
Logger . error ( ` [PodcastManager] Podcast Episode finished but library item was not found ${ this . currentDownload . libraryItem . id } ` )
return false
}
2022-03-27 00:23:33 +01:00
2022-03-22 01:24:38 +01:00
var podcastEpisode = this . currentDownload . podcastEpisode
podcastEpisode . audioFile = audioFile
libraryItem . media . addPodcastEpisode ( podcastEpisode )
2022-04-25 00:03:43 +02:00
if ( libraryItem . isInvalid ) {
// First episode added to an empty podcast
libraryItem . isInvalid = false
}
2022-03-27 00:23:33 +01:00
libraryItem . libraryFiles . push ( libraryFile )
2022-03-22 01:24:38 +01:00
libraryItem . updatedAt = Date . now ( )
await this . db . updateLibraryItem ( libraryItem )
this . emitter ( 'item_updated' , libraryItem . toJSONExpanded ( ) )
return true
}
async getLibraryFile ( path , relPath ) {
var newLibFile = new LibraryFile ( )
await newLibFile . setDataFromPath ( path , relPath )
return newLibFile
}
2022-03-20 22:41:06 +01:00
2022-03-22 01:24:38 +01:00
async probeAudioFile ( libraryFile ) {
var path = libraryFile . metadata . path
2022-05-31 02:26:53 +02:00
var mediaProbeData = await prober . probe ( path )
if ( mediaProbeData . error ) {
Logger . error ( ` [PodcastManager] Podcast Episode downloaded but failed to probe " ${ path } " ` , mediaProbeData . error )
2022-03-22 01:24:38 +01:00
return false
}
var newAudioFile = new AudioFile ( )
2022-05-31 02:26:53 +02:00
newAudioFile . setDataFromProbe ( libraryFile , mediaProbeData )
2022-03-22 01:24:38 +01:00
return newAudioFile
2022-03-20 22:41:06 +01:00
}
2022-03-26 17:59:34 +01:00
schedulePodcastEpisodeCron ( ) {
try {
2022-03-27 01:58:59 +01:00
Logger . debug ( ` [PodcastManager] Scheduled podcast episode check cron " ${ this . serverSettings . podcastEpisodeSchedule } " ` )
2022-05-03 00:17:26 +02:00
this . episodeScheduleTask = cron . schedule ( this . serverSettings . podcastEpisodeSchedule , ( ) => {
Logger . debug ( ` [PodcastManager] Running cron ` )
this . checkForNewEpisodes ( )
} )
2022-03-26 17:59:34 +01:00
} catch ( error ) {
2022-03-27 01:58:59 +01:00
Logger . error ( ` [PodcastManager] Failed to schedule podcast cron ${ this . serverSettings . podcastEpisodeSchedule } ` , error )
2022-03-26 17:59:34 +01:00
}
}
2022-03-27 01:58:59 +01:00
cancelCron ( ) {
Logger . debug ( ` [PodcastManager] Canceled new podcast episode check cron ` )
if ( this . episodeScheduleTask ) {
this . episodeScheduleTask . destroy ( )
this . episodeScheduleTask = null
}
}
async checkForNewEpisodes ( ) {
2022-04-14 17:15:42 +02:00
var podcastsWithAutoDownload = this . db . libraryItems . filter ( li => li . mediaType === 'podcast' && li . media . autoDownloadEpisodes )
2022-03-27 01:58:59 +01:00
if ( ! podcastsWithAutoDownload . length ) {
2022-05-03 00:17:26 +02:00
Logger . info ( ` [PodcastManager] checkForNewEpisodes - No podcasts with auto download set ` )
2022-03-27 01:58:59 +01:00
this . cancelCron ( )
return
}
2022-05-03 00:17:26 +02:00
Logger . debug ( ` [PodcastManager] checkForNewEpisodes - Checking ${ podcastsWithAutoDownload . length } Podcasts ` )
2022-03-27 01:58:59 +01:00
2022-03-26 17:59:34 +01:00
for ( const libraryItem of podcastsWithAutoDownload ) {
2022-04-14 17:15:42 +02:00
const lastEpisodeCheckDate = new Date ( libraryItem . media . lastEpisodeCheck || 0 )
2022-05-12 01:55:19 +02:00
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 )
2022-05-03 00:17:26 +02:00
Logger . debug ( ` [PodcastManager] checkForNewEpisodes checked result ${ newEpisodes ? newEpisodes . length : 'N/A' } ` )
2022-04-14 17:15:42 +02:00
2022-03-27 01:58:59 +01:00
if ( ! newEpisodes ) { // Failed
2022-05-02 02:54:33 +02:00
// Allow up to 3 failed attempts before disabling auto download
if ( ! this . failedCheckMap [ libraryItem . id ] ) this . failedCheckMap [ libraryItem . id ] = 0
this . failedCheckMap [ libraryItem . id ] ++
if ( this . failedCheckMap [ libraryItem . id ] > 2 ) {
Logger . error ( ` [PodcastManager] checkForNewEpisodes 3 failed attempts at checking episodes for " ${ libraryItem . media . metadata . title } " - disabling auto download ` )
libraryItem . media . autoDownloadEpisodes = false
delete this . failedCheckMap [ libraryItem . id ]
} else {
Logger . warn ( ` [PodcastManager] checkForNewEpisodes ${ this . failedCheckMap [ libraryItem . id ] } failed attempts at checking episodes for " ${ libraryItem . media . metadata . title } " ` )
}
2022-03-27 01:58:59 +01:00
} else if ( newEpisodes . length ) {
2022-05-02 02:54:33 +02:00
delete this . failedCheckMap [ libraryItem . id ]
2022-03-27 01:58:59 +01:00
Logger . info ( ` [PodcastManager] Found ${ newEpisodes . length } new episodes for podcast " ${ libraryItem . media . metadata . title } " - starting download ` )
this . downloadPodcastEpisodes ( libraryItem , newEpisodes )
2022-04-14 17:15:42 +02:00
} else {
2022-05-02 02:54:33 +02:00
delete this . failedCheckMap [ libraryItem . id ]
2022-04-14 17:15:42 +02:00
Logger . debug ( ` [PodcastManager] No new episodes for " ${ libraryItem . media . metadata . title } " ` )
2022-03-27 01:58:59 +01:00
}
2022-03-26 17:59:34 +01:00
2022-04-14 17:15:42 +02:00
libraryItem . media . lastEpisodeCheck = Date . now ( )
libraryItem . updatedAt = Date . now ( )
await this . db . updateLibraryItem ( libraryItem )
this . emitter ( 'item_updated' , libraryItem . toJSONExpanded ( ) )
2022-03-26 17:59:34 +01:00
}
}
2022-05-12 01:55:19 +02:00
async checkPodcastForNewEpisodes ( podcastLibraryItem , dateToCheckForEpisodesAfter ) {
2022-03-27 01:58:59 +01:00
if ( ! podcastLibraryItem . media . metadata . feedUrl ) {
2022-05-02 02:54:33 +02:00
Logger . error ( ` [PodcastManager] checkPodcastForNewEpisodes no feed url for ${ podcastLibraryItem . media . metadata . title } (ID: ${ podcastLibraryItem . id } ) ` )
2022-03-27 01:58:59 +01:00
return false
}
var feed = await this . getPodcastFeed ( podcastLibraryItem . media . metadata . feedUrl )
if ( ! feed || ! feed . episodes ) {
2022-05-02 02:54:33 +02:00
Logger . error ( ` [PodcastManager] checkPodcastForNewEpisodes invalid feed payload for ${ podcastLibraryItem . media . metadata . title } (ID: ${ podcastLibraryItem . id } ) ` , feed )
2022-03-27 01:58:59 +01:00
return false
}
2022-05-02 02:54:33 +02:00
2022-03-27 01:58:59 +01:00
// Filter new and not already has
2022-05-12 01:55:19 +02:00
var newEpisodes = feed . episodes . filter ( ep => ep . publishedAt > dateToCheckForEpisodesAfter && ! podcastLibraryItem . media . checkHasEpisodeByFeedUrl ( ep . enclosure . url ) )
2022-04-29 23:42:40 +02:00
// Max new episodes for safety = 3
newEpisodes = newEpisodes . slice ( 0 , 3 )
return newEpisodes
}
async checkAndDownloadNewEpisodes ( libraryItem ) {
const lastEpisodeCheckDate = new Date ( libraryItem . media . lastEpisodeCheck || 0 )
Logger . info ( ` [PodcastManager] checkAndDownloadNewEpisodes for " ${ libraryItem . media . metadata . title } " - Last episode check: ${ lastEpisodeCheckDate } ` )
2022-05-12 01:55:19 +02:00
var newEpisodes = await this . checkPodcastForNewEpisodes ( libraryItem , libraryItem . media . lastEpisodeCheck )
2022-04-29 23:42:40 +02:00
if ( newEpisodes . length ) {
Logger . info ( ` [PodcastManager] Found ${ newEpisodes . length } new episodes for podcast " ${ libraryItem . media . metadata . title } " - starting download ` )
this . downloadPodcastEpisodes ( libraryItem , newEpisodes )
} else {
Logger . info ( ` [PodcastManager] No new episodes found for podcast " ${ libraryItem . media . metadata . title } " ` )
}
libraryItem . media . lastEpisodeCheck = Date . now ( )
libraryItem . updatedAt = Date . now ( )
await this . db . updateLibraryItem ( libraryItem )
this . emitter ( 'item_updated' , libraryItem . toJSONExpanded ( ) )
2022-03-27 01:58:59 +01:00
return newEpisodes
}
2022-05-29 18:46:45 +02:00
getPodcastFeed ( feedUrl , excludeEpisodeMetadata = false ) {
2022-05-03 00:17:26 +02:00
Logger . debug ( ` [PodcastManager] getPodcastFeed for " ${ feedUrl } " ` )
return axios . get ( feedUrl , { timeout : 5000 } ) . then ( async ( data ) => {
2022-03-26 17:59:34 +01:00
if ( ! data || ! data . data ) {
Logger . error ( 'Invalid podcast feed request response' )
2022-03-27 01:58:59 +01:00
return false
2022-03-26 17:59:34 +01:00
}
2022-05-03 00:17:26 +02:00
Logger . debug ( ` [PodcastManager] getPodcastFeed for " ${ feedUrl } " success - parsing xml ` )
2022-05-29 18:46:45 +02:00
var payload = await parsePodcastRssFeedXml ( data . data , excludeEpisodeMetadata )
2022-04-13 23:55:48 +02:00
if ( ! payload ) {
2022-03-27 01:58:59 +01:00
return false
2022-03-26 17:59:34 +01:00
}
2022-04-13 23:55:48 +02:00
return payload . podcast
2022-03-26 17:59:34 +01:00
} ) . catch ( ( error ) => {
console . error ( 'Failed' , error )
2022-03-27 01:58:59 +01:00
return false
2022-03-26 17:59:34 +01:00
} )
}
2022-05-29 18:46:45 +02:00
async getOPMLFeeds ( opmlText ) {
var extractedFeeds = opmlParser . parse ( opmlText )
if ( ! extractedFeeds || ! extractedFeeds . length ) {
Logger . error ( '[PodcastManager] getOPMLFeeds: No RSS feeds found in OPML' )
return {
error : 'No RSS feeds found in OPML'
}
}
var rssFeedData = [ ]
for ( let feed of extractedFeeds ) {
var feedData = await this . getPodcastFeed ( feed . feedUrl , true )
if ( feedData ) {
feedData . metadata . feedUrl = feed . feedUrl
rssFeedData . push ( feedData )
}
}
return {
feeds : rssFeedData
}
}
2022-03-20 22:41:06 +01:00
}
module . exports = PodcastManager