Skip to content

Instantly share code, notes, and snippets.

@abdorah
Last active March 12, 2023 19:26
Show Gist options
  • Save abdorah/ed29fdfe9cd139ca1088964c2ea4bc52 to your computer and use it in GitHub Desktop.
Save abdorah/ed29fdfe9cd139ca1088964c2ea4bc52 to your computer and use it in GitHub Desktop.
A nodeJs script that loops on a list of files and create a google podcasts rss feed file
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
<channel>
<title></title>
<itunes:author></itunes:author>
<description></description>
<itunes:image href="link to a picture"/>
<language>ar</language>
<item>
<title>سورة الفاتحة - 1</title>
<enclosure url="link to mp3 file" length="int value of size in bytes"
type="audio/mpeg"/>
</item>
</channel>
</rss>
const fs = require('fs');
// Util methods to create xml-like tags
const wrap = (tagName, value) => `<${tagName}>${value}</${tagName}>`;
const enclose = (tagName, attributes) => {
const attrString = Array.from(attributes.entries())
.map(([key, value]) => `${key}="${value}"`)
.join(' ');
return `<${tagName} ${attrString}/>`;
};
// Util method to write to a file.
const write = (fileName, content) => fs.writeFile(fileName, content, (err) => {
if (err) {
console.error(err);
return;
}
console.log('File has been written!');
});
// Util method to get the size of a file in bytes
const getLength = (filePath) => {
return new Promise((resolve, reject) => {
fs.stat(filePath, function (err, stats) {
if (err) {
reject(err);
} else {
resolve(stats.size);
}
});
});
}
const rootUrl = "https://firebasestorage.googleapis.com/v0/b/project";
const rootFolder = "C:\\Users\\Kotbi Abderrahmane\\files\\";
async function main() {
const map = new Map();
let items = `
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
<channel>
<title><title>
<itunes:author></itunes:author>
<description></description>
<itunes:image href="https://firebasestorage.googleapis.com/v0/b/picture.jpg"/>
<language>ar</language>
`;
const promises = [];
for (let index = 1; index < 115; index++) {
const number = String(index).padStart(3, '0')
const fileName = `${rootFolder + number}.mp3`
const url = `${rootUrl}F001.mp3`
const promise = getLength(fileName).then((fileLength) => {
map.set("length", fileLength).set("url", url).set("type", "audio/mpeg")
items += wrap("item", "\n\t\t\t" + [wrap("title", number) + "\n\t\t\t" + enclose("enclosure", map) + "\n\t\t"]) + "\n\t\t"
}).catch((err) => {
console.error(err);
});
promises.push(promise);
}
await Promise.allSettled(promises);
items = items.slice(0, -2) + "\t</channel>\n"+ "</rss>";
write("file.xml", items)
};
main();
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read: if true;
allow write: if false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment