Files
rpg-static-site/content/feeds/podcast.11ty.js
2024-11-11 08:41:45 -06:00

79 lines
2.8 KiB
JavaScript

const { Podcast } = require('podcast');
const music_metadata = require('music-metadata');
const fs = require('fs');
async function getMp3Duration(filePath) {
try {
const metadata = await music_metadata.parseFile(filePath);
console.log('Duration:', metadata.format.duration, 'seconds');
return metadata.format.duration; // Duration in seconds
} catch (error) {
console.error('Error reading file:', error.message);
return null;
}
}
class PodcastFeed {
data() {
return {
// Writes to "/my-permalink/hello/index.html"
permalink: (data) => `${data.page.filePathStem}.xml`,
};
}
async render(data) {
const feed = new Podcast({
title: 'title',
description: 'description',
feedUrl: `${data.site.url}${data.page.filePathStem}.xml`,
siteUrl: data.site.url,
imageUrl: 'http://example.com/icon.png',
author: `${data.site.author.name}`,
copyright: '2013 Dylan Greene',
language: 'en',
categories: ['Category 1','Category 2','Category 3'],
pubDate: 'May 20, 2012 04:00:00 GMT',
ttl: 60,
itunesAuthor: `${data.site.author.name}`,
itunesSubtitle: 'I am a sub title',
itunesSummary: 'I am a summary',
itunesOwner: { name: `${data.site.author.name}`, email: `${data.site.author.email}` },
itunesExplicit: false,
itunesCategory: [{
text: 'Entertainment',
subcats: [{
text: 'Television'
}]
}],
itunesImage: 'http://example.com/image.png'
});
data.collections.episode.forEach(episode=>{
const episode_data = episode.data
const zero_pad_season = episode_data.season.toString().padStart(2, '0')
// const duration = getMp3Duration(`../episodes/s${zero_pad_season}/s${zero_pad_season}e${episode_data.episode}.mp3`)
/* loop over data and add to feed */
feed.addItem({
title: `S${zero_pad_season}E${episode_data.episode}: ${episode_data.title}`,
description: episode.content,
url: episode.url, // link to the item
guid: episode.url, // optional - defaults to url
categories: episode_data.categories,
author: data.site.author.name,
date: episode_data.date, // any format that js Date can parse.
// enclosure : {url:`${data.site.url}/episodes/s${zero_pad_season}/s${zero_pad_season}e${episode_data.episode}.mp3`}, // optional enclosure
enclosure : {url:`https://cdn.localhost/podcast/s${zero_pad_season}e${episode_data.episode}.mp3`}, // optional enclosure
itunesAuthor: data.site.author,
itunesExplicit: episode_data.itunesExplicit,
// itunesDuration: duration,
});
})
// cache the xml to send to clients
const xml = feed.buildXml();
return xml
}
}
module.exports = PodcastFeed