2022-03-22 01:24:38 +01:00
|
|
|
const fs = require('fs-extra')
|
2022-03-26 17:59:34 +01:00
|
|
|
const cron = require('node-cron')
|
|
|
|
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')
|
|
|
|
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-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
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
} else {
|
|
|
|
Logger.info(`[PodcastManager] Successfully downloaded podcast episode "${this.currentDownload.podcastEpisode.title}"`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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
|
|
|
|
var audioProbeData = await prober.probe(path)
|
|
|
|
if (audioProbeData.error) {
|
|
|
|
Logger.error(`[PodcastManager] Podcast Episode downloaded but failed to probe "${path}"`, audioProbeData.error)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
var newAudioFile = new AudioFile()
|
|
|
|
newAudioFile.setDataFromProbe(libraryFile, audioProbeData)
|
|
|
|
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-03-26 17:59:34 +01:00
|
|
|
this.episodeScheduleTask = cron.schedule(this.serverSettings.podcastEpisodeSchedule, this.checkForNewEpisodes.bind(this))
|
|
|
|
} 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-03-26 17:59:34 +01:00
|
|
|
var podcastsWithAutoDownload = this.db.libraryItems.find(li => li.mediaType === 'podcast' && li.media.autoDownloadEpisodes)
|
2022-03-27 01:58:59 +01:00
|
|
|
if (!podcastsWithAutoDownload.length) {
|
|
|
|
this.cancelCron()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-26 17:59:34 +01:00
|
|
|
for (const libraryItem of podcastsWithAutoDownload) {
|
2022-03-27 01:58:59 +01:00
|
|
|
Logger.info(`[PodcastManager] checkForNewEpisodes Cron for "${libraryItem.media.metadata.title}"`)
|
|
|
|
var newEpisodes = await this.checkPodcastForNewEpisodes(libraryItem)
|
|
|
|
var hasUpdates = false
|
|
|
|
if (!newEpisodes) { // Failed
|
|
|
|
libraryItem.media.autoDownloadEpisodes = false
|
|
|
|
hasUpdates = true
|
|
|
|
} else if (newEpisodes.length) {
|
|
|
|
Logger.info(`[PodcastManager] Found ${newEpisodes.length} new episodes for podcast "${libraryItem.media.metadata.title}" - starting download`)
|
|
|
|
this.downloadPodcastEpisodes(libraryItem, newEpisodes)
|
|
|
|
hasUpdates = true
|
|
|
|
}
|
2022-03-26 17:59:34 +01:00
|
|
|
|
2022-03-27 01:58:59 +01:00
|
|
|
if (hasUpdates) {
|
|
|
|
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-03-27 01:58:59 +01:00
|
|
|
async checkPodcastForNewEpisodes(podcastLibraryItem) {
|
|
|
|
if (!podcastLibraryItem.media.metadata.feedUrl) {
|
|
|
|
Logger.error(`[PodcastManager] checkPodcastForNewEpisodes no feed url for item ${podcastLibraryItem.id} - disabling auto download`)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
var feed = await this.getPodcastFeed(podcastLibraryItem.media.metadata.feedUrl)
|
|
|
|
if (!feed || !feed.episodes) {
|
|
|
|
Logger.error(`[PodcastManager] checkPodcastForNewEpisodes invalid feed payload ${podcastLibraryItem.id} - disabling auto download`)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// Filter new and not already has
|
|
|
|
var newEpisodes = feed.episodes.filter(ep => ep.publishedAt > podcastLibraryItem.media.lastEpisodeCheck && !podcastLibraryItem.media.checkHasEpisodeByFeedUrl(ep.enclosure.url))
|
|
|
|
// Max new episodes for safety = 2
|
|
|
|
newEpisodes = newEpisodes.slice(0, 2)
|
|
|
|
return newEpisodes
|
|
|
|
}
|
|
|
|
|
|
|
|
getPodcastFeed(feedUrl) {
|
|
|
|
return axios.get(feedUrl).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-04-13 23:55:48 +02:00
|
|
|
var payload = await parsePodcastRssFeedXml(data.data)
|
|
|
|
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-03-20 22:41:06 +01:00
|
|
|
}
|
|
|
|
module.exports = PodcastManager
|