Update OPML api route to load podcasts from db

This commit is contained in:
advplyr 2023-08-19 15:19:27 -05:00
parent 8d451217a3
commit ff0d6326d3
4 changed files with 48 additions and 17 deletions

View File

@ -1047,8 +1047,28 @@ class LibraryController {
res.json(payload) res.json(payload)
} }
getOPMLFile(req, res) { /**
const opmlText = this.podcastManager.generateOPMLFileText(req.libraryItems) * GET: /api/libraries/:id/opml
* Get OPML file for a podcast library
* @param {import('express').Request} req
* @param {import('express').Response} res
*/
async getOPMLFile(req, res) {
const userPermissionPodcastWhere = libraryItemsPodcastFilters.getUserPermissionPodcastWhereQuery(req.user)
const podcasts = await Database.podcastModel.findAll({
attributes: ['id', 'feedURL', 'title', 'description', 'itunesPageURL', 'language'],
where: userPermissionPodcastWhere.podcastWhere,
replacements: userPermissionPodcastWhere.replacements,
include: {
model: Database.libraryItemModel,
attributes: ['id', 'libraryId'],
where: {
libraryId: req.library.id
}
}
})
const opmlText = this.podcastManager.generateOPMLFileText(podcasts)
res.type('application/xml') res.type('application/xml')
res.send(opmlText) res.send(opmlText)
} }

View File

@ -372,8 +372,13 @@ class PodcastManager {
} }
} }
generateOPMLFileText(libraryItems) { /**
return opmlGenerator.generate(libraryItems) * OPML file string for podcasts in a library
* @param {import('../models/Podcast')[]} podcasts
* @returns {string} XML string
*/
generateOPMLFileText(podcasts) {
return opmlGenerator.generate(podcasts)
} }
getDownloadQueueDetails(libraryId = null) { getDownloadQueueDetails(libraryId = null) {

View File

@ -95,7 +95,7 @@ class ApiRouter {
this.router.get('/libraries/:id/matchall', LibraryController.middlewareNew.bind(this), LibraryController.matchAll.bind(this)) this.router.get('/libraries/:id/matchall', LibraryController.middlewareNew.bind(this), LibraryController.matchAll.bind(this))
this.router.post('/libraries/:id/scan', LibraryController.middlewareNew.bind(this), LibraryController.scan.bind(this)) this.router.post('/libraries/:id/scan', LibraryController.middlewareNew.bind(this), LibraryController.scan.bind(this))
this.router.get('/libraries/:id/recent-episodes', LibraryController.middlewareNew.bind(this), LibraryController.getRecentEpisodes.bind(this)) this.router.get('/libraries/:id/recent-episodes', LibraryController.middlewareNew.bind(this), LibraryController.getRecentEpisodes.bind(this))
this.router.get('/libraries/:id/opml', LibraryController.middleware.bind(this), LibraryController.getOPMLFile.bind(this)) this.router.get('/libraries/:id/opml', LibraryController.middlewareNew.bind(this), LibraryController.getOPMLFile.bind(this))
this.router.post('/libraries/order', LibraryController.reorder.bind(this)) this.router.post('/libraries/order', LibraryController.reorder.bind(this))
// //

View File

@ -1,23 +1,29 @@
const xml = require('../../libs/xml') const xml = require('../../libs/xml')
module.exports.generate = (libraryItems, indent = true) => { /**
* Generate OPML file string for podcasts in a library
* @param {import('../../models/Podcast')[]} podcasts
* @param {boolean} [indent=true]
* @returns {string}
*/
module.exports.generate = (podcasts, indent = true) => {
const bodyItems = [] const bodyItems = []
libraryItems.forEach((item) => { podcasts.forEach((podcast) => {
if (!item.media.metadata.feedUrl) return if (!podcast.feedURL) return
const feedAttributes = { const feedAttributes = {
type: 'rss', type: 'rss',
text: item.media.metadata.title, text: podcast.title,
title: item.media.metadata.title, title: podcast.title,
xmlUrl: item.media.metadata.feedUrl xmlUrl: podcast.feedURL
} }
if (item.media.metadata.description) { if (podcast.description) {
feedAttributes.description = item.media.metadata.description feedAttributes.description = podcast.description
} }
if (item.media.metadata.itunesPageUrl) { if (podcast.itunesPageUrl) {
feedAttributes.htmlUrl = item.media.metadata.itunesPageUrl feedAttributes.htmlUrl = podcast.itunesPageUrl
} }
if (item.media.metadata.language) { if (podcast.language) {
feedAttributes.language = item.media.metadata.language feedAttributes.language = podcast.language
} }
bodyItems.push({ bodyItems.push({
outline: { outline: {