audiobookshelf/server/utils/generators/opmlGenerator.js

58 lines
1.2 KiB
JavaScript
Raw Normal View History

2023-05-28 22:10:34 +02:00
const xml = require('../../libs/xml')
/**
* 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) => {
2023-05-28 22:10:34 +02:00
const bodyItems = []
podcasts.forEach((podcast) => {
if (!podcast.feedURL) return
2023-05-28 22:10:34 +02:00
const feedAttributes = {
type: 'rss',
text: podcast.title,
title: podcast.title,
xmlUrl: podcast.feedURL
2023-05-28 22:10:34 +02:00
}
if (podcast.description) {
feedAttributes.description = podcast.description
2023-05-28 22:10:34 +02:00
}
if (podcast.itunesPageUrl) {
feedAttributes.htmlUrl = podcast.itunesPageUrl
2023-05-28 22:10:34 +02:00
}
if (podcast.language) {
feedAttributes.language = podcast.language
2023-05-28 22:10:34 +02:00
}
bodyItems.push({
outline: {
_attr: feedAttributes
}
})
})
const data = [
{
opml: [
{
_attr: {
version: '1.0'
}
},
{
head: [
{
title: 'Audiobookshelf Podcast Subscriptions'
}
]
},
{
body: bodyItems
}
]
}
]
return '<?xml version="1.0" encoding="UTF-8"?>\n' + xml(data, indent)
}