2022-05-02 21:41:59 +02:00
|
|
|
const Path = require('path')
|
2022-05-03 00:21:16 +02:00
|
|
|
const fs = require('fs-extra')
|
2022-05-20 01:51:58 +02:00
|
|
|
const date = require('date-and-time')
|
2022-05-02 21:41:59 +02:00
|
|
|
const { Podcast } = require('podcast')
|
|
|
|
const Logger = require('../Logger')
|
|
|
|
|
|
|
|
// Not functional at the moment
|
|
|
|
class RssFeedManager {
|
2022-05-02 23:42:30 +02:00
|
|
|
constructor(db, emitter) {
|
2022-05-02 21:41:59 +02:00
|
|
|
this.db = db
|
2022-05-02 23:42:30 +02:00
|
|
|
this.emitter = emitter
|
2022-05-02 21:41:59 +02:00
|
|
|
this.feeds = {}
|
|
|
|
}
|
|
|
|
|
2022-05-02 23:42:30 +02:00
|
|
|
findFeedForItem(libraryItemId) {
|
|
|
|
return Object.values(this.feeds).find(feed => feed.libraryItemId === libraryItemId)
|
|
|
|
}
|
|
|
|
|
2022-05-02 21:41:59 +02:00
|
|
|
getFeed(req, res) {
|
|
|
|
var feedData = this.feeds[req.params.id]
|
|
|
|
if (!feedData) {
|
|
|
|
Logger.error(`[RssFeedManager] Feed not found ${req.params.id}`)
|
|
|
|
res.sendStatus(404)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var xml = feedData.feed.buildXml()
|
|
|
|
res.set('Content-Type', 'text/xml')
|
|
|
|
res.send(xml)
|
|
|
|
}
|
|
|
|
|
|
|
|
getFeedItem(req, res) {
|
|
|
|
var feedData = this.feeds[req.params.id]
|
|
|
|
if (!feedData) {
|
|
|
|
Logger.error(`[RssFeedManager] Feed not found ${req.params.id}`)
|
|
|
|
res.sendStatus(404)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var remainingPath = req.params['0']
|
|
|
|
var fullPath = Path.join(feedData.libraryItemPath, remainingPath)
|
|
|
|
res.sendFile(fullPath)
|
|
|
|
}
|
|
|
|
|
2022-05-02 23:42:30 +02:00
|
|
|
getFeedCover(req, res) {
|
|
|
|
var feedData = this.feeds[req.params.id]
|
|
|
|
if (!feedData) {
|
|
|
|
Logger.error(`[RssFeedManager] Feed not found ${req.params.id}`)
|
|
|
|
res.sendStatus(404)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!feedData.mediaCoverPath) {
|
|
|
|
res.sendStatus(404)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const extname = Path.extname(feedData.mediaCoverPath).toLowerCase().slice(1)
|
|
|
|
res.type(`image/${extname}`)
|
|
|
|
var readStream = fs.createReadStream(feedData.mediaCoverPath)
|
|
|
|
readStream.pipe(res)
|
|
|
|
}
|
|
|
|
|
2022-05-04 01:52:34 +02:00
|
|
|
openFeed(userId, slug, libraryItem, serverAddress) {
|
2022-05-20 01:51:58 +02:00
|
|
|
const media = libraryItem.media
|
|
|
|
const mediaMetadata = media.metadata
|
|
|
|
const isPodcast = libraryItem.mediaType === 'podcast'
|
2022-05-02 21:41:59 +02:00
|
|
|
|
2022-05-04 01:52:34 +02:00
|
|
|
const feedUrl = `${serverAddress}/feed/${slug}`
|
2022-05-20 01:51:58 +02:00
|
|
|
const author = isPodcast ? mediaMetadata.author : mediaMetadata.authorName
|
|
|
|
|
2022-05-02 21:41:59 +02:00
|
|
|
const feed = new Podcast({
|
2022-05-20 01:51:58 +02:00
|
|
|
title: mediaMetadata.title,
|
|
|
|
description: mediaMetadata.description,
|
2022-05-02 21:41:59 +02:00
|
|
|
feedUrl,
|
2022-05-20 01:51:58 +02:00
|
|
|
siteUrl: `${serverAddress}/items/${libraryItem.id}`,
|
|
|
|
imageUrl: media.coverPath ? `${serverAddress}/feed/${slug}/cover` : `${serverAddress}/Logo.png`,
|
|
|
|
author: author || 'advplyr',
|
2022-05-02 21:41:59 +02:00
|
|
|
language: 'en'
|
|
|
|
})
|
2022-05-20 01:51:58 +02:00
|
|
|
|
|
|
|
if (isPodcast) { // PODCAST EPISODES
|
|
|
|
media.episodes.forEach((episode) => {
|
|
|
|
var contentUrl = episode.audioTrack.contentUrl.replace(/\\/g, '/')
|
|
|
|
contentUrl = contentUrl.replace(`/s/item/${libraryItem.id}`, `/feed/${slug}/item`)
|
|
|
|
|
|
|
|
feed.addItem({
|
|
|
|
title: episode.title,
|
|
|
|
description: episode.description || '',
|
|
|
|
enclosure: {
|
|
|
|
url: `${serverAddress}${contentUrl}`,
|
|
|
|
type: episode.audioTrack.mimeType,
|
|
|
|
size: episode.size
|
|
|
|
},
|
|
|
|
date: episode.pubDate || '',
|
2022-05-02 21:41:59 +02:00
|
|
|
url: `${serverAddress}${contentUrl}`,
|
2022-05-20 01:51:58 +02:00
|
|
|
author: author || 'advplyr'
|
|
|
|
})
|
2022-05-02 21:41:59 +02:00
|
|
|
})
|
2022-05-20 01:51:58 +02:00
|
|
|
} else { // AUDIOBOOK EPISODES
|
|
|
|
|
|
|
|
// Example: <pubDate>Fri, 04 Feb 2015 00:00:00 GMT</pubDate>
|
|
|
|
const audiobookPubDate = date.format(new Date(libraryItem.addedAt), 'ddd, DD MMM YYYY HH:mm:ss [GMT]')
|
|
|
|
|
|
|
|
media.tracks.forEach((audioTrack) => {
|
|
|
|
var contentUrl = audioTrack.contentUrl.replace(/\\/g, '/')
|
|
|
|
contentUrl = contentUrl.replace(`/s/item/${libraryItem.id}`, `/feed/${slug}/item`)
|
|
|
|
|
|
|
|
var title = audioTrack.title
|
|
|
|
if (media.chapters.length) {
|
|
|
|
// If audio track start and chapter start are within 1 seconds of eachother then use the chapter title
|
|
|
|
var matchingChapter = media.chapters.find(ch => Math.abs(ch.start - audioTrack.startOffset) < 1)
|
|
|
|
if (matchingChapter && matchingChapter.title) title = matchingChapter.title
|
|
|
|
}
|
|
|
|
|
|
|
|
feed.addItem({
|
|
|
|
title,
|
|
|
|
description: '',
|
|
|
|
enclosure: {
|
|
|
|
url: `${serverAddress}${contentUrl}`,
|
|
|
|
type: audioTrack.mimeType,
|
|
|
|
size: audioTrack.metadata.size
|
|
|
|
},
|
|
|
|
date: audiobookPubDate,
|
|
|
|
url: `${serverAddress}${contentUrl}`,
|
|
|
|
author: author || 'advplyr'
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-05-02 21:41:59 +02:00
|
|
|
|
|
|
|
const feedData = {
|
2022-05-04 01:52:34 +02:00
|
|
|
id: slug,
|
|
|
|
slug,
|
2022-05-02 23:42:30 +02:00
|
|
|
userId,
|
2022-05-02 21:41:59 +02:00
|
|
|
libraryItemId: libraryItem.id,
|
|
|
|
libraryItemPath: libraryItem.path,
|
2022-05-20 01:51:58 +02:00
|
|
|
mediaCoverPath: media.coverPath,
|
2022-05-02 21:41:59 +02:00
|
|
|
serverAddress: serverAddress,
|
|
|
|
feedUrl,
|
|
|
|
feed
|
|
|
|
}
|
2022-05-04 01:52:34 +02:00
|
|
|
this.feeds[slug] = feedData
|
2022-05-02 21:41:59 +02:00
|
|
|
return feedData
|
|
|
|
}
|
|
|
|
|
2022-05-20 01:51:58 +02:00
|
|
|
openFeedForItem(user, libraryItem, options) {
|
2022-05-02 21:41:59 +02:00
|
|
|
const serverAddress = options.serverAddress
|
2022-05-04 01:52:34 +02:00
|
|
|
const slug = options.slug
|
|
|
|
|
|
|
|
if (this.feeds[slug]) {
|
|
|
|
Logger.error(`[RssFeedManager] Slug already in use`)
|
|
|
|
return {
|
|
|
|
error: `Slug "${slug}" already in use`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const feedData = this.openFeed(user.id, slug, libraryItem, serverAddress)
|
2022-05-20 01:51:58 +02:00
|
|
|
Logger.debug(`[RssFeedManager] Opened RSS feed ${feedData.feedUrl}`)
|
2022-05-02 23:42:30 +02:00
|
|
|
this.emitter('rss_feed_open', { libraryItemId: libraryItem.id, feedUrl: feedData.feedUrl })
|
2022-05-02 21:41:59 +02:00
|
|
|
return feedData
|
|
|
|
}
|
2022-05-02 23:42:30 +02:00
|
|
|
|
2022-05-20 01:51:58 +02:00
|
|
|
closeFeedForItem(libraryItemId) {
|
2022-05-02 23:42:30 +02:00
|
|
|
var feed = this.findFeedForItem(libraryItemId)
|
|
|
|
if (!feed) return
|
|
|
|
this.closeRssFeed(feed.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
closeRssFeed(id) {
|
|
|
|
if (!this.feeds[id]) return
|
|
|
|
var feedData = this.feeds[id]
|
|
|
|
this.emitter('rss_feed_closed', { libraryItemId: feedData.libraryItemId, feedUrl: feedData.feedUrl })
|
|
|
|
delete this.feeds[id]
|
|
|
|
Logger.info(`[RssFeedManager] Closed RSS feed "${feedData.feedUrl}"`)
|
|
|
|
}
|
2022-05-02 21:41:59 +02:00
|
|
|
}
|
|
|
|
module.exports = RssFeedManager
|