Created
April 2, 2018 00:43
-
-
Save robcalcroft/5e7bb8864ac146927bf7c8cafcbd0eca to your computer and use it in GitHub Desktop.
XML podcast feed utility
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const xml2js = require('xml2js'); | |
const parser = new xml2js.Parser(); | |
const builder = new xml2js.Builder(); | |
const createFeedItem = ({ | |
title, | |
filePath, | |
callback | |
}) => { | |
const fileName = filePath.split('/').slice(-1)[0]; | |
const size = fs.statSync(filePath).size; | |
const guessedDuration = Math.round(size/1000000) + 2; | |
let duration = '01:00:00'; | |
if (guessedDuration >= 60) { | |
duration = `01:0${guessedDuration - 60}:00`; | |
} else { | |
duration = `00:${guessedDuration}:00`; | |
} | |
return { | |
title: [title], | |
guid: [`http://----/ricky-gervais-show-xfm/episodes/${fileName}`], | |
description: [title], | |
enclosure: [{ | |
'$': { | |
url: `http://----/ricky-gervais-show-xfm/episodes/${fileName}`, | |
length: String(size), | |
type: 'audio/mpeg', | |
}, | |
}], | |
category: ['Comedy'], | |
pubDate: [new Date().toUTCString()], | |
'itunes:author': ['Ricky Gervais, Stephen Merchant, Karl Pilkington'], | |
'itunes:subtitle': [title], | |
'itunes:summary': [title], | |
'itunes:duration': [duration], | |
}; | |
} | |
const feed = fs.readFileSync(`${__dirname}/feed.xml`); | |
parser.parseString(feed, (error, result) => { | |
result.rss.channel[0].lastBuildDate = new Date().toUTCString(); | |
result.rss.channel[0].item = []; | |
fs.readdirSync(`${__dirname}/episodes`).forEach(file => { | |
result.rss.channel[0].item.push(createFeedItem({ | |
title: file, | |
filePath: `${__dirname}/episodes/${file}`, | |
})); | |
}); | |
fs.writeFileSync('feed.xml', builder.buildObject(result)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment