audiobookshelf/server/utils/generators/opmlGenerator.js

59 lines
1.4 KiB
JavaScript
Raw Normal View History

2023-05-28 22:10:34 +02:00
const xml = require('../../libs/xml')
const escapeForXML = require('../../libs/xml/escapeForXML')
2023-05-28 22:10:34 +02:00
/**
* 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: escapeForXML(podcast.title),
title: escapeForXML(podcast.title),
xmlUrl: escapeForXML(podcast.feedURL)
2023-05-28 22:10:34 +02:00
}
if (podcast.description) {
feedAttributes.description = escapeForXML(podcast.description)
2023-05-28 22:10:34 +02:00
}
if (podcast.itunesPageUrl) {
feedAttributes.htmlUrl = escapeForXML(podcast.itunesPageUrl)
2023-05-28 22:10:34 +02:00
}
if (podcast.language) {
feedAttributes.language = escapeForXML(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)
}